RTI Connext Traditional C++ API Version 7.3.0
FlatSample.hpp
1/*
2(c) Copyright, Real-Time Innovations, 2018.
3All rights reserved.
4
5No duplications, whole or partial, manual or electronic, may be made
6without express written permission. Any such copies, or
7revisions thereof, must display this notice unaltered.
8This 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
39namespace rti { namespace flat {
40
41struct fixed_size_type_tag_t {};
42struct variable_size_type_tag_t {};
43
44typedef RTIXCdrUnsignedLong offset_t;
45typedef RTIXCdrUnsignedLong member_id_t;
46
47// The untemplated base of Sample<T>
48class SampleBase {
49public:
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
86protected: // 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
147template <typename OffsetType>
148class Sample : public SampleBase {
149public:
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
305private:
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
339template <typename T>
341
342} }
343
344#ifdef DOXYGEN_DOCUMENTATION_ONLY
345
376
417
448#endif
449
450#endif // RTI_DDS_FLAT_FLATSAMPLE_HPP_
451
The generic definition of FlatData topic-types.
Definition: FlatSample.hpp:148
OffsetType Offset
The related Offset type.
Definition: FlatSample.hpp:155
Offset root()
Provides the Offset to the top-level type.
Definition: FlatSample.hpp:181
static Sample< OffsetType > * create_data()
Creates an unmanaged FlatData Sample.
Definition: FlatSampleImpl.hpp:67
static void delete_data(rti::flat::Sample< OffsetType > *sample)
Releases an unmanaged Sample.
Definition: FlatSample.hpp:286
Sample< OffsetType > * clone() const
Clones a Sample, creating an unmanaged Sample.
Definition: FlatSample.hpp:259
ConstOffset root() const
Provides the Offset to the top-level type.
Definition: FlatSample.hpp:203
OffsetType::ConstOffset ConstOffset
The related read-only Offset type.
Definition: FlatSample.hpp:162
rti::flat::Sample< MyFlatMutableOffset > MyFlatMutable
Represents an arbitrary user-defined flat mutable IDL struct.
Definition: FlatSample.hpp:416
rti::flat::Sample< MyFlatFinalOffset > MyFlatFinal
Represents an arbitrary user-defined FlatData final IDL struct.
Definition: FlatSample.hpp:375
rti::flat::Sample< MyFlatUnionOffset > MyFlatUnion
Represents an arbitrary user-defined flat mutable IDL union.
Definition: FlatSample.hpp:447
The RTI namespace.
Definition: AggregationBuilders.hpp:17
Given a Sample, an Offset or a Builder, it allows obtaining the other types.
Definition: FlatSample.hpp:340