RTI Connext Traditional C++ API  Version 6.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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>
17 class PrimitiveSequenceBuilder;
18 
19 template <typename ElementBuilder>
20 class MutableSequenceBuilder;
21 
22 template <typename ElementOffset>
23 class FinalSequenceBuilder;
24 
25 template <typename ElementBuilder, unsigned int N>
26 class MutableArrayBuilder;
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
134 template <typename T>
135 struct default_primitive_value {
136  static T get()
137  {
138  return (T) 0;
139  }
140 };
141 
142 template <typename T, unsigned int N>
143 struct final_offset_initializer<rti::flat::PrimitiveArrayOffset<T, N> > {
144 
145  static bool initialize(rti::flat::PrimitiveArrayOffset<T, N>& array)
146  {
147  T default_value = default_primitive_value<T>::get();
148  if (default_value == (T) 0) { // don't need to deal with endianness
149  memset(array.get_buffer(), 0, array.get_buffer_size());
150  } else {
151  for (unsigned int i = 0; i < array.element_count(); i++) {
152  array.set_element(i, default_value);
153  }
154  }
155  return true;
156  }
157 };
158 
159 template <typename T, unsigned int N>
160 struct final_offset_initializer<rti::flat::FinalAlignedArrayOffset<T, N> > {
161 
162  static bool initialize(rti::flat::FinalAlignedArrayOffset<T, N>& array)
163  {
164  for (unsigned int i = 0; i < N; i++) {
165  T offset = array.get_element(i);
166  if (offset.is_null()) {
167  return false;
168  }
169 
170  if (!final_offset_initializer<T>::initialize(offset)) {
171  return false;
172  }
173  }
174 
175  return true;
176  }
177 
178 };
179 
180 } } }
181 
182 #endif // RTI_DDS_FLAT_BUILDERHELPER_HPP_
183 

RTI Connext Traditional C++ API Version 6.0.1 Copyright © Sat Nov 23 2019 Real-Time Innovations, Inc