RTI Connext Traditional C++ API  Version 6.1.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  return RTIXCdrTypeCode_hasCFriendlyCdrLayout(
39  type_code<TopicType>::get(),
40  &size,
41  &alignment,
42  /* The number of element is not important; it's only important to
43  know if there's one or more than one */
44  IsCollection ? 2 : 1,
45  RTI_XCDR_TRUE);
46 }
47 
48 }
49 
50 template <typename TopicType>
51 bool has_cpp_friendly_cdr_layout()
52 {
53  static bool result =
54  detail::has_cpp_friendly_cdr_layout_impl<TopicType, false>();
55  return result;
56 }
57 
58 template <typename TopicType>
59 bool has_cpp_friendly_cdr_layout_collection()
60 {
61  static bool result =
62  detail::has_cpp_friendly_cdr_layout_impl<TopicType, true>();
63  return result;
64 }
65 
66 struct NoOpPropertyConfigurator {
67  static void configure(RTIXCdrInterpreterProgramsGenProperty&)
68  {
69  // In general (trad. and micro C++), no additional properties.
70  //
71  // dds_cpp.2.0 defines a custom Configurator
72  //
73  }
74 };
75 
76 /*i
77  * @brief Programs singleton for the to/from cdr functions
78  *
79  * The combination of the ProgramSingleton template parameters builds a single
80  * instance for the given type, program kinds and options.
81  * This will typically be instantiated in IDL-generated code.
82  *
83  * @tparam TopicType The topic-type these programs operate with
84  * @tparam ProgramKind A mask of the different programs to be generated
85  * @tparam ResolveAlias Value for RTIXCdrInterpreterProgramsGenProperty::resolveAlias
86  * when creating the programs
87  * @tparam InlineStruct Value for RTIXCdrInterpreterProgramsGenProperty::inlineStruct
88  * when creating the programs
89  * @tparam OptimizeEnum Value for RTIXCdrInterpreterProgramsGenProperty::optimizeEnum
90  * when creating the programs
91  * @tparam KeysOnly (default false) Whether to generate programs only for key fields
92  * @tparam PropertyConfigurator (default no-op) A functor that configures
93  * additional RTIXCdrInterpreterProgramsGenProperty to generate the
94  * programs.
95  *
96  * get_instance() creates the singleton the first time it's called. The singleton
97  * is automatically deleted by the destructor of a static local variable.
98  */
99 template <
100  typename TopicType,
101  int ProgramKind,
102  bool ResolveAlias,
103  bool InlineStruct,
104  bool OptimizeEnum,
105  bool KeysOnly = false,
106  typename PropertyConfigurator = NoOpPropertyConfigurator>
107 struct ProgramsSingleton {
108 private:
109  ProgramsSingleton()
110  {
111  RTIXCdrInterpreterProgramsGenProperty property =
112  RTIXCdrInterpreterProgramsGenProperty_INITIALIZER;
113 
114  property.generateWithAllFields =
115  KeysOnly ? RTI_XCDR_FALSE : RTI_XCDR_TRUE;
116  property.resolveAlias = ResolveAlias ? RTI_XCDR_TRUE : RTI_XCDR_FALSE;
117  property.inlineStruct = InlineStruct ? RTI_XCDR_TRUE : RTI_XCDR_FALSE;
118  property.optimizeEnum = OptimizeEnum ? RTI_XCDR_TRUE : RTI_XCDR_FALSE;
119  PropertyConfigurator::configure(property);
120 
121  programs_ = RTIXCdrInterpreterPrograms_new(
122  type_code<TopicType>::get(),
123  &property,
124  ProgramKind);
125  }
126 
127  ~ProgramsSingleton()
128  {
129  if (programs_ != NULL) {
130  RTIXCdrInterpreterPrograms_delete(programs_);
131  }
132  }
133 
134  RTIXCdrInterpreterPrograms *programs_;
135 
136 public:
137  // Returns the singleton instance. It can be NULL if the program creation
138  // failed.
139  static RTIXCdrInterpreterPrograms * get_instance()
140  {
141  static ProgramsSingleton<
142  TopicType,
143  ProgramKind,
144  ResolveAlias,
145  InlineStruct,
146  OptimizeEnum,
147  KeysOnly,
148  PropertyConfigurator> instance;
149  return instance.programs_;
150  }
151 
152 };
153 
154 // Returns a ProgramsSingleton with the programs that the generated
155 // to/from cdr functions need
156 template <
157  typename TopicType,
158  bool ResolveAlias,
159  bool InlineStruct,
160  bool OptimizeEnum,
161  typename PropertyConfigurator>
162 RTIXCdrInterpreterPrograms * get_cdr_serialization_programs_w_property_configurator()
163 {
164  return ProgramsSingleton<
165  TopicType,
166  RTI_XCDR_SER_PROGRAM
167  | RTI_XCDR_DESER_PROGRAM
168  | RTI_XCDR_GET_SER_SIZE_PROGRAM
169  | RTI_XCDR_GET_MAX_SER_SIZE_PROGRAM,
170  ResolveAlias,
171  InlineStruct,
172  OptimizeEnum,
173  false,
174  PropertyConfigurator>::get_instance();
175 }
176 
177 template <
178  typename TopicType,
179  bool ResolveAlias,
180  bool InlineStruct,
181  bool OptimizeEnum>
182 RTIXCdrInterpreterPrograms * get_cdr_serialization_programs()
183 {
184  return ProgramsSingleton<
185  TopicType,
186  RTI_XCDR_SER_PROGRAM
187  | RTI_XCDR_DESER_PROGRAM
188  | RTI_XCDR_GET_SER_SIZE_PROGRAM
189  | RTI_XCDR_GET_MAX_SER_SIZE_PROGRAM,
190  ResolveAlias,
191  InlineStruct,
192  OptimizeEnum,
193  false,
194  NoOpPropertyConfigurator>::get_instance();
195 }
196 
197 /*i
198  * Provides the interpreter programs for a type. (Currently it only provides
199  * the initialize_sample program used to initialize FlatData samples)
200  *
201  * This struct is to be specialized in generated code for each TopicType.
202  * Specializations must define a static member function:
203  * RTIXCdrInterpreterPrograms * get(), which will call
204  * get_cdr_initialization_programs() with the options (ResolveAlias, etc.) used
205  * to generate code for this type.
206  */
207 template <typename SampleType>
208 struct type_programs;
209 
210 } } // namespace rti::xcdr
211 
212 #endif // RTI_XCDR_INTERPRETER_HPP_
Definition: AggregationBuilders.hpp:17