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

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