RTI Connext Traditional C++ API  Version 6.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
FlatSample.hpp
1 /*
2 (c) Copyright, Real-Time Innovations, 2018.
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_DDS_FLAT_FLATSAMPLE_HPP_
12 #define RTI_DDS_FLAT_FLATSAMPLE_HPP_
13 
14 // Note: FlatSampleImpl.hpp needs to be included in addition to this header.
15 
16 #include <cstdlib>
17 #include <memory>
18 
19 #include "xcdr/xcdr_stream_impl.h"
20 #include "xcdr/xcdr_flat_data.h"
21 #include "xcdr/xcdr_interpreter.h"
22 
23 #include "rti/xcdr/Interpreter.hpp" // for type_programs
24 
39 namespace rti { namespace flat {
40 
41 struct fixed_size_type_tag_t {};
42 struct variable_size_type_tag_t {};
43 
44 typedef RTIXCdrUnsignedLong offset_t;
45 typedef RTIXCdrUnsignedLong member_id_t;
46 
47 // The untemplated base of Sample<T>
48 class SampleBase {
49 public:
50 
51  unsigned char* get_buffer()
52  {
53  return &data[0];
54  }
55 
56  const unsigned char* get_buffer() const
57  {
58  return &data[0];
59  }
60 
61  unsigned char* get_root_buffer()
62  {
63  return get_buffer() + RTI_XCDR_ENCAPSULATION_SIZE;
64  }
65 
66  /*i
67  * @brief Provides the address of the top-level type
68  */
69  const unsigned char* get_root_buffer() const
70  {
71  return get_buffer() + RTI_XCDR_ENCAPSULATION_SIZE;
72  }
73 
74  void initialize_stream(
75  RTIXCdrStream& stream,
76  offset_t offset,
77  offset_t serialized_size) const
78  {
79  RTIXCdrFlatData_initializeStream(
80  &stream,
81  const_cast<unsigned char*>(get_buffer()),
82  offset,
83  serialized_size);
84  }
85 
86 protected: // xlc compiler fails when the default constructor is private
87 #if !defined(RTI_FLAT_DATA_CXX11_DELETED_FUNCTIONS)
88  SampleBase() {} // Samples can only be created via Sample::create_data
89  SampleBase(const SampleBase&) {}
90  SampleBase& operator=(const SampleBase&);
91 #else
92  SampleBase() = delete;
93  SampleBase(const SampleBase&) = delete;
94  SampleBase& operator=(const SampleBase&) = delete;
95 #endif
96 
97  // The actual size of data (and therefore, the Sample object) is variable,
98  // and equal to the serialized size of the IDL Sample
99  unsigned char data[1];
100 };
101 
147 template <typename OffsetType>
148 class Sample : public SampleBase {
149 public:
155  typedef OffsetType Offset;
156 
162  typedef typename OffsetType::ConstOffset ConstOffset;
163 
182  {
183  return root_impl(typename Offset::offset_kind());
184  }
185 
204  {
205  return root_impl(typename ConstOffset::offset_kind());
206  }
207 
211  RTIXCdrEndian endian() const
212  {
213  RTIXCdrStream tmp_stream;
214  initialize_stream(tmp_stream, 0, RTI_XCDR_ENCAPSULATION_SIZE);
215  return tmp_stream._endian;
216  }
217 
221  static Sample<OffsetType>* from_buffer(unsigned char *buffer)
222  {
223  return reinterpret_cast<Sample<OffsetType>*>(buffer);
224  }
225 
245  static Sample<OffsetType>* create_data(); // impl in FlatSampleImpl.hpp
246 
260  {
261  unsigned char *buffer = RTIXCdrFlatData_cloneSample(
262  get_buffer(),
263  sample_size());
264 
265  if (buffer == NULL) {
266  #ifndef RTI_FLAT_DATA_NO_EXCEPTIONS
267  throw std::bad_alloc();
268  #else
269  return NULL;
270  #endif
271  }
272 
273  return reinterpret_cast<rti::flat::Sample<OffsetType> *>(buffer);
274  }
275 
287  {
288  RTIXCdrFlatData_deleteSample(sample);
289  }
290 
293  offset_t buffer_size() const
294  {
295  return sample_size() + RTI_XCDR_ENCAPSULATION_SIZE;
296  }
297 
300  offset_t sample_size() const
301  {
302  return sample_size_impl(typename OffsetType::offset_kind());
303  }
304 
305 private:
306  offset_t sample_size_impl(fixed_size_type_tag_t) const
307  {
308  return OffsetType::serialized_size(0);
309  }
310 
311  offset_t sample_size_impl(variable_size_type_tag_t) const
312  {
313  return RTIXCdrFlatSample_getMutableSampleSize(
314  get_buffer(),
315  RTI_XCDR_ENCAPSULATION_SIZE);
316  }
317 
318  Offset root_impl(fixed_size_type_tag_t)
319  {
320  return Offset(this, RTI_XCDR_ENCAPSULATION_SIZE);
321  }
322 
323  Offset root_impl(variable_size_type_tag_t)
324  {
325  return Offset(this, RTI_XCDR_ENCAPSULATION_SIZE, sample_size());
326  }
327 
328  ConstOffset root_impl(fixed_size_type_tag_t) const
329  {
330  return ConstOffset(this, RTI_XCDR_ENCAPSULATION_SIZE);
331  }
332 
333  ConstOffset root_impl(variable_size_type_tag_t) const
334  {
335  return ConstOffset(this, RTI_XCDR_ENCAPSULATION_SIZE, sample_size());
336  }
337 };
338 
339 template <typename T>
340 struct flat_type_traits;
341 
342 } }
343 
344 #ifdef DOXYGEN_DOCUMENTATION_ONLY
345 
376 
417 
448 #endif
449 
450 #endif // RTI_DDS_FLAT_FLATSAMPLE_HPP_
451 

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