RTI Connext Traditional C++ API Version 7.2.0
BuilderHelper.hpp
1/*
2(c) Copyright, Real-Time Innovations, 2018.
3All rights reserved.
4
5No duplications, whole or partial, manual or electronic, may be made
6without express written permission. Any such copies, or
7revisions thereof, must display this notice unaltered.
8This 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
14namespace rti { namespace flat {
15
16template <typename T>
17class PrimitiveSequenceBuilder;
18
19template <typename ElementBuilder>
20class MutableSequenceBuilder;
21
22template <typename ElementOffset>
23class FinalSequenceBuilder;
24
25template <typename ElementBuilder, unsigned int N>
26class MutableArrayBuilder;
27
28class StringBuilder;
29
30namespace detail {
31
32//
33// lc_code:
34//
35
36template <size_t PrimitiveSize>
37struct primitive_lc_code_helper;
38
39template <>
40struct 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
47template <>
48struct 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
55template <>
56struct 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
63template <>
64struct 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
71template <typename T>
72struct 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.
79template <typename T>
80struct lc_code {
81 enum {
82 value = 5 // LC = 5 => member length is provided in NEXTINT
83 };
84};
85
86template <typename T>
87struct lc_code<MutableSequenceBuilder<T> > {
88 enum {
89 value = 4
90 };
91};
92
93template <typename T>
94struct lc_code<FinalSequenceBuilder<T> > {
95 enum {
96 value = 4
97 };
98};
99
100template <typename T, unsigned int N>
101struct lc_code<MutableArrayBuilder<T, N> > {
102 enum {
103 value = 4
104 };
105};
106
107// Primitive sequences may use an optimized LC code
108template <typename T>
109struct lc_code<PrimitiveSequenceBuilder<T> > {
110 enum { value = primitive_lc_code_helper<sizeof(T)>::sequence };
111};
112
113template <>
114struct lc_code<StringBuilder> : lc_code<PrimitiveSequenceBuilder<char> > {};
115
116//
117// Initialization of final types
118//
119
120template <typename Offset>
121struct 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++)
135template <typename T, typename Enable = void>
136struct default_primitive_value {
137 static T get()
138 {
139 return (T) 0;
140 }
141};
142
143template <typename T, unsigned int N>
144struct 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
160template <typename T, unsigned int N>
161struct 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
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:730
ElementOffset get_element(unsigned int i)
Gets the Offset to an element.
Definition: SequenceOffsets.hpp:768
const unsigned char * get_buffer() const
Gets this member's position in the buffer.
Definition: Offset.hpp:554
offset_t get_buffer_size() const
Gets the size, in bytes, of this member in the buffer.
Definition: Offset.hpp:568
Offset to an array of primitive elements.
Definition: SequenceOffsets.hpp:220
unsigned int element_count() const
Returns the number of elements, N.
Definition: SequenceOffsets.hpp:242
The generic definition of FlatData topic-types.
Definition: FlatSample.hpp:148
The RTI namespace.
Definition: AggregationBuilders.hpp:17