Skip to content

Commit 1959e99

Browse files
committed
[#459] Introduce non-explicit empty constructor in generated C++ code
1 parent 7aae904 commit 1959e99

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+444
-125
lines changed

compiler/extensions/cpp/freemarker/Choice.h.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public:
5757
};
5858
<#if withWriterCode>
5959

60+
<@compound_default_constructor compoundConstructorsData/>
61+
6062
<@compound_constructor_declaration compoundConstructorsData/>
6163
</#if>
6264

compiler/extensions/cpp/freemarker/CompoundConstructor.inc.ftl

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
<#include "CompoundField.inc.ftl">
22
<#include "CompoundParameter.inc.ftl">
3+
<#macro compound_default_constructor compoundConstructorsData>
4+
<#if withCodeComments>
5+
/**
6+
* Default constructor.
7+
*/
8+
</#if>
9+
${compoundConstructorsData.compoundName}() noexcept :
10+
${compoundConstructorsData.compoundName}(allocator_type())
11+
{}
12+
</#macro>
13+
314
<#macro compound_constructor_declaration compoundConstructorsData>
415
<#if withCodeComments>
516
/**
@@ -8,7 +19,7 @@
819
* \param allocator Allocator to construct from.
920
*/
1021
</#if>
11-
explicit ${compoundConstructorsData.compoundName}(const allocator_type& allocator = allocator_type()) noexcept;
22+
explicit ${compoundConstructorsData.compoundName}(const allocator_type& allocator) noexcept;
1223
</#macro>
1324

1425
<#macro compound_constructor_definition compoundConstructorsData memberInitializationMacroName="">

compiler/extensions/cpp/freemarker/CompoundField.inc.ftl

+2-2
Original file line numberDiff line numberDiff line change
@@ -1057,9 +1057,9 @@ ${I};
10571057
<#if !field.typeInfo.isSimple || field.optional??>
10581058
<#if numTemplateArgs != 0>
10591059
<#lt>,
1060-
typename ZSERIO_T_${field.name}<#rt>
1060+
typename ZSERIO_T_${field.name} = <@field_raw_cpp_type_name field/><#rt>
10611061
<#else>
1062-
typename ZSERIO_T_${field.name}<#t>
1062+
typename ZSERIO_T_${field.name} = <@field_raw_cpp_type_name field/><#t>
10631063
</#if>
10641064
<#if numTemplateArgs == 0 && field?is_first>
10651065
<#local firstTemplateArgName="ZSERIO_T_${field.name}"/>

compiler/extensions/cpp/freemarker/Service.h.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public:
5252
* \param allocator Allocator to construct from.
5353
*/
5454
</#if>
55-
Service(const allocator_type& allocator = allocator_type());
55+
explicit Service(const allocator_type& allocator = allocator_type());
5656
<#if withCodeComments>
5757

5858
/** Default destructor. */

compiler/extensions/cpp/freemarker/Structure.h.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public:
4747
using allocator_type = ${types.allocator.default};
4848
<#if withWriterCode>
4949

50+
<@compound_default_constructor compoundConstructorsData/>
51+
5052
<@compound_constructor_declaration compoundConstructorsData/>
5153
<#if fieldList?has_content>
5254

compiler/extensions/cpp/freemarker/Union.h.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public:
5151
};
5252
<#if withWriterCode>
5353

54+
<@compound_default_constructor compoundConstructorsData/>
55+
5456
<@compound_constructor_declaration compoundConstructorsData/>
5557
</#if>
5658

test/arguments/set_top_level_package/cpp/SetTopLevelPackageTest.cpp

+18-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,24 @@ const size_t SetTopLevelPackageTest::SIMPLE_STRUCTURE_BIT_SIZE = 32;
1919

2020
TEST_F(SetTopLevelPackageTest, emptyConstructor)
2121
{
22-
SimpleStructure simpleStructure;
23-
const size_t expectedBitSize = SIMPLE_STRUCTURE_BIT_SIZE;
24-
simpleStructure.initializeChildren();
25-
ASSERT_EQ(expectedBitSize, simpleStructure.bitSizeOf());
22+
{
23+
SimpleStructure simpleStructure;
24+
simpleStructure.initializeChildren();
25+
ASSERT_EQ(SIMPLE_STRUCTURE_BIT_SIZE, simpleStructure.bitSizeOf());
26+
}
27+
28+
{
29+
SimpleStructure simpleStructure = {};
30+
simpleStructure.initializeChildren();
31+
ASSERT_EQ(SIMPLE_STRUCTURE_BIT_SIZE, simpleStructure.bitSizeOf());
32+
}
33+
}
34+
35+
TEST_F(SetTopLevelPackageTest, fieldConstructor)
36+
{
37+
SimpleStructure simpleStructure2 = SimpleStructure({}, {}, {}, {}, {}, {});
38+
simpleStructure2.initializeChildren();
39+
ASSERT_EQ(SIMPLE_STRUCTURE_BIT_SIZE, simpleStructure2.bitSizeOf());
2640
}
2741

