Skip to content

Commit 589bce0

Browse files
reicheratworkeboasson
authored andcommitted
Simplified entity_properties usage
Enum write functions no longer take entity_properties, the bit_bound is now taken from a constexpr trait for the enum Entity properties trees are now generated the same for primitives/ enums/constructed types Fixed bug in xcdr1 serialization of sequences of primitives, which did had an emheader which might not be able to contain its length Signed-off-by: Martijn Reicher <[email protected]>
1 parent e319d18 commit 589bce0

File tree

7 files changed

+135
-192
lines changed

7 files changed

+135
-192
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class OMG_DDS_API basic_cdr_stream : public cdr_stream {
9595
* @param[in] N The number of entities to read.
9696
*/
9797
template<typename T, std::enable_if_t<std::is_enum<T>::value && !std::is_arithmetic<T>::value, bool> = true >
98-
bool read(basic_cdr_stream& str, T& toread, const entity_properties_t &, size_t N = 1) {
98+
bool read(basic_cdr_stream& str, T& toread, size_t N = 1) {
9999
return read_enum_impl<basic_cdr_stream,T,uint32_t>(str, toread, N);
100100
}
101101

@@ -108,7 +108,7 @@ bool read(basic_cdr_stream& str, T& toread, const entity_properties_t &, size_t
108108
* @param[in] N The number of entities to write.
109109
*/
110110
template<typename T, std::enable_if_t<std::is_enum<T>::value && !std::is_arithmetic<T>::value, bool> = true >
111-
bool write(basic_cdr_stream& str, const T& towrite, const entity_properties_t &, size_t N = 1) {
111+
bool write(basic_cdr_stream& str, const T& towrite, size_t N = 1) {
112112
return write_enum_impl<basic_cdr_stream,T,uint32_t>(str, towrite, N);
113113
}
114114

@@ -120,7 +120,7 @@ bool write(basic_cdr_stream& str, const T& towrite, const entity_properties_t &,
120120
* @param[in] N The number of entities to move.
121121
*/
122122
template<typename T, std::enable_if_t<std::is_enum<T>::value && !std::is_arithmetic<T>::value, bool> = true >
123-
bool move(basic_cdr_stream& str, const T&, const entity_properties_t &, size_t N = 1) {
123+
bool move(basic_cdr_stream& str, const T&, size_t N = 1) {
124124
return move(str, uint32_t(0), N);
125125
}
126126

@@ -132,7 +132,7 @@ bool move(basic_cdr_stream& str, const T&, const entity_properties_t &, size_t N
132132
* @param[in] N The number of entities at most to move.
133133
*/
134134
template<typename T, std::enable_if_t<std::is_enum<T>::value && !std::is_arithmetic<T>::value, bool> = true >
135-
bool max(basic_cdr_stream& str, const T&, const entity_properties_t &, size_t N = 1) {
135+
bool max(basic_cdr_stream& str, const T&, size_t N = 1) {
136136
return max(str, uint32_t(0), N);
137137
}
138138

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

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <list>
1818
#include <map>
1919
#include <mutex>
20+
#include <type_traits>
2021
#include "cdr_enums.hpp"
2122

2223
namespace org {
@@ -25,6 +26,8 @@ namespace cyclonedds {
2526
namespace core {
2627
namespace cdr {
2728

29+
#define decl_ref_type(x) std::remove_cv_t<std::remove_reference_t<decltype(x)>>
30+
2831
/**
2932
* @brief
3033
* Bit bound descriptors.
@@ -244,45 +247,39 @@ struct OMG_DDS_API final_entry: public entity_properties_t {
244247
* @brief
245248
* Type properties getter function for basic types.
246249
*
250+
* @return entity_properties_t "Tree" representing the type.
251+
*/
252+
template<typename T, std::enable_if_t<std::is_arithmetic<T>::value, bool> = true >
253+
entity_properties_t get_type_props() {
254+
entity_properties_t props;
255+
static_assert(sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4 || sizeof(T) == 8);
256+
props.e_bb = bit_bound(sizeof(T));
257+
return props;
258+
}
259+
260+
/**
261+
* @brief
262+
* Forward declaration for type properties getter function.
263+
*
247264
* This template function is replaced/implemented by the types implemented through IDL generation.
248265
* It generates a static container which is initialized the first time the function is called,
249266
* this is then returned.
250267
*
251268
* @return entity_properties_t "Tree" representing the type.
252269
*/
253-
template<typename T>
254-
entity_properties_t get_type_props() {
255-
static std::mutex mtx;
256-
static entity_properties_t props;
257-
static bool initialized = false;
258-
std::lock_guard<std::mutex> lock(mtx);
259-
260-
if (initialized)
261-
return props;
270+
template<typename T, std::enable_if_t<!std::is_arithmetic<T>::value, bool> = true >
271+
entity_properties_t get_type_props();
262272

263-
props.clear();
264-
key_endpoint keylist;
265-
switch (sizeof(T)) {
266-
case 1:
267-
props.e_bb = bb_8_bits;
268-
break;
269-
case 2:
270-
props.e_bb = bb_16_bits;
271-
break;
272-
case 4:
273-
props.e_bb = bb_32_bits;
274-
break;
275-
case 8:
276-
props.e_bb = bb_64_bits;
277-
break;
278-
}
279-
props.m_members_by_seq.push_back(final_entry());
280-
props.m_members_by_id.push_back(final_entry());
281-
props.m_keys.push_back(final_entry());
282-
props.finish(keylist);
283-
initialized = true;
284-
return props;
285-
}
273+
/**
274+
* @brief
275+
* Forward declaration for bit bound property of enum classes.
276+
*
277+
* This function is implementated by each enum class that is encountered.
278+
*
279+
* @return bit_bound The bit bound for the indicated enum.
280+
*/
281+
template<typename T, std::enable_if_t<std::is_enum<T>::value, bool> = true >
282+
constexpr bit_bound get_enum_bit_bound();
286283

287284
}
288285
}

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -224,15 +224,14 @@ class OMG_DDS_API xcdr_v1_stream : public cdr_stream {
224224
*
225225
* @param[in, out] str The stream which is read from.
226226
* @param[out] toread The variable to read into.
227-
* @param[in] props The properties of the variable.
228227
* @param[in] N The number of entities to read.
229228
*
230229
* @return Whether the operation was completed succesfully.
231230
*/
232231
template<typename T, std::enable_if_t<std::is_enum<T>::value && !std::is_arithmetic<T>::value, bool> = true >
233-
bool read(xcdr_v1_stream& str, T& toread, const entity_properties_t &props, size_t N = 1)
232+
bool read(xcdr_v1_stream& str, T& toread, size_t N = 1)
234233
{
235-
switch (str.is_key() ? bb_32_bits : props.e_bb)
234+
switch (str.is_key() ? bb_32_bits : get_enum_bit_bound<T>())
236235
{
237236
case bb_8_bits:
238237
return read_enum_impl<xcdr_v1_stream,T,uint8_t>(str, toread, N);
@@ -255,15 +254,14 @@ bool read(xcdr_v1_stream& str, T& toread, const entity_properties_t &props, size
255254
*
256255
* @param[in, out] str The stream which is written to.
257256
* @param[in] towrite The variable to write.
258-
* @param[in] props The properties of the variable.
259257
* @param[in] N The number of entities to write.
260258
*
261259
* @return Whether the operation was completed succesfully.
262260
*/
263261
template<typename T, std::enable_if_t<std::is_enum<T>::value && !std::is_arithmetic<T>::value, bool> = true >
264-
bool write(xcdr_v1_stream& str, const T& towrite, const entity_properties_t &props, size_t N = 1)
262+
bool write(xcdr_v1_stream& str, const T& towrite, size_t N = 1)
265263
{
266-
switch (str.is_key() ? bb_32_bits : props.e_bb)
264+
switch (str.is_key() ? bb_32_bits : get_enum_bit_bound<T>())
267265
{
268266
case bb_8_bits:
269267
return write_enum_impl<xcdr_v1_stream,T,uint8_t>(str, towrite, N);
@@ -285,15 +283,14 @@ bool write(xcdr_v1_stream& str, const T& towrite, const entity_properties_t &pro
285283
* Moves the cursor of the stream by the size the enum would take up.
286284
*
287285
* @param[in, out] str The stream whose cursor is moved.
288-
* @param[in] props The properties of the variable.
289286
* @param[in] N The number of entities to move.
290287
*
291288
* @return Whether the operation was completed succesfully.
292289
*/
293290
template<typename T, std::enable_if_t<std::is_enum<T>::value && !std::is_arithmetic<T>::value, bool> = true >
294-
bool move(xcdr_v1_stream& str, const T&, const entity_properties_t &props, size_t N = 1)
291+
bool move(xcdr_v1_stream& str, const T&, size_t N = 1)
295292
{
296-
switch (str.is_key() ? bb_32_bits : props.e_bb)
293+
switch (str.is_key() ? bb_32_bits : get_enum_bit_bound<T>())
297294
{
298295
case bb_8_bits:
299296
return move(str, int8_t(0), N);
@@ -316,15 +313,14 @@ bool move(xcdr_v1_stream& str, const T&, const entity_properties_t &props, size_
316313
*
317314
* @param[in, out] str The stream whose cursor is moved.
318315
* @param[in] max_sz The variable to move the cursor by, no contents of this variable are used, it is just used to determine the template.
319-
* @param[in] props The properties of the variable.
320316
* @param[in] N The number of entities at most to move.
321317
*
322318
* @return Whether the operation was completed succesfully.
323319
*/
324320
template<typename T, std::enable_if_t<std::is_enum<T>::value && !std::is_arithmetic<T>::value, bool> = true >
325-
bool max(xcdr_v1_stream& str, const T& max_sz, const entity_properties_t &props, size_t N = 1)
321+
bool max(xcdr_v1_stream& str, const T& max_sz, size_t N = 1)
326322
{
327-
return move(str, max_sz, props, N);
323+
return move(str, max_sz, N);
328324
}
329325

330326
}

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -295,14 +295,13 @@ class OMG_DDS_API xcdr_v2_stream : public cdr_stream {
295295
*
296296
* @param[in, out] str The stream which is read from.
297297
* @param[out] toread The variable to read into.
298-
* @param[in] props The properties of the variable.
299298
* @param[in] N The number of entities to read.
300299
*
301300
* @return Whether the operation was completed succesfully.
302301
*/
303302
template<typename T, std::enable_if_t<std::is_enum<T>::value && !std::is_arithmetic<T>::value, bool> = true >
304-
bool read(xcdr_v2_stream& str, T& toread, const entity_properties_t &props, size_t N = 1) {
305-
switch (str.is_key() ? bb_32_bits : props.e_bb)
303+
bool read(xcdr_v2_stream& str, T& toread, size_t N = 1) {
304+
switch (str.is_key() ? bb_32_bits : get_enum_bit_bound<T>())
306305
{
307306
case bb_8_bits:
308307
return read_enum_impl<xcdr_v2_stream,T,uint8_t>(str, toread, N);
@@ -325,14 +324,13 @@ bool read(xcdr_v2_stream& str, T& toread, const entity_properties_t &props, size
325324
*
326325
* @param [in, out] str The stream which is written to.
327326
* @param [in] towrite The variable to write.
328-
* @param[in] props The properties of the variable.
329327
* @param[in] N The number of entities to write.
330328
*
331329
* @return Whether the operation was completed succesfully.
332330
*/
333331
template<typename T, std::enable_if_t<std::is_enum<T>::value && !std::is_arithmetic<T>::value, bool> = true >
334-
bool write(xcdr_v2_stream& str, const T& towrite, const entity_properties_t &props, size_t N = 1) {
335-
switch (str.is_key() ? bb_32_bits : props.e_bb)
332+
bool write(xcdr_v2_stream& str, const T& towrite, size_t N = 1) {
333+
switch (str.is_key() ? bb_32_bits : get_enum_bit_bound<T>())
336334
{
337335
case bb_8_bits:
338336
return write_enum_impl<xcdr_v2_stream,T,uint8_t>(str, towrite, N);
@@ -354,14 +352,13 @@ bool write(xcdr_v2_stream& str, const T& towrite, const entity_properties_t &pro
354352
* Moves the cursor of the stream by the size the enum would take up.
355353
*
356354
* @param[in, out] str The stream whose cursor is moved.
357-
* @param[in] props The properties of the variable.
358355
* @param[in] N The number of entities to move.
359356
*
360357
* @return Whether the operation was completed succesfully.
361358
*/
362359
template<typename T, std::enable_if_t<std::is_enum<T>::value && !std::is_arithmetic<T>::value, bool> = true >
363-
bool move(xcdr_v2_stream& str, const T&, const entity_properties_t &props, size_t N = 1) {
364-
switch (str.is_key() ? bb_32_bits : props.e_bb)
360+
bool move(xcdr_v2_stream& str, const T&, size_t N = 1) {
361+
switch (str.is_key() ? bb_32_bits : get_enum_bit_bound<T>())
365362
{
366363
case bb_8_bits:
367364
return move(str, int8_t(0), N);
@@ -384,14 +381,13 @@ bool move(xcdr_v2_stream& str, const T&, const entity_properties_t &props, size_
384381
*
385382
* @param[in, out] str The stream whose cursor is moved.
386383
* @param[in] max_sz The variable to move the cursor by, no contents of this variable are used, it is just used to determine the template.
387-
* @param[in] props The properties of the variable.
388384
* @param[in] N The number of entities at most to move.
389385
*
390386
* @return Whether the operation was completed succesfully.
391387
*/
392388
template<typename T, std::enable_if_t<std::is_enum<T>::value && !std::is_arithmetic<T>::value, bool> = true >
393-
bool max(xcdr_v2_stream& str, const T& max_sz, const entity_properties_t &props, size_t N = 1) {
394-
return move(str, max_sz, props, N);
389+
bool max(xcdr_v2_stream& str, const T& max_sz, size_t N = 1) {
390+
return move(str, max_sz, N);
395391
}
396392

397393
}

src/ddscxx/tests/CDRStreamer.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,12 @@ TEST_F(CDRStreamer, cdr_sequence)
353353
};
354354

355355
bytes SSM_xcdr_v1_normal {
356-
0x40, 0x00, 0x00, 0x07 /*sequence_struct.c.mheader*/,
356+
0x7F, 0x01, 0x00, 0x08 /*sequence_struct.c.mheader (pid_list_extended + length = 8)*/,
357+
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07 /*sequence_struct.c.mheader (extended)*/,
357358
0x00, 0x00, 0x00, 0x03/*sequence_struct.c.length*/, 'z', 'y', 'x'/*sequence_struct.c.data*/,
358359
0x00 /*padding bytes (1)*/,
359-
0x00, 0x01, 0x00, 0x14 /*sequence_struct.c.mheader (extended)*/,
360+
0x7F, 0x01, 0x00, 0x08 /*sequence_struct.l.mheader (pid_list_extended + length = 8)*/,
361+
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x14 /*sequence_struct.l.mheader (extended)*/,
360362
0x00, 0x00, 0x00, 0x04/*sequence_struct.l.length*/, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01/*sequence_struct.l.data*/,
361363
0x7F, 0x02, 0x00, 0x00 /*inner list termination header*/
362364
};

src/ddscxx/tests/data/RegressionModels.idl

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,27 @@ union duplicate_types_union switch(long) {
7070
td_d d_2; //should be the same as d_1;
7171
};
7272

73+
@bit_bound(8) @appendable enum e1 {
74+
e_0, e_1, e_2, e_3
75+
};
76+
77+
typedef e1 e1_arr[3];
78+
79+
struct s_e1 {
80+
e1_arr c;
81+
};
82+
7383
union W switch(char) {
7484
case 'a':
7585
char c[2];
7686
case 'b':
7787
char d[2][3];
88+
case 'x':
89+
e1 x[3];
90+
case 'y':
91+
e1_arr y;
92+
case 'z':
93+
e1 z;
7894
};
7995

8096
typedef long long_array_5[5];
@@ -119,14 +135,4 @@ struct s_bm1 {
119135
bm1 c[3];
120136
};
121137

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-
132138
};

0 commit comments

Comments
 (0)