13 #ifndef RTI_XCDR_STREAM_HPP_
14 #define RTI_XCDR_STREAM_HPP_
16 #include "xcdr/xcdr_stream.h"
18 namespace rti {
namespace xcdr {
21 #define RTI_XCDR_SPECIALIZE_PRIMITIVE_TYPE_TRAITS_BODY(\
25 static void serialize_fast(RTIXCdrStream *stream, const TYPE* v)\
27 RTIXCdrStream_ ## SERIALIZE_MACRO ## ByteFast(stream, (CDR_TYPE *) v);\
29 static void deserialize_fast(RTIXCdrStream *stream, TYPE* v)\
31 RTIXCdrStream_de ## SERIALIZE_MACRO ## ByteFast(stream, (CDR_TYPE *) v);\
33 static bool serialize(RTIXCdrStream *stream, const TYPE* v)\
35 return RTIXCdrStream_ ## SERIALIZE_MACRO ## Byte(stream, (CDR_TYPE *) v, RTI_XCDR_TRUE);\
37 static bool deserialize(RTIXCdrStream *stream, TYPE* v)\
39 return RTIXCdrStream_de ## SERIALIZE_MACRO ## Byte(stream, (CDR_TYPE *) v, RTI_XCDR_TRUE);\
42 #define RTI_XCDR_SPECIALIZE_PRIMITIVE_TYPE_TRAITS(\
47 struct primitive_type_traits<TYPE> {\
48 RTI_XCDR_SPECIALIZE_PRIMITIVE_TYPE_TRAITS_BODY(TYPE, CDR_TYPE, SERIALIZE_MACRO)\
51 #define RTI_XCDR_SPECIALIZE_PRIMITIVE_TYPE_TRAITS_W_ALIGNMENT_PARAM(\
57 struct primitive_type_traits<TYPE> {\
58 static void serialize_fast(RTIXCdrStream *stream, const TYPE* v)\
60 RTIXCdrStream_ ## SERIALIZE_MACRO ## ByteFast(stream, (CDR_TYPE *) v);\
62 static void deserialize_fast(RTIXCdrStream *stream, TYPE* v)\
64 RTIXCdrStream_de ## SERIALIZE_MACRO ## ByteFast(stream, (CDR_TYPE *) v);\
66 static bool serialize(RTIXCdrStream *stream, const TYPE* v)\
68 return RTIXCdrStream_ ## SERIALIZE_MACRO ## Byte(stream, (CDR_TYPE *) v, RTI_XCDR_TRUE, ALIGNMENT);\
70 static bool deserialize(RTIXCdrStream *stream, TYPE* v)\
72 return RTIXCdrStream_de ## SERIALIZE_MACRO ## Byte(stream, (CDR_TYPE *) v, RTI_XCDR_TRUE, ALIGNMENT);\
76 #define RTI_XCDR_SPECIALIZE_PRIMITIVE_TYPE_TRAITS_NO_ALIGN_PARAM(\
81 struct primitive_type_traits<TYPE> {\
82 static void serialize_fast(RTIXCdrStream *stream, const TYPE* v)\
84 RTIXCdrStream_ ## SERIALIZE_MACRO ## ByteFast(stream, (CDR_TYPE *) v);\
86 static void deserialize_fast(RTIXCdrStream *stream, TYPE* v)\
88 RTIXCdrStream_de ## SERIALIZE_MACRO ## ByteFast(stream, (CDR_TYPE *) v);\
90 static bool serialize(RTIXCdrStream *stream, const TYPE* v)\
92 return RTIXCdrStream_ ## SERIALIZE_MACRO ## Byte(stream, (CDR_TYPE *) v);\
94 static bool deserialize(RTIXCdrStream *stream, TYPE* v)\
96 return RTIXCdrStream_de ## SERIALIZE_MACRO ## Byte(stream, (CDR_TYPE *) v);\
100 #if !defined(RTI_AVOID_FIXED_WIDTH_INTEGERS)
104 template <
typename T>
105 struct primitive_type_traits {};
109 template <
typename T>
110 struct primitive_type_traits {
111 RTI_XCDR_SPECIALIZE_PRIMITIVE_TYPE_TRAITS_BODY(T, RTIXCdr4Byte, serialize4)
115 RTI_XCDR_SPECIALIZE_PRIMITIVE_TYPE_TRAITS_NO_ALIGN_PARAM(\
116 char, RTIXCdr1Byte, serialize1);
127 #if !defined(RTI_AVOID_FIXED_WIDTH_INTEGERS)
128 RTI_XCDR_SPECIALIZE_PRIMITIVE_TYPE_TRAITS_NO_ALIGN_PARAM(\
129 uint8_t, RTIXCdr1Byte, serialize1);
131 RTI_XCDR_SPECIALIZE_PRIMITIVE_TYPE_TRAITS(\
132 int16_t, RTIXCdr2Byte, serialize2);
133 RTI_XCDR_SPECIALIZE_PRIMITIVE_TYPE_TRAITS(\
134 uint16_t, RTIXCdr2Byte, serialize2);
136 RTI_XCDR_SPECIALIZE_PRIMITIVE_TYPE_TRAITS(\
137 int32_t, RTIXCdr4Byte, serialize4);
138 RTI_XCDR_SPECIALIZE_PRIMITIVE_TYPE_TRAITS(\
139 uint32_t, RTIXCdr4Byte, serialize4);
141 typedef uint32_t length_t;
143 RTI_XCDR_SPECIALIZE_PRIMITIVE_TYPE_TRAITS_NO_ALIGN_PARAM(\
144 unsigned char, RTIXCdr1Byte, serialize1);
146 RTI_XCDR_SPECIALIZE_PRIMITIVE_TYPE_TRAITS(\
147 short, RTIXCdr2Byte, serialize2);
148 RTI_XCDR_SPECIALIZE_PRIMITIVE_TYPE_TRAITS(\
149 unsigned short, RTIXCdr2Byte, serialize2);
153 typedef unsigned int length_t;
156 RTI_XCDR_SPECIALIZE_PRIMITIVE_TYPE_TRAITS(\
157 float, RTIXCdr4Byte, serialize4);
159 RTI_XCDR_SPECIALIZE_PRIMITIVE_TYPE_TRAITS_W_ALIGNMENT_PARAM(\
162 RTI_XCDR_SPECIALIZE_PRIMITIVE_TYPE_TRAITS_W_ALIGNMENT_PARAM(\
164 RTI_XCDR_SPECIALIZE_PRIMITIVE_TYPE_TRAITS_W_ALIGNMENT_PARAM(\
165 double, RTIXCdr8Byte, serialize8, 4);
170 template <
typename T>
171 void serialize_fast(RTIXCdrStream& stream,
const T& value)
173 primitive_type_traits<T>::serialize_fast(&stream, &value);
176 template <
typename T>
177 void deserialize_fast(RTIXCdrStream& stream, T& value)
179 primitive_type_traits<T>::deserialize_fast(&stream, &value);
186 RTIXCdrStream_set(&stream_, NULL, 0);
189 explicit Stream(
const RTIXCdrStream& c_stream)
196 Stream(
const Stream& other)
198 stream_ = other.stream_;
204 unsigned char *buffer,
208 RTIXCdrFlatData_initializeStream(
215 bool skip(length_t size)
217 if (!check_size(size)) {
220 RTIXCdrStream_increaseCurrentPosition(&stream_, size);
224 void skip_fast(length_t size)
226 RTIXCdrStream_increaseCurrentPosition(&stream_, size);
229 void skip_back(length_t bytes)
231 RTIXCdrStream_increaseCurrentPosition(
233 -static_cast<int>(bytes));
236 unsigned char * buffer()
238 return reinterpret_cast<unsigned char*
>(stream_._buffer);
241 const unsigned char * buffer()
const
243 return reinterpret_cast<unsigned char*
>(stream_._buffer);
246 unsigned char * current_position()
248 return reinterpret_cast<unsigned char*
>(
249 RTIXCdrStream_getCurrentPosition(&stream_));
252 void current_position(
unsigned char * pos)
254 RTIXCdrStream_setCurrentPosition(
256 reinterpret_cast<char*>(pos));
259 length_t used_size()
const
261 return static_cast<length_t
>(
262 RTIXCdrStream_getCurrentPosition(&stream_) - stream_._buffer);
265 length_t remaining_size()
const
267 return total_size() - used_size();
270 length_t total_size()
const
272 return stream_._bufferLength;
275 bool needs_byte_swap()
const
277 return stream_._needByteSwap == RTI_XCDR_TRUE;
280 template <
typename T>
281 bool serialize(T value)
283 return primitive_type_traits<T>::serialize(&stream_, &value);
286 template <
typename T>
287 void serialize_fast(T value)
289 primitive_type_traits<T>::serialize_fast(&stream_, &value);
292 template <
typename T>
293 bool serialize_no_align(T value)
295 if (!check_size(
sizeof(T))) {
298 primitive_type_traits<T>::serialize_fast(&stream_, &value);
302 template <
typename T>
306 primitive_type_traits<T>::deserialize_fast(&stream_, &value);
310 void serialize_fast(
const void *array, length_t bytes)
312 RTIXCdrStream_serializeNByteFast(&stream_, array, bytes);
315 char * serialize_dheader()
317 return RTIXCdrStream_serializeDHeader(&stream_);
320 bool finish_dheader(
char *dheader_position)
322 return RTIXCdrStream_serializeDHeaderLength(&stream_, dheader_position);
325 bool serialize_emheader(
327 RTIXCdrUnsignedLong parameter_id,
328 RTIXCdrUnsignedLong lc,
329 bool must_understand =
false)
331 RTIXCdrBoolean failure = 0;
332 position = RTIXCdrStream_serializeV2ParameterHeader(
336 must_understand ? RTI_XCDR_TRUE : RTI_XCDR_FALSE,
342 void finish_emheader(
char * emheader_position)
344 RTIXCdrStream_finishV2ParameterHeader(&stream_, emheader_position);
347 bool align(
unsigned int alignment)
349 return RTIXCdrStream_align(&stream_, (RTIXCdrAlignment) alignment);
352 bool check_size(length_t size)
const
354 bool result = RTIXCdrStream_checkSize(&stream_, size) == RTI_XCDR_TRUE;
358 bool need_byte_swap()
const
360 return stream_._needByteSwap != 0;
363 RTIXCdrStream& c_stream()
379 explicit Memento(Stream& stream) :
381 original_position_(stream.current_position()),
382 relative_buffer_(stream.c_stream()._relativeBuffer)
395 if (original_position_ != NULL) {
396 stream_.current_position(original_position_);
397 stream_.c_stream()._relativeBuffer = relative_buffer_;
402 unsigned char * discard()
404 unsigned char *pos = original_position_;
405 original_position_ = NULL;
411 unsigned char *original_position_;
412 char *relative_buffer_;
417 RTIXCdrStream stream_;
422 #endif // RTI_XCDR_STREAM_HPP_