Skip to content

Commit e319d18

Browse files
reicheratworkeboasson
authored andcommitted
Add generation of entity_properties type for enums
The absence of this function was causing problems with subroutines where the generic version of this function was called, returning the incorrect bit bound for the enums Add unittests to check for generated properties and header insertion Signed-off-by: Martijn Reicher <[email protected]>
1 parent a5aaa39 commit e319d18

File tree

4 files changed

+83
-29
lines changed

4 files changed

+83
-29
lines changed

src/ddscxx/include/org/eclipse/cyclonedds/core/cdr/entity_properties.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,11 @@ entity_properties_t get_type_props() {
255255
static std::mutex mtx;
256256
static entity_properties_t props;
257257
static bool initialized = false;
258+
std::lock_guard<std::mutex> lock(mtx);
258259

259260
if (initialized)
260261
return props;
261262

262-
std::lock_guard<std::mutex> lock(mtx);
263263
props.clear();
264264
key_endpoint keylist;
265265
switch (sizeof(T)) {

src/ddscxx/tests/Regression.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,35 @@ TEST_F(Regression, delimiters_bitmask)
224224
readwrite_test(s, s_bm1_bytes, xcdr_v2_stream(endianness::little_endian));
225225
}
226226

227+
TEST_F(Regression, emumerators_properties)
228+
{
229+
auto props = org::eclipse::cyclonedds::core::cdr::get_type_props<e1>();
230+
231+
EXPECT_EQ(props.e_ext, extensibility::ext_appendable);
232+
EXPECT_EQ(props.e_bb, bb_8_bits);
233+
EXPECT_TRUE(props.xtypes_necessary);
234+
ASSERT_EQ(props.m_members_by_seq.size(), 1);
235+
EXPECT_FALSE(bool(props.m_members_by_seq.front()));
236+
ASSERT_EQ(props.m_members_by_id.size(), 1);
237+
EXPECT_FALSE(bool(props.m_members_by_id.front()));
238+
ASSERT_EQ(props.m_keys.size(), 1);
239+
EXPECT_FALSE(bool(props.m_keys.front()));
240+
}
241+
242+
TEST_F(Regression, delimiters_emumerators)
243+
{
244+
bytes s_e1_bytes =
245+
{
246+
0x03, 0x00, 0x00, 0x00, //s_e1.dheader(3)
247+
0x03, 0x01, 0x02, //s_e1.c
248+
};
249+
250+
s_e1 s;
251+
s.c({e1::e_3, e1::e_1, e1::e_2});
252+
253+
readwrite_test(s, s_e1_bytes, xcdr_v2_stream(endianness::little_endian));
254+
}
255+
227256
TEST_F(Regression, arrays_in_union_case)
228257
{
229258
W w_a, w_b;

src/ddscxx/tests/data/RegressionModels.idl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,14 @@ struct s_bm1 {
119119
bm1 c[3];
120120
};
121121

122+
@bit_bound(8) @appendable enum e1 {
123+
e_0, e_1, e_2, e_3
124+
};
125+
126+
typedef e1 e1_arr[3];
127+
128+
struct s_e1 {
129+
e1_arr c;
130+
};
131+
122132
};

src/idlcxx/src/streamers.c

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,8 @@ generate_entity_properties(
682682

683683
const char *opt = is_optional(decl) ? "true" : "false";
684684
if (idl_is_struct(type_spec)
685-
|| idl_is_union(type_spec)) {
685+
|| idl_is_union(type_spec)
686+
|| idl_is_enum(type_spec)) {
686687
char *type = NULL;
687688
if (IDL_PRINTA(&type, get_cpp11_fully_scoped_name, type_spec, streams->generator) < 0)
688689
return IDL_RETCODE_NO_MEMORY;
@@ -762,20 +763,6 @@ generate_entity_properties(
762763
IDL_ANY:
763764
IDL_LDOUBLE:*/
764765
}
765-
} else if (idl_is_enum(type_spec)) {
766-
const idl_enum_t *en = type_spec;
767-
bb = "bb_32_bits";
768-
if (en->bit_bound.annotation) {
769-
if (en->bit_bound.value > 32) {
770-
bb = "bb_64_bits";
771-
} else if (en->bit_bound.value > 16) {
772-
bb = "bb_32_bits";
773-
} else if (en->bit_bound.value > 8) {
774-
bb = "bb_16_bits";
775-
} else {
776-
bb = "bb_8_bits";
777-
}
778-
}
779766
} else if (idl_is_bitmask(type_spec)) {
780767
const idl_bitmask_t *bm = type_spec;
781768
bb = "bb_32_bits";
@@ -1102,10 +1089,10 @@ print_constructed_type_open(struct streams *streams, const idl_node_t *node)
11021089
" {\n"
11031090
" static std::mutex mtx;\n"
11041091
" static entity_properties_t props;\n"
1105-
" static bool initialized = false;\n\n"
1092+
" static bool initialized = false;\n"
1093+
" std::lock_guard<std::mutex> lock(mtx);\n\n"
11061094
" if (initialized)\n"
11071095
" return props;\n\n"
1108-
" std::lock_guard<std::mutex> lock(mtx);\n"
11091096
" props.clear();\n"
11101097
" key_endpoint keylist;\n\n";
11111098
static const char *sfmt =
@@ -1489,11 +1476,21 @@ process_enum(
14891476
if (IDL_PRINTA(&fullname, get_cpp11_fully_scoped_name, _enum, gen) < 0)
14901477
return IDL_RETCODE_NO_MEMORY;
14911478

1492-
static const char *fmt = "template<>\n"\
1493-
"%s enum_conversion<%s>(uint32_t in)%s";
1494-
1495-
if (putf(&str->props, fmt, fullname, fullname, " {\n switch (in) {\n")
1496-
|| idl_fprintf(gen->header.handle, fmt, fullname, fullname, ";\n\n") < 0)
1479+
static const char *conv_func = "template<>\n"\
1480+
"%s enum_conversion<%s>(uint32_t in)%s",
1481+
*prop_func = "template<>\n"\
1482+
"entity_properties_t get_type_props<%s>()%s",
1483+
*prop_func_contents = " {\n"
1484+
"entity_properties_t props;\n",
1485+
*prop_func_close =
1486+
" props.m_members_by_seq.push_back(final_entry());\n"
1487+
" props.m_members_by_id.push_back(final_entry());\n"
1488+
" props.m_keys.push_back(final_entry());\n"
1489+
" return props;\n"
1490+
"}\n\n";
1491+
1492+
if (putf(&str->props, conv_func, fullname, fullname, " {\n switch (in) {\n")
1493+
|| idl_fprintf(gen->header.handle, conv_func, fullname, fullname, ";\n\n") < 0)
14971494
return IDL_RETCODE_NO_MEMORY;
14981495

14991496
/*count the number of enumerators*/
@@ -1533,16 +1530,34 @@ process_enum(
15331530
}
15341531
}
15351532

1533+
//generate entity properties function for enums
1534+
uint16_t bb = 32;
1535+
if (_enum->bit_bound.annotation) {
1536+
if (_enum->bit_bound.value > 32)
1537+
bb = 64;
1538+
else if (_enum->bit_bound.value > 16)
1539+
bb = 32;
1540+
else if (_enum->bit_bound.value > 8)
1541+
bb = 16;
1542+
else
1543+
bb = 8;
1544+
}
1545+
1546+
if (putf(&str->props," }\n}\n\n") ||
1547+
putf(&str->props, prop_func, fullname, prop_func_contents) ||
1548+
idl_fprintf(gen->header.handle, prop_func, fullname, ";\n\n") < 0 ||
1549+
(_enum->extensibility.value != IDL_FINAL &&
1550+
putf(&str->props, " props.e_ext = extensibility::%s;\n props.xtypes_necessary = true;\n", _enum->extensibility.value == IDL_APPENDABLE ? "ext_appendable" : "ext_mutable")) ||
1551+
putf(&str->props, " props.e_bb = bb_%"PRIu16"_bits;\n", bb) ||
1552+
putf(&str->props, prop_func_close)) {
1553+
ret = IDL_RETCODE_NO_MEMORY;
1554+
}
1555+
15361556
/*cleanup*/
15371557
if (already_encountered)
15381558
free(already_encountered);
1539-
if (ret)
1540-
return ret;
15411559

1542-
if (putf(&str->props," }\n}\n\n"))
1543-
return IDL_RETCODE_NO_MEMORY;
1544-
1545-
return IDL_RETCODE_OK;
1560+
return ret;
15461561
}
15471562

15481563
idl_retcode_t

0 commit comments

Comments
 (0)