2842
} // namespace simple_structure

test/language/bitmask_types/cpp/BitfieldBitmaskTest.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,15 @@ const Permission::underlying_type BitfieldBitmaskTest::WRITE_VALUE = 4;
3333

3434
TEST_F(BitfieldBitmaskTest, emptyConstructor)
3535
{
36-
const Permission permission;
37-
ASSERT_EQ(0, permission.getValue());
36+
{
37+
const Permission permission;
38+
ASSERT_EQ(0, permission.getValue());
39+
}
40+
41+
{
42+
const Permission permission = {};
43+
ASSERT_EQ(0, permission.getValue());
44+
}
3845
}
3946

4047
TEST_F(BitfieldBitmaskTest, valuesConstructor)

test/language/bitmask_types/cpp/BitfieldConstBitmaskTest.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,15 @@ const Permission::underlying_type BitfieldConstBitmaskTest::WRITE_VALUE = 4;
3434

3535
TEST_F(BitfieldConstBitmaskTest, emptyConstructor)
3636
{
37-
const Permission permission;
38-
ASSERT_EQ(0, permission.getValue());
37+
{
38+
const Permission permission;
39+
ASSERT_EQ(0, permission.getValue());
40+
}
41+
42+
{
43+
const Permission permission = {};
44+
ASSERT_EQ(0, permission.getValue());
45+
}
3946
}
4047

4148
TEST_F(BitfieldConstBitmaskTest, valuesConstroctor)

test/language/bitmask_types/cpp/BitmaskDefinedByConstantTest.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,17 @@ const Permission::underlying_type BitmaskDefinedByConstantTest::WRITE_VALUE = 4;
3434

3535
TEST_F(BitmaskDefinedByConstantTest, emptyConstructor)
3636
{
37-
const Permission permission;
38-
ASSERT_EQ(0, permission.getValue());
37+
{
38+
const Permission permission;
39+
ASSERT_EQ(0, permission.getValue());
40+
}
41+
{
42+
const Permission permission = {};
43+
ASSERT_EQ(0, permission.getValue());
44+
}
3945
}
4046

