RTI Connext Traditional C++ API  Version 7.0.0
BuilderHelper.hpp
1 /*
2 (c) Copyright, Real-Time Innovations, 2018.
3 All rights reserved.
4 
5 No duplications, whole or partial, manual or electronic, may be made
6 without express written permission. Any such copies, or
7 revisions thereof, must display this notice unaltered.
8 This code contains trade secrets of Real-Time Innovations, Inc.
9 */
10 
11 #ifndef RTI_DDS_FLAT_BUILDERHELPER_HPP_
12 #define RTI_DDS_FLAT_BUILDERHELPER_HPP_
13 
14 namespace rti { namespace flat {
15 
16 template <typename T>
18 
19 template <typename ElementBuilder>
21 
22 template <typename ElementOffset>
24 
25 template <typename ElementBuilder, unsigned int N>
27 
28 class StringBuilder;
29 
30 namespace detail {
31 
32 //
33 // lc_code:
34 //
35 
36 template <size_t PrimitiveSize>
37 struct primitive_lc_code_helper;
38 
39 template <>
40 struct primitive_lc_code_helper<1> {
41  enum {
42  single = 0, // LC = 1 => member length is 1
43  sequence = 5 // LC = 5 => member length is also NEXTINT
44  };
45 };
46 
47 template <>
48 struct primitive_lc_code_helper<2> {
49  enum {
50  single = 1, // LC = 1 => member length is 2
51  sequence = 4 // LC = 4 => member length is provided as an additional int
52  };
53 };
54 
55 template <>
56 struct primitive_lc_code_helper<4> {
57  enum {
58  single = 2, // LC = 2 => member length is 4
59  sequence = 6 // LC = 6 => member length is 4 * NEXTINT
60  };
61 };
62 
63 template <>
64 struct primitive_lc_code_helper<8> {
65  enum {
66  single = 3, // LC = 3 => member length is 8
67  sequence = 7 // LC = 7 => member length is 8 * NEXTINT
68  };
69 };
70 
71 template <typename T>
72 struct primitive_lc_code {
73  enum { value = primitive_lc_code_helper<sizeof(T)>::single };
74 };
75 
76 // In general, use LC = 5, for mutable aggregation types. Final aggregation
77 // types don't use this 'lc_code' trait type. The other cases are specialized
78 // after this.
79 template <typename T>
80 struct lc_code {
81  enum {
82  value = 5 // LC = 5 => member length is provided in NEXTINT
83  };
84 };
85 
86 template <typename T>
87 struct lc_code<MutableSequenceBuilder<T> > {
88  enum {
89  value = 4
90  };
91 };
92 
93 template <typename T>
94 struct lc_code<FinalSequenceBuilder<T> > {
95  enum {
96  value = 4
97  };
98 };
99 
100 template <typename T, unsigned int N>
101 struct lc_code<MutableArrayBuilder<T, N> > {
102  enum {
103  value = 4
104  };
105 };
106 
107 // Primitive sequences may use an optimized LC code
108 template <typename T>
109 struct lc_code<PrimitiveSequenceBuilder<T> > {
110  enum { value = primitive_lc_code_helper<sizeof(T)>::sequence };
111 };
112 
113 template <>
114 struct lc_code<StringBuilder> : lc_code<PrimitiveSequenceBuilder<char> > {};
115 
116 //
117 // Initialization of final types
118 //
119 
120 template <typename Offset>
121 struct final_offset_initializer {
122 
123  static bool initialize(Offset& offset)
124  {
125  return RTI_XCDR_TRUE == RTIXCdrFlatData_initializeSample(
126  (char *) offset.get_buffer(),
127  offset.get_buffer_size(),
128  rti::xcdr::type_programs<rti::flat::Sample<Offset> >::get());
129  }
130 
131 };
132 
133 // This will be specialized for enums in generated code (traditional C++)
134 // and for enum classes in FlatDataTraits.hpp (modern C++)
135 template <typename T, typename Enable = void>
136 struct default_primitive_value {
137  static T get()
138  {
139  return (T) 0;
140  }
141 };
142 
143 template <typename T, unsigned int N>
144 struct final_offset_initializer<rti::flat::PrimitiveArrayOffset<T, N> > {
145 
146  static bool initialize(rti::flat::PrimitiveArrayOffset<T, N>& array)
147  {
148  T default_value = default_primitive_value<T>::get();
149  if (default_value == (T) 0) { // don't need to deal with endianness
150  memset(array.get_buffer(), 0, array.get_buffer_size());
151  } else {
152  for (unsigned int i = 0; i < array.element_count(); i++) {
153  array.set_element(i, default_value);
154  }
155  }
156  return true;
157  }
158 };
159 
160 template <typename T, unsigned int N>
161 struct final_offset_initializer<rti::flat::FinalAlignedArrayOffset<T, N> > {
162 
163  static bool initialize(rti::flat::FinalAlignedArrayOffset<T, N>& array)
164  {
165  for (unsigned int i = 0; i < N; i++) {
166  T offset = array.get_element(i);
167  if (offset.is_null()) {
168  return false;
169  }
170 
171  if (!final_offset_initializer<T>::initialize(offset)) {
172  return false;
173  }
174  }
175 
176  return true;
177  }
178 
179 };
180 
181 } } }
182 
183 #endif // RTI_DDS_FLAT_BUILDERHELPER_HPP_
184 
unsigned int element_count() const
Returns the number of elements, N.
Definition: SequenceOffsets.hpp:242
const unsigned char * get_buffer() const
Gets this member&#39;s position in the buffer.
Definition: Offset.hpp:554
Offset to an array of primitive elements.
Definition: SequenceOffsets.hpp:220
Offset to an array of variable-size elements.
Definition: SequenceOffsets.hpp:580
ElementOffset get_element(unsigned int i)
Gets the Offset to an element.
Definition: SequenceOffsets.hpp:766
bool set_element(unsigned int i, T value)
Sets an element by index.
Definition: SequenceOffsets.hpp:111
Offset to an array of final elements.
Definition: SequenceOffsets.hpp:728
Builds a sequence member of variable-size elements.
Definition: BuilderHelper.hpp:20
offset_t get_buffer_size() const
Gets the size, in bytes, of this member in the buffer.
Definition: Offset.hpp:568
Builds an array member of variable-size elements.
Definition: BuilderHelper.hpp:26
Definition: AggregationBuilders.hpp:17
Builds a sequence member of fixed-size elements.
Definition: BuilderHelper.hpp:23
Builds a sequence of primitive members.
Definition: BuilderHelper.hpp:17