RTI Connext Traditional C++ API  Version 7.0.0
Interpreter.hpp
1 /* $Id$
2 
3 (c) Copyright, Real-Time Innovations, 2018.
4 All rights reserved.
5 
6 No duplications, whole or partial, manual or electronic, may be made
7 without express written permission. Any such copies, or
8 revisions thereof, must display this notice unaltered.
9 This 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 
19 namespace 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  */
28 template <typename TopicType>
29 struct type_code;
30 
31 namespace detail {
32 
33 template <typename TopicType, bool IsCollection>
34 bool has_cpp_friendly_cdr_layout_impl()
35 {
36  RTIXCdrUnsignedLongLong size;
37  RTIXCdrAlignment alignment;
38  struct RTIXCdrGlobalTypePluginProgramProperty property;
39 
40  RTIXCdrInterpreter_getGlobalTypePluginProgramProperty(&property);
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  property.dheaderInNonPrimitiveCollections);
53 }
54 
55 }
56 
57 template <typename TopicType>
58 bool has_cpp_friendly_cdr_layout()
59 {
60  static bool result =
61  detail::has_cpp_friendly_cdr_layout_impl<TopicType, false>();
62  return result;
63 }
64 
65 template <typename TopicType>
66 bool has_cpp_friendly_cdr_layout_collection()
67 {
68  static bool result =
69  detail::has_cpp_friendly_cdr_layout_impl<TopicType, true>();
70  return result;
71 }
72 
73 struct NoOpPropertyConfigurator {
74  static void configure(RTIXCdrInterpreterProgramsGenProperty&)
75  {
76  // In general (trad. and micro C++), no additional properties.
77  //
78  // dds_cpp.2.0 defines a custom Configurator
79  //
80  }
81 };
82 
83 /*i
84  * @brief Programs singleton for the to/from cdr functions
85  *
86  * The combination of the ProgramSingleton template parameters builds a single
87  * instance for the given type, program kinds and options.
88  * This will typically be instantiated in IDL-generated code.
89  *
90  * @tparam TopicType The topic-type these programs operate with
91  * @tparam ProgramKind A mask of the different programs to be generated
92  * @tparam ResolveAlias Value for RTIXCdrInterpreterProgramsGenProperty::resolveAlias
93  * when creating the programs
94  * @tparam InlineStruct Value for RTIXCdrInterpreterProgramsGenProperty::inlineStruct
95  * when creating the programs
96  * @tparam OptimizeEnum Value for RTIXCdrInterpreterProgramsGenProperty::optimizeEnum
97  * when creating the programs
98  * @tparam KeysOnly (default false) Whether to generate programs only for key fields
99  * @tparam PropertyConfigurator (default no-op) A functor that configures
100  * additional RTIXCdrInterpreterProgramsGenProperty to generate the
101  * programs.
102  *
103  * get_instance() creates the singleton the first time it's called. The singleton
104  * is automatically deleted by the destructor of a static local variable.
105  */
106 template <
107  typename TopicType,
108  int ProgramKind,
109  bool ResolveAlias,
110  bool InlineStruct,
111  bool OptimizeEnum,
112  bool KeysOnly = false,
113  typename PropertyConfigurator = NoOpPropertyConfigurator>
114 struct ProgramsSingleton {
115 private:
116  ProgramsSingleton()
117  {
118  RTIXCdrInterpreterProgramsGenProperty property =
119  RTIXCdrInterpreterProgramsGenProperty_INITIALIZER;
120 
121  property.generateWithAllFields =
122  KeysOnly ? RTI_XCDR_FALSE : RTI_XCDR_TRUE;
123  property.resolveAlias = ResolveAlias ? RTI_XCDR_TRUE : RTI_XCDR_FALSE;
124  property.inlineStruct = InlineStruct ? RTI_XCDR_TRUE : RTI_XCDR_FALSE;
125  property.optimizeEnum = OptimizeEnum ? RTI_XCDR_TRUE : RTI_XCDR_FALSE;
126  PropertyConfigurator::configure(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  RTIXCdrInterpreterPrograms *programs_;
142 
143 public:
144  // Returns the singleton instance. It can be NULL if the program creation
145  // failed.
146  static RTIXCdrInterpreterPrograms * get_instance()
147  {
148  static ProgramsSingleton<
149  TopicType,
150  ProgramKind,
151  ResolveAlias,
152  InlineStruct,
153  OptimizeEnum,
154  KeysOnly,
155  PropertyConfigurator> instance;
156  return instance.programs_;
157  }
158 
159 };
160 
161 // Returns a ProgramsSingleton with the programs that the generated
162 // to/from cdr functions need
163 template <
164  typename TopicType,
165  bool ResolveAlias,
166  bool InlineStruct,
167  bool OptimizeEnum,
168  typename PropertyConfigurator>
169 RTIXCdrInterpreterPrograms * get_cdr_serialization_programs_w_property_configurator()
170 {
171  return ProgramsSingleton<
172  TopicType,
173  RTI_XCDR_SER_PROGRAM
174  | RTI_XCDR_DESER_PROGRAM
175  | RTI_XCDR_GET_SER_SIZE_PROGRAM
176  | RTI_XCDR_GET_MAX_SER_SIZE_PROGRAM,
177  ResolveAlias,
178  InlineStruct,
179  OptimizeEnum,
180  false,
181  PropertyConfigurator>::get_instance();
182 }
183 
184 template <
185  typename TopicType,
186  bool ResolveAlias,
187  bool InlineStruct,
188  bool OptimizeEnum>
189 RTIXCdrInterpreterPrograms * get_cdr_serialization_programs()
190 {
191  return ProgramsSingleton<
192  TopicType,
193  RTI_XCDR_SER_PROGRAM
194  | RTI_XCDR_DESER_PROGRAM
195  | RTI_XCDR_GET_SER_SIZE_PROGRAM
196  | RTI_XCDR_GET_MAX_SER_SIZE_PROGRAM,
197  ResolveAlias,
198  InlineStruct,
199  OptimizeEnum,
200  false,
201  NoOpPropertyConfigurator>::get_instance();
202 }
203 
204 /*i
205  * Provides the interpreter programs for a type. (Currently it only provides
206  * the initialize_sample program used to initialize FlatData samples)
207  *
208  * This struct is to be specialized in generated code for each TopicType.
209  * Specializations must define a static member function:
210  * RTIXCdrInterpreterPrograms * get(), which will call
211  * get_cdr_initialization_programs() with the options (ResolveAlias, etc.) used
212  * to generate code for this type.
213  */
214 template <typename SampleType>
215 struct type_programs;
216 
217 } } // namespace rti::xcdr
218 
219 #endif // RTI_XCDR_INTERPRETER_HPP_
Definition: AggregationBuilders.hpp:17