41-
TEST_F(BitmaskDefinedByConstantTest, valuesConstroctor)
47+
TEST_F(BitmaskDefinedByConstantTest, valuesConstructor)
4248
{
4349
const Permission permission(Permission::Values::WRITE);
4450
ASSERT_EQ(WRITE_VALUE, permission.getValue());

test/language/bitmask_types/cpp/BitmaskUsedByBitmaskTest.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,14 @@ const Permission::underlying_type BitmaskUsedByBitmaskTest::WRITE_VALUE =
3636

3737
TEST_F(BitmaskUsedByBitmaskTest, emptyConstructor)
3838
{
39-
const Permission permission;
40-
ASSERT_EQ(0, permission.getValue());
39+
{
40+
const Permission permission;
41+
ASSERT_EQ(0, permission.getValue());
42+
}
43+
{
44+
const Permission permission = {};
45+
ASSERT_EQ(0, permission.getValue());
46+
}
4147
}
4248

4349
TEST_F(BitmaskUsedByBitmaskTest, valuesConstroctor)

test/language/bitmask_types/cpp/BitmaskWithoutNoneTest.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,14 @@ const Permission::underlying_type BitmaskWithoutNoneTest::WRITE_VALUE = 4;
3131

3232
TEST_F(BitmaskWithoutNoneTest, emptyConstructor)
3333
{
34-
const Permission permission;
35-
ASSERT_EQ(0, permission.getValue());
34+
{
35+
const Permission permission;
36+
ASSERT_EQ(0, permission.getValue());
37+
}
38+
{
39+
const Permission permission = {};
40+
ASSERT_EQ(0, permission.getValue());
41+
}
3642
}
3743

3844
TEST_F(BitmaskWithoutNoneTest, valuesConstroctor)

test/language/bitmask_types/cpp/UInt64BitmaskTest.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,14 @@ const Permission::underlying_type Uint64BitmaskTest::WRITE_PERMISSION_VALUE = 4;
3333

3434
TEST_F(Uint64BitmaskTest, emptyConstructor)
3535
{
36-
const Permission permission;
37-
ASSERT_EQ(0, permission.getValue());
36+
{
37+
const Permission permission;
38+
ASSERT_EQ(0, permission.getValue());
39+
}
40+
{
41+
const Permission permission = {};
42+
ASSERT_EQ(0, permission.getValue());
43+
}
3844
}
3945

4046
TEST_F(Uint64BitmaskTest, valuesConstroctor)

test/language/bitmask_types/cpp/UInt8BitmaskTest.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,14 @@ const Permission::underlying_type Uint8BitmaskTest::WRITE_VALUE = 4;
3333

3434
TEST_F(Uint8BitmaskTest, emptyConstructor)
3535
{
36-
const Permission permission;
37-
ASSERT_EQ(0, permission.getValue());
36+
{
37+
const Permission permission;
38+
ASSERT_EQ(0, permission.getValue());
39+
}
40+
{
41+
const Permission permission = {};
42+
ASSERT_EQ(0, permission.getValue());
43+
}
3844
}
3945

4046
TEST_F(Uint8BitmaskTest, valuesConstroctor)

test/language/bitmask_types/cpp/VarUIntBitmaskTest.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,14 @@ const Permission::underlying_type VarUIntBitmaskTest::WRITE_VALUE = 4;
3333

3434
TEST_F(VarUIntBitmaskTest, emptyConstructor)
3535
{
36-
const Permission permission;
37-
ASSERT_EQ(0, permission.getValue());
36+
{
37+
const Permission permission;
38+
ASSERT_EQ(0, permission.getValue());
39+
}
40+
{
41+
const Permission permission = {};
42+
ASSERT_EQ(0, permission.getValue());
43+
}
3844
}
3945

4046
TEST_F(VarUIntBitmaskTest, valuesConstroctor)

test/language/choice_types/cpp/BitmaskParamChoiceTest.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,14 @@ class BitmaskParamChoiceTest : public ::testing::Test
4040

4141
TEST_F(BitmaskParamChoiceTest, emptyConstructor)
4242
{
43-
BitmaskParamChoice bitmaskParamChoice;
44-
ASSERT_THROW(bitmaskParamChoice.getSelector(), zserio::CppRuntimeException);
43+
{
44+
BitmaskParamChoice bitmaskParamChoice;
45+
ASSERT_THROW(bitmaskParamChoice.getSelector(), zserio::CppRuntimeException);
46+
}
47+
{
48+
BitmaskParamChoice bitmaskParamChoice = {};
49+
ASSERT_THROW(bitmaskParamChoice.getSelector(), zserio::CppRuntimeException);
50+
}
4551
}
4652

4753
TEST_F(BitmaskParamChoiceTest, bitStreamReaderConstructor)

test/language/choice_types/cpp/BoolParamChoiceTest.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,14 @@ class BoolParamChoiceTest : public ::testing::Test
2626

2727
TEST_F(BoolParamChoiceTest, emptyConstructor)
2828
{
29-
BoolParamChoice boolParamChoice;
30-
ASSERT_THROW(boolParamChoice.getSelector(), zserio::CppRuntimeException);
29+
{
30+
BoolParamChoice boolParamChoice;
31+
ASSERT_THROW(boolParamChoice.getSelector(), zserio::CppRuntimeException);
32+
}
33+
{
34+
BoolParamChoice boolParamChoice = {};
35+
ASSERT_THROW(boolParamChoice.getSelector(), zserio::CppRuntimeException);
36+
}
3137
}
3238

3339
TEST_F(BoolParamChoiceTest, bitStreamReaderConstructor)

test/language/choice_types/cpp/EmptyChoiceTest.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@ namespace empty_choice
1212

1313
TEST(EmptyChoiceTest, emptyConstructor)
1414
{
15-
EmptyChoice emptyChoice;
16-
ASSERT_THROW(emptyChoice.getSelector(), zserio::CppRuntimeException);
15+
{
16+
EmptyChoice emptyChoice;
17+
ASSERT_THROW(emptyChoice.getSelector(), zserio::CppRuntimeException);
18+
}
19+
{
20+
EmptyChoice emptyChoice = {};
21+
ASSERT_THROW(emptyChoice.getSelector(), zserio::CppRuntimeException);
22+
}
1723
}
1824

1925
TEST(EmptyChoiceTest, bitStreamReaderConstructor)

test/language/choice_types/cpp/EmptyChoiceWithCaseTest.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,14 @@ namespace empty_choice_with_case
1313

1414
TEST(EmptyChoiceWithCaseTest, emptyConstructor)
1515
{
16-
EmptyChoiceWithCase emptyChoiceWithCase;
17-
ASSERT_THROW(emptyChoiceWithCase.getSelector(), zserio::CppRuntimeException);
16+
{
17+
EmptyChoiceWithCase emptyChoiceWithCase;
18+
ASSERT_THROW(emptyChoiceWithCase.getSelector(), zserio::CppRuntimeException);
19+
}
20+
{
21+
EmptyChoiceWithCase emptyChoiceWithCase = {};
22+
ASSERT_THROW(emptyChoiceWithCase.getSelector(), zserio::CppRuntimeException);
23+
}
1824
}
1925

2026
TEST(EmptyChoiceWithCaseTest, bitStreamReaderConstructor)

test/language/choice_types/cpp/EmptyChoiceWithDefaultTest.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,14 @@ namespace empty_choice_with_default
1313

1414
TEST(EmptyChoiceWithDefaultTest, emptyConstructor)
1515
{
16-
EmptyChoiceWithDefault emptyChoiceWithDefault;
17-
ASSERT_THROW(emptyChoiceWithDefault.getSelector(), zserio::CppRuntimeException);
16+
{
17+
EmptyChoiceWithDefault emptyChoiceWithDefault;
18+
ASSERT_THROW(emptyChoiceWithDefault.getSelector(), zserio::CppRuntimeException);
19+
}
20+
{
21+
EmptyChoiceWithDefault emptyChoiceWithDefault = {};
22+
ASSERT_THROW(emptyChoiceWithDefault.getSelector(), zserio::CppRuntimeException);
23+
}
1824
}
1925

2026
TEST(EmptyChoiceWithDefaultTest, bitStreamReaderConstructor)

test/language/choice_types/cpp/EnumParamChoiceTest.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,14 @@ class EnumParamChoiceTest : public ::testing::Test
4040

4141
TEST_F(EnumParamChoiceTest, emptyConstructor)
4242
{
43-
EnumParamChoice enumParamChoice;
44-
ASSERT_THROW(enumParamChoice.getSelector(), zserio::CppRuntimeException);
43+
{
44+
EnumParamChoice enumParamChoice;
45+
ASSERT_THROW(enumParamChoice.getSelector(), zserio::CppRuntimeException);
46+
}
47+
{
48+
EnumParamChoice enumParamChoice = {};
49+
ASSERT_THROW(enumParamChoice.getSelector(), zserio::CppRuntimeException);
50+
}
4551
}
4652

4753
TEST_F(EnumParamChoiceTest, bitStreamReaderConstructor)

test/language/choice_types/cpp/FullBitmaskParamChoiceTest.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,15 @@ class FullBitmaskParamChoiceTest : public ::testing::Test
4141

4242
TEST_F(FullBitmaskParamChoiceTest, emptyConstructor)
4343
{
44-
FullBitmaskParamChoice fullBitmaskParamChoice;
45-
ASSERT_THROW(fullBitmaskParamChoice.getSelector(), zserio::CppRuntimeException);
44+
{
45+
FullBitmaskParamChoice fullBitmaskParamChoice;
46+
ASSERT_THROW(fullBitmaskParamChoice.getSelector(), zserio::CppRuntimeException);
47+
}
48+
49+
{
50+
FullBitmaskParamChoice fullBitmaskParamChoice = {};
51+
ASSERT_THROW(fullBitmaskParamChoice.getSelector(), zserio::CppRuntimeException);
52+
}
4653
}
4754

4855
TEST_F(FullBitmaskParamChoiceTest, bitStreamReaderConstructor)

test/language/choice_types/cpp/FullEnumParamChoiceTest.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,14 @@ class FullEnumParamChoiceTest : public ::testing::Test
4040

4141
TEST_F(FullEnumParamChoiceTest, emptyConstructor)
4242
{
43-
FullEnumParamChoice fullEnumParamChoice;
44-
ASSERT_THROW(fullEnumParamChoice.getSelector(), zserio::CppRuntimeException);
43+
{
44+
FullEnumParamChoice fullEnumParamChoice;
45+
ASSERT_THROW(fullEnumParamChoice.getSelector(), zserio::CppRuntimeException);
46+
}
47+
{
48+
FullEnumParamChoice fullEnumParamChoice = {};
49+
ASSERT_THROW(fullEnumParamChoice.getSelector(), zserio::CppRuntimeException);
50+
}
4551
}
4652

4753
TEST_F(FullEnumParamChoiceTest, bitStreamReaderConstructor)

test/language/choice_types/cpp/UInt16ParamChoiceTest.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,14 @@ const uint16_t UInt16ParamChoiceTest::VARIANT_C_SELECTOR = 7;
5858

5959
TEST_F(UInt16ParamChoiceTest, emptyConstructor)
6060
{
61-
UInt16ParamChoice uint16ParamChoice;
62-
ASSERT_THROW(uint16ParamChoice.getSelector(), zserio::CppRuntimeException);
61+
{
62+
UInt16ParamChoice uint16ParamChoice;
63+
ASSERT_THROW(uint16ParamChoice.getSelector(), zserio::CppRuntimeException);
64+
}
65+
{
66+
UInt16ParamChoice uint16ParamChoice = {};
67+
ASSERT_THROW(uint16ParamChoice.getSelector(), zserio::CppRuntimeException);
68+
}
6369
}
6470

6571
TEST_F(UInt16ParamChoiceTest, bitStreamReaderConstructor)

0 commit comments

Comments
 (0)