Skip to content

Commit

Permalink
[gbc c++] Apply export attribute to enum functions
Browse files Browse the repository at this point in the history
Resolves #861
  • Loading branch information
ara-ayvazyan authored and chwarr committed Apr 25, 2018
1 parent 52deb69 commit 101a8f5
Show file tree
Hide file tree
Showing 10 changed files with 977 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ different versioning scheme, following the Haskell community's
* C++ codegen now generates lazily constructed enum name-to-value and value-to-name maps.
Additionally, a user-defined map type can now be provided to `GetNameToValueMap` and
`GetValueToNameMap`.
* C++ codegen now applies the `--export-attribute` to the `ToString`,
`FromString`, `ToEnum` and `FromEnum` functions.
* `import` statements can now end with an optional semicolon.

### C++ ###
Expand Down Expand Up @@ -85,6 +87,9 @@ different versioning scheme, following the Haskell community's
assignment operators.
* Added `operator==(const bond::maybe<T>&, const T&)` and `operator==(const
T&, const bond::maybe<T>&)` to compare directly to instances of `T`.
* Fixed an issue with the `ToString`, `FromString`, `ToEnum` and `FromEnum` functions
that were previously not exported from a DLL when the `--export-attribute` option was
passed to `gbc`. [Issue #861](https://github.com/Microsoft/bond/issues/861)

### C# ###

Expand Down
2 changes: 1 addition & 1 deletion compiler/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ cppCodegen options@Cpp {..} = do
]
core_files = [
reflection_h export_attribute
, types_h header enum_header allocator alloc_ctors_enabled type_aliases_enabled scoped_alloc_enabled
, types_h export_attribute header enum_header allocator alloc_ctors_enabled type_aliases_enabled scoped_alloc_enabled
, types_cpp
, apply_h applyProto export_attribute
, apply_cpp applyProto
Expand Down
14 changes: 8 additions & 6 deletions compiler/src/Language/Bond/Codegen/Cpp/Types_h.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ import qualified Language.Bond.Codegen.Cpp.Util as CPP

-- | Codegen template for generating /base_name/_type.h containing definitions
-- of C++ types representing the schema.
types_h :: [String] -- ^ list of optional header files to be @#include@'ed by the generated code
types_h :: Maybe String -- ^ Optional attribute to decorate the enum conversion function declarations
-> [String] -- ^ list of optional header files to be @#include@'ed by the generated code
-> Bool -- ^ 'True' to generate enum definitions into a separate file /base_name/_enum.h
-> Maybe String -- ^ optional custom allocator to be used in the generated code
-> Bool -- ^ 'True' to generate constructors with allocator
-> Bool -- ^ 'True' to generate type aliases
-> Bool -- ^ 'True' to use std::scoped_allocator_adaptor for strings and containers
-> MappingContext -> String -> [Import] -> [Declaration] -> (String, L.Text)
types_h userHeaders enumHeader allocator alloc_ctors_enabled type_aliases_enabled scoped_alloc_enabled cpp file imports declarations = ("_types.h", [lt|
types_h export_attribute userHeaders enumHeader allocator alloc_ctors_enabled type_aliases_enabled scoped_alloc_enabled cpp file imports declarations = ("_types.h", [lt|
#pragma once
#{newlineBeginSep 0 includeHeader userHeaders}
#include <bond/core/bond_version.h>
Expand Down Expand Up @@ -431,13 +432,13 @@ namespace std
return s_nameToValueMap;
}
#endif
const std::string& ToString(enum #{declName} value);
#{export_attr}const std::string& ToString(enum #{declName} value);

void FromString(const std::string& name, enum #{declName}& value);
#{export_attr}void FromString(const std::string& name, enum #{declName}& value);

bool ToEnum(enum #{declName}& value, const std::string& name);
#{export_attr}bool ToEnum(enum #{declName}& value, const std::string& name);

bool FromEnum(std::string& name, enum #{declName} value);
#{export_attr}bool FromEnum(std::string& name, enum #{declName} value);

} // namespace #{declName}
} // namespace _bond_enumerators
Expand All @@ -448,5 +449,6 @@ namespace std
|]
enumUsing = if enumHeader then mempty else [lt|using namespace _bond_enumerators::#{declName};
|]
export_attr = optional (\a -> [lt|#{a} |]) export_attribute

typeDeclaration _ = mempty
5 changes: 5 additions & 0 deletions compiler/tests/TestMain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ tests = testGroup "Compiler tests"
, "--export-attribute=DllExport"
]
"service"
, verifyExportsCodegen
[ "c++"
, "--export-attribute=DllExport"
]
"with_enum_header"
]
, verifyCodegen
[ "c++"
Expand Down
9 changes: 6 additions & 3 deletions compiler/tests/Tests/Codegen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,13 @@ verifyApplyCodegen args baseName =
verifyExportsCodegen :: [String] -> FilePath -> TestTree
verifyExportsCodegen args baseName =
testGroup baseName $
map (verifyFile options baseName (cppExpandAliases (type_aliases_enabled options) cppTypeMapping) "exports") templates
map (verifyFile options baseName (cppExpandAliases (type_aliases_enabled options) cppTypeMapping) "exports") (templates options)
where
options = processOptions args
templates = [ reflection_h (export_attribute options) ]
templates Cpp {..} =
[ reflection_h export_attribute
, types_h export_attribute header enum_header allocator alloc_ctors_enabled type_aliases_enabled scoped_alloc_enabled
]

verifyCppGrpcCodegen :: [String] -> FilePath -> TestTree
verifyCppGrpcCodegen args baseName =
Expand Down Expand Up @@ -121,7 +124,7 @@ verifyFiles options baseName =
templates Cpp {..} =
[ (reflection_h export_attribute)
, types_cpp
, types_h header enum_header allocator alloc_ctors_enabled type_aliases_enabled scoped_alloc_enabled
, types_h export_attribute header enum_header allocator alloc_ctors_enabled type_aliases_enabled scoped_alloc_enabled
] <>
[ enum_h | enum_header]
templates Cs {..} =
Expand Down
89 changes: 89 additions & 0 deletions compiler/tests/generated/exports/service_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@

#pragma once

#include <bond/core/bond_version.h>

#if BOND_VERSION < 0x0700
#error This file was generated by a newer version of the Bond compiler and is incompatible with your version of the Bond library.
#endif

#if BOND_MIN_CODEGEN_VERSION > 0x0b00
#error This file was generated by an older version of the Bond compiler and is incompatible with your version of the Bond library.
#endif

#include <bond/core/config.h>
#include <bond/core/containers.h>


#include "basic_types_types.h"
#include "namespace_basic_types_types.h"

namespace tests
{

struct dummy
{
int32_t count;

dummy()
: count()
{
}


// Compiler generated copy ctor OK
dummy(const dummy&) = default;

#if defined(_MSC_VER) && (_MSC_VER < 1900) // Versions of MSVC prior to 1900 do not support = default for move ctors
dummy(dummy&& other)
: count(std::move(other.count))
{
}
#else
dummy(dummy&&) = default;
#endif


#if defined(_MSC_VER) && (_MSC_VER < 1900) // Versions of MSVC prior to 1900 do not support = default for move ctors
dummy& operator=(dummy other)
{
other.swap(*this);
return *this;
}
#else
// Compiler generated operator= OK
dummy& operator=(const dummy&) = default;
dummy& operator=(dummy&&) = default;
#endif

bool operator==(const dummy& other) const
{
return true
&& (count == other.count);
}

bool operator!=(const dummy& other) const
{
return !(*this == other);
}

void swap(dummy& other)
{
using std::swap;
swap(count, other.count);
}

struct Schema;

protected:
void InitMetadata(const char*, const char*)
{
}
};

inline void swap(::tests::dummy& left, ::tests::dummy& right)
{
left.swap(right);
}
} // namespace tests

Loading

0 comments on commit 101a8f5

Please sign in to comment.