RTI Routing Service  Version 6.1.1
TypeInfo.hpp
1 /*
2  * (c) Copyright, Real-Time Innovations, 2017.
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_ROUTING_ADAPTER_TYPE_INFOHPP_
12 #define RTI_ROUTING_ADAPTER_TYPE_INFOHPP_
13 
14 // IMPORTANT: macros.hpp must be the first RTI header included in every header
15 // file so that symbols are exported correctly on Windows
16 #include <dds/core/macros.hpp>
17 
18 #include <routingservice/routingservice_infrastructure.h>
19 #include <dds/core/Value.hpp>
20 #include <dds/core/SafeEnumeration.hpp>
21 #include <dds/core/xtypes/DynamicType.hpp>
22 #include <rti/core/NativeValueType.hpp>
23 
24 namespace rti { namespace routing {
25 
45  enum type {
54  DYNAMIC_TYPE = RTI_ROUTING_SERVICE_TYPE_REPRESENTATION_DYNAMIC_TYPE,
64  XML = RTI_ROUTING_SERVICE_TYPE_REPRESENTATION_XML,
70  JAVA_OBJECT = RTI_ROUTING_SERVICE_TYPE_REPRESENTATION_JAVA_OBJECT,
77  CUSTOM = RTI_ROUTING_SERVICE_TYPE_REPRESENTATION_FIRST_CUSTOM_REPRESENTATION
78  };
79 };
80 
89 typedef dds::core::safe_enum<TypeRepresentationKind_def> TypeRepresentationKind;
90 
91 
92 class TypeInfoAdapter {
93 public:
94  typedef RTI_RoutingServiceTypeInfo native_type;
95 
96  static void initialize(native_type& native_value)
97  {
98  static const native_type default_value = {
99  NULL,
100  RTI_ROUTING_SERVICE_TYPE_REPRESENTATION_DYNAMIC_TYPE,
101  NULL};
102  native_value = default_value;
103  }
104 
105  static void finalize(native_type& native_value)
106  {
107  if (native_value.type_name != NULL) {
108  RTIOsapiHeap_freeString(native_value.type_name);
109  native_value.type_name = NULL;
110  }
111  }
112 
113  static void copy(native_type& destination, const native_type& source)
114  {
115  char *result = DDS_String_replace(
116  &destination.type_name,
117  source.type_name);
118  if (source.type_name != NULL && result == NULL) {
119  throw std::bad_alloc();
120  }
121  destination.type_representation_kind =
122  source.type_representation_kind;
123  destination.type_representation =
124  source.type_representation;
125  }
126 
127  static bool equals(const native_type& first, const native_type& second)
128  {
129  if (strcmp(first.type_name, second.type_name) != 0) {
130  return false;
131  }
132  if (first.type_representation_kind
133  != second.type_representation_kind) {
134  return false;
135  }
136 
137  return first.type_representation == second.type_representation;
138  }
139 
140 };
141 
142 class TypeInfo;
143 
144 }
145 
146 // native_type_traits needs to be defined in rti::core
147 namespace core {
148 
149 template <>
150 struct native_type_traits<rti::routing::TypeInfo> {
151  typedef rti::routing::TypeInfoAdapter adapter_type;
152  typedef RTI_RoutingServiceTypeInfo native_type;
153 };
154 
155 }
156 
157 namespace routing {
158 
169 class TypeInfo : public rti::core::NativeValueType<TypeInfo> {
170 public:
171  RTI_NATIVE_VALUE_TYPE_DEFINE_DEFAULT_MOVE_OPERATIONS(TypeInfo)
172 
173  typedef rti::core::NativeValueType<TypeInfo> Base;
174 
180  typedef RTI_RoutingServiceTypeRepresentation TypeRepresentationType;
181 public:
182 
193  const std::string& the_type_name,
194  TypeRepresentationKind the_type_representation_kind =
195  TypeRepresentationKind::DYNAMIC_TYPE)
196  {
197  type_name(the_type_name);
198  type_representation_kind(the_type_representation_kind);
199  }
200 
201  TypeInfo(const struct RTI_RoutingServiceTypeInfo &native_stream_info)
202  : Base(native_stream_info)
203  {
204  }
205 
209  std::string type_name() const
210  {
211  return native().type_name;
212  }
213 
223  TypeInfo& type_name(const std::string& the_type_name)
224  {
225 
226  if (DDS_String_replace(
227  &native().type_name,
228  the_type_name.c_str()) == NULL) {
229  throw std::bad_alloc();
230  }
231 
232  return *this;
233  }
234 
239  {
240  return static_cast<TypeRepresentationKind::type>(
241  native().type_representation_kind);
242  }
243 
252  TypeRepresentationKind the_type_representation_kind)
253  {
254  native().type_representation_kind = static_cast<int>(
255  the_type_representation_kind.underlying());
256  return *this;
257  }
258 
262  TypeRepresentationType type_representation() const
263  {
264  return native().type_representation;
265  }
266 
275  TypeInfo& type_representation(TypeRepresentationType the_type_representation)
276  {
277  native().type_representation = the_type_representation;
278  return *this;
279  }
280 
287  const dds::core::xtypes::DynamicType& dynamic_type() const
288  {
289  if (type_representation_kind() != TypeRepresentationKind::DYNAMIC_TYPE) {
290  throw dds::core::IllegalOperationError(
291  "type representation is not dynamic type");
292  }
293 
294  const DDS_TypeCode *native_typecode = reinterpret_cast<DDS_TypeCode*>(
295  type_representation());
296  return rti::core::native_conversions::cast_from_native<
297  dds::core::xtypes::DynamicType>(*native_typecode);
298  }
299 
308  void dynamic_type(const dds::core::xtypes::DynamicType *dynamic_type)
309  {
310  DDS_TypeCode *native_typecode = const_cast<DDS_TypeCode*>(
311  reinterpret_cast<const DDS_TypeCode *>(dynamic_type));
312  type_representation(static_cast<TypeRepresentationType>(native_typecode));
313  type_representation_kind(TypeRepresentationKind::DYNAMIC_TYPE);
314  }
315 };
316 
317 }}
318 
319 #endif // RTI_ROUTING_ADAPTER_TYPE_INFOHPP_
Definition of the type information associated with a RTI Routing Service stream.
Definition: TypeInfo.hpp:169
TypeInfo & type_representation(TypeRepresentationType the_type_representation)
Sets the type representation value.
Definition: TypeInfo.hpp:275
TypeInfo(const std::string &the_type_name, TypeRepresentationKind the_type_representation_kind=TypeRepresentationKind::DYNAMIC_TYPE)
Constructs a TypeInfo with the minimum required information.
Definition: TypeInfo.hpp:192
TypeRepresentationKind type_representation_kind() const
Getter (see setter with the same name)
Definition: TypeInfo.hpp:238
type
Definition of all the possible type representation kinds.
Definition: TypeInfo.hpp:45
void dynamic_type(const dds::core::xtypes::DynamicType *dynamic_type)
Sets the type representation value as DynamicType object.
Definition: TypeInfo.hpp:308
valid type representations that RTI Routing Service accepts.
TypeInfo & type_representation_kind(TypeRepresentationKind the_type_representation_kind)
Sets the type representation kind.
Definition: TypeInfo.hpp:251
const dds::core::xtypes::DynamicType & dynamic_type() const
Returns the type_representation as DynamicType object.
Definition: TypeInfo.hpp:287
TypeRepresentationType type_representation() const
Getter (see setter with the same name)
Definition: TypeInfo.hpp:262
Dynamic type representation.
Definition: TypeInfo.hpp:54
std::string type_name() const
Getter (see setter with the same name)
Definition: TypeInfo.hpp:209
The definition of the rti::routing::TypeRepresentationKind.
Definition: TypeInfo.hpp:41
TypeInfo & type_name(const std::string &the_type_name)
Sets the registered name of the type associated with the data.
Definition: TypeInfo.hpp:223
[Not supported] Custom type representation.
Definition: TypeInfo.hpp:77
RTI_RoutingServiceTypeRepresentation TypeRepresentationType
Type representation defined as an opaque pointer.
Definition: TypeInfo.hpp:180
Definition: AdapterPlugin.hpp:25
[Not supported] Java object type representation.
Definition: TypeInfo.hpp:70
[Not supported] XML type representation.
Definition: TypeInfo.hpp:64