RTI Connext Traditional C++ API Version 7.3.0
Interpreter.hpp
1/* $Id$
2
3(c) Copyright, Real-Time Innovations, 2018.
4All rights reserved.
5
6No duplications, whole or partial, manual or electronic, may be made
7without express written permission. Any such copies, or
8revisions thereof, must display this notice unaltered.
9This code contains trade secrets of Real-Time Innovations, Inc.
10
11============================================================================= */
12
13#ifndef RTI_XCDR_INTERPRETER_HPP_
14#define RTI_XCDR_INTERPRETER_HPP_
15
16#include "xcdr/xcdr_interpreter.h"
17
18
19namespace rti { namespace xcdr {
20
21/*i
22 * Provides the type code of a type.
23 *
24 * This struct is to be specialized in generated code for each TopicType.
25 * Specializations must define a static member function:
26 * RTIXCdrTypeCode * get()
27 */
28template <typename TopicType>
29struct type_code;
30
31namespace detail {
32
33template <typename TopicType, bool IsCollection>
34bool has_cpp_friendly_cdr_layout_impl()
35{
36 RTIXCdrUnsignedLongLong size;
37 RTIXCdrAlignment alignment;
38 RTIXCdrXTypesComplianceMask xTypesComplianceMask;
39
40 xTypesComplianceMask = RTIXCdrInterpreter_getGlobalXtypeComplianceMask();
41
42 return RTIXCdrTypeCode_hasCFriendlyCdrLayout(
43 type_code<TopicType>::get(),
44 &size,
45 &alignment,
46 /*
47 * The number of element is not important; it's only important to
48 * know if there's one or more than one
49 */
50 IsCollection ? 2 : 1,
51 RTI_XCDR_TRUE, /* v2Encapsulation */
52 RTIXCdrXTypesComplianceMask_isBitSet(
53 xTypesComplianceMask,
54 RTI_XCDR_XTYPES_DHEADER_IN_NON_PRIMITIVE_COLLECTIONS_BIT),
55 RTIXCdrXTypesComplianceMask_isBitSet(
56 xTypesComplianceMask,
57 RTI_XCDR_XTYPES_ENUM_AS_PRIMITIVE_IN_COLLECTIONS_BIT));
58}
59
60}
61
62template <typename TopicType>
63bool has_cpp_friendly_cdr_layout()
64{
65 static bool result =
66 detail::has_cpp_friendly_cdr_layout_impl<TopicType, false>();
67 return result;
68}
69
70template <typename TopicType>
71bool has_cpp_friendly_cdr_layout_collection()
72{
73 static bool result =
74 detail::has_cpp_friendly_cdr_layout_impl<TopicType, true>();
75 return result;
76}
77
78struct NoOpPropertyConfigurator {
79 static void configure(RTIXCdrInterpreterProgramsGenProperty&)
80 {
81 // In general (trad. and micro C++), no additional properties.
82 //
83 // dds_cpp.2.0 defines a custom Configurator
84 //
85 }
86};
87
88/*i
89 * @brief Programs singleton for the to/from cdr functions
90 *
91 * The combination of the ProgramSingleton template parameters builds a single
92 * instance for the given type, program kinds and options.
93 * This will typically be instantiated in IDL-generated code.
94 *
95 * @tparam TopicType The topic-type these programs operate with
96 * @tparam ProgramKind A mask of the different programs to be generated
97 * @tparam ResolveAlias Value for RTIXCdrInterpreterProgramsGenProperty::resolveAlias
98 * when creating the programs
99 * @tparam InlineStruct Value for RTIXCdrInterpreterProgramsGenProperty::inlineStruct
100 * when creating the programs
101 * @tparam OptimizeEnum Value for RTIXCdrInterpreterProgramsGenProperty::optimizeEnum
102 * when creating the programs
103 * @tparam KeysOnly (default false) Whether to generate programs only for key fields
104 * @tparam PropertyConfigurator (default no-op) A functor that configures
105 * additional RTIXCdrInterpreterProgramsGenProperty to generate the
106 * programs.
107 *
108 * get_instance() creates the singleton the first time it's called. The singleton
109 * is automatically deleted by the destructor of a static local variable.
110 */
111template <
112 typename TopicType,
113 int ProgramKind,
114 bool ResolveAlias,
115 bool InlineStruct,
116 bool OptimizeEnum,
117 bool KeysOnly = false,
118 typename PropertyConfigurator = NoOpPropertyConfigurator>
119struct ProgramsSingleton {
120private:
121 ProgramsSingleton()
122 {
123 RTIXCdrInterpreterProgramsGenProperty property =
124 RTIXCdrInterpreterProgramsGenProperty_INITIALIZER;
125
126 configure_property(property);
127
128 programs_ = RTIXCdrInterpreterPrograms_new(
129 type_code<TopicType>::get(),
130 &property,
131 ProgramKind);
132 }
133
134 ~ProgramsSingleton()
135 {
136 if (programs_ != NULL) {
137 RTIXCdrInterpreterPrograms_delete(programs_);
138 }
139 }
140
141 static void configure_property(
142 RTIXCdrInterpreterProgramsGenProperty& property)
143 {
144 property.generateWithAllFields =
145 KeysOnly ? RTI_XCDR_FALSE : RTI_XCDR_TRUE;
146 property.resolveAlias = ResolveAlias ? RTI_XCDR_TRUE : RTI_XCDR_FALSE;
147 property.inlineStruct = InlineStruct ? RTI_XCDR_TRUE : RTI_XCDR_FALSE;
148 property.optimizeEnum = OptimizeEnum ? RTI_XCDR_TRUE : RTI_XCDR_FALSE;
149 PropertyConfigurator::configure(property);
150 }
151
152 RTIXCdrInterpreterPrograms *programs_;
153
154public:
155 // Returns the singleton instance. It can be NULL if the program creation
156 // failed.
157 static RTIXCdrInterpreterPrograms * get_instance()
158 {
159 static ProgramsSingleton<
160 TopicType,
161 ProgramKind,
162 ResolveAlias,
163 InlineStruct,
164 OptimizeEnum,
165 KeysOnly,
166 PropertyConfigurator> instance;
167 return instance.programs_;
168 }
169
170 // Creates the programs returned by get_instance() again for this singleton.
171 // This is required in testing when the global interpreter properties are
172 // modified from test to test.
173 static RTIXCdrInterpreterPrograms *regenerate_programs()
174 {
175 RTIXCdrInterpreterPrograms *programs = get_instance();
176 if (programs == NULL) {
177 return NULL;
178 }
179
180 RTIXCdrInterpreterProgramsGenProperty property =
181 RTIXCdrInterpreterProgramsGenProperty_INITIALIZER;
182 configure_property(property);
183
184 // finalize and re-initialize the programs. Note that get_instance()
185 // will continue returning the same object, but internally it's been
186 // re-initialized.
187 RTIXCdrInterpreterPrograms_finalize(programs);
188 if (!RTIXCdrInterpreterPrograms_initialize(
189 programs,
190 type_code<TopicType>::get(),
191 &property,
192 ProgramKind)) {
193 return NULL;
194 }
195
196 return programs;
197 }
198
199};
200
201// Returns a ProgramsSingleton with the programs that the generated
202// to/from cdr functions need
203template <
204 typename TopicType,
205 bool ResolveAlias,
206 bool InlineStruct,
207 bool OptimizeEnum,
208 typename PropertyConfigurator>
209RTIXCdrInterpreterPrograms * get_cdr_serialization_programs_w_property_configurator()
210{
211 return ProgramsSingleton<
212 TopicType,
213 RTI_XCDR_SER_PROGRAM
214 | RTI_XCDR_DESER_PROGRAM
215 | RTI_XCDR_GET_SER_SIZE_PROGRAM
216 | RTI_XCDR_GET_MAX_SER_SIZE_PROGRAM,
217 ResolveAlias,
218 InlineStruct,
219 OptimizeEnum,
220 false,
221 PropertyConfigurator>::get_instance();
222}
223
224template <
225 typename TopicType,
226 bool ResolveAlias,
227 bool InlineStruct,
228 bool OptimizeEnum>
229RTIXCdrInterpreterPrograms * get_cdr_serialization_programs()
230{
231 return ProgramsSingleton<
232 TopicType,
233 RTI_XCDR_SER_PROGRAM
234 | RTI_XCDR_DESER_PROGRAM
235 | RTI_XCDR_GET_SER_SIZE_PROGRAM
236 | RTI_XCDR_GET_MAX_SER_SIZE_PROGRAM,
237 ResolveAlias,
238 InlineStruct,
239 OptimizeEnum,
240 false,
241 NoOpPropertyConfigurator>::get_instance();
242}
243
244/*i
245 * Provides the interpreter programs for a type. (Currently it only provides
246 * the initialize_sample program used to initialize FlatData samples)
247 *
248 * This struct is to be specialized in generated code for each TopicType.
249 * Specializations must define a static member function:
250 * RTIXCdrInterpreterPrograms * get(), which will call
251 * get_cdr_initialization_programs() with the options (ResolveAlias, etc.) used
252 * to generate code for this type.
253 */
254template <typename SampleType>
255struct type_programs;
256
257} } // namespace rti::xcdr
258
259#endif // RTI_XCDR_INTERPRETER_HPP_
The RTI namespace.
Definition: AggregationBuilders.hpp:17