11 #ifndef RTI_DDS_FLAT_SEQUENCEOFFSETS_HPP_
12 #define RTI_DDS_FLAT_SEQUENCEOFFSETS_HPP_
14 #include "rti/flat/Offset.hpp"
15 #include "rti/flat/SequenceIterator.hpp"
17 namespace rti {
namespace flat {
37 rti::flat::SampleBase *sample,
39 offset_t sequence_size)
46 bool is_cpp_compatible()
const
48 return sizeof(T) == 1 || !stream_.needs_byte_swap();
66 return reinterpret_cast<T*
>(this->
get_buffer());
74 const T * get_elements()
const
76 return reinterpret_cast<const T*
>(this->
get_buffer());
91 if (!stream_.check_size((i + 1) * ((
unsigned int)
sizeof(T)))) {
95 return OffsetBase::template deserialize<T>(i * (
unsigned int)
sizeof(T));
114 if (!stream_.check_size((i + 1) * static_cast<unsigned int>(
sizeof(T)))) {
118 return OffsetBase::serialize(i * static_cast<unsigned int>(
sizeof(T)), value);
122 template <
typename T>
123 struct PrimitiveSequenceOffsetHelper;
132 template <
typename T>
135 typedef variable_size_type_tag_t offset_kind;
136 typedef PrimitiveSequenceOffsetHelper<T> Helper;
143 rti::flat::SampleBase *sample,
145 offset_t serialized_size)
148 serialized_size >=
static_cast<offset_t
>(
sizeof(rti::xcdr::length_t)) ? sample : NULL,
149 offset +
static_cast<offset_t
>(
sizeof(rti::xcdr::length_t)),
150 serialized_size -
static_cast<offset_t
>(
sizeof(rti::xcdr::length_t)))
152 #ifdef RTI_FLAT_DATA_NO_EXCEPTIONS
155 rti::xcdr::Stream::Memento stream_memento(this->stream_);
156 this->stream_.skip_back(
sizeof(rti::xcdr::length_t));
157 element_count_ = this->stream_.template deserialize_fast<rti::xcdr::length_t>();
158 #ifdef RTI_FLAT_DATA_NO_EXCEPTIONS
169 RTI_FLAT_OFFSET_CHECK_NOT_NULL(
return 0);
171 return element_count_;
175 unsigned int element_count_;
179 template <
typename T>
180 struct PrimitiveSequenceOffsetHelper {
181 static offset_t calculate_serialized_size(
182 rti::flat::SampleBase *sample,
183 offset_t absolute_offset,
186 RTI_FLAT_ASSERT(sample != NULL,
return 0);
187 RTI_FLAT_ASSERT(max_size >
sizeof(rti::xcdr::length_t),
return 0);
189 PrimitiveSequenceOffset<T> tmp(sample, absolute_offset, max_size);
191 return (tmp.element_count() *
sizeof(T)) +
sizeof(rti::xcdr::length_t);
206 template <
typename T,
unsigned int N>
209 typedef fixed_size_type_tag_t offset_kind;
234 static offset_t serialized_size(offset_t)
236 return sizeof(T) * N;
243 struct StringOffsetHelper;
252 typedef variable_size_type_tag_t offset_kind;
253 typedef StringOffsetHelper Helper;
261 offset_t absolute_offset,
262 offset_t serialized_size)
277 return reinterpret_cast<char*
>(this->
get_buffer());
286 return reinterpret_cast<const char*
>(this->
get_buffer());
302 struct StringOffsetHelper {
303 static offset_t calculate_serialized_size(
304 rti::flat::SampleBase *sample,
305 offset_t absolute_offset,
308 StringOffset tmp(sample, absolute_offset, max_size);
309 return tmp.element_count()
311 + (
unsigned int)
sizeof(rti::xcdr::length_t);
316 typedef PrimitiveSequenceOffset<unsigned char> WStringOffset;
330 template <
typename ElementOffset>
346 rti::flat::SampleBase *sample,
348 offset_t sequence_size)
357 bool is_cpp_compatible() const
359 return !stream_.needs_byte_swap()
360 && rti::xcdr::has_cpp_friendly_cdr_layout<
361 typename rti::flat::flat_type_traits<ElementOffset>::flat_type>();
364 ElementOffset get_element(
unsigned int i)
366 RTI_FLAT_OFFSET_CHECK_NOT_NULL(
return ElementOffset());
368 return get_element_impl<ElementOffset>(
370 typename ElementOffset::offset_kind());
387 RTI_FLAT_OFFSET_CHECK_NOT_NULL(
return iterator(NULL, 0, 0));
400 RTI_FLAT_OFFSET_CHECK_NOT_NULL(
return iterator(NULL, 0, 0));
410 template <
typename E>
411 E get_element_impl(
unsigned int i, variable_size_type_tag_t)
414 while (it !=
end() && i > 0) {
425 template <
typename E>
426 E get_element_impl(
unsigned int i, fixed_size_type_tag_t)
428 offset_t size = i * E::serialized_size_w_padding();
431 if (!stream_.check_size(size + E::serialized_size(0))) {
435 return E(this->sample_, this->absolute_offset_ + size);
439 template <
typename ElementOffset>
440 struct SequenceOffsetHelper;
456 template <
typename ElementOffset>
459 typedef variable_size_type_tag_t offset_kind;
460 typedef SequenceOffsetHelper<ElementOffset> Helper;
467 rti::flat::SampleBase *sample,
469 offset_t sequence_size)
472 sequence_size >=
sizeof(rti::xcdr::length_t) ? sample: NULL,
477 RTIXCdrAlignment_alignSizeUp(offset, RTI_XCDR_SEQ_LENGTH_ALIGNMENT)
478 +
static_cast<offset_t
>(
sizeof(rti::xcdr::length_t)),
479 sequence_size -
static_cast<offset_t
>(
sizeof(rti::xcdr::length_t)))
481 #ifdef RTI_FLAT_DATA_NO_EXCEPTIONS
484 rti::xcdr::Stream::Memento stream_memento(this->stream_);
485 this->stream_.skip_back(
sizeof(rti::xcdr::length_t));
486 element_count_ = this->stream_.template deserialize_fast<rti::xcdr::length_t>();
487 #ifdef RTI_FLAT_DATA_NO_EXCEPTIONS
500 RTI_FLAT_OFFSET_CHECK_NOT_NULL(
return ElementOffset());
502 if (i >= element_count_) {
503 return ElementOffset();
513 RTI_FLAT_OFFSET_CHECK_NOT_NULL(
return 0);
515 return element_count_;
519 unsigned int element_count_;
522 template <
typename E>
523 struct SequenceOffsetHelper {
525 static offset_t calculate_serialized_size(
526 rti::flat::SampleBase *sample,
527 offset_t absolute_offset,
533 SequenceOffset<E> tmp(
536 max_offset - absolute_offset);
537 unsigned int count = tmp.element_count();
538 typename SequenceOffset<E>::iterator it = tmp.begin();
539 for (
unsigned int i = 0; i < count; i++) {
545 return detail::ptrdiff(it.get_position(), sample->get_buffer())
564 template <
typename ElementOffset,
unsigned int N>
567 typedef variable_size_type_tag_t offset_kind;
574 rti::flat::SampleBase *sample,
576 offset_t sequence_size)
593 return ElementOffset();
619 template <
typename ElementOffset,
unsigned int N>
622 typedef fixed_size_type_tag_t offset_kind;
631 rti::flat::SampleBase *sample,
632 offset_t absolute_offset,
633 offset_t first_element_size,
634 offset_t element_size)
638 first_element_size + element_size * (N - 1)),
639 first_element_size_(first_element_size),
640 element_size_(element_size)
652 RTI_FLAT_OFFSET_CHECK_NOT_NULL(
return ElementOffset());
655 return ElementOffset();
658 offset_t element_offset = this->absolute_offset_;
660 element_offset += this->first_element_size_
661 + (i - 1) * this->element_size_;
664 return ElementOffset(this->sample_, element_offset);
668 offset_t first_element_size_;
669 offset_t element_size_;
712 template <
typename ElementOffset,
unsigned int N>
715 typedef fixed_size_type_tag_t offset_kind;
724 rti::flat::SampleBase *sample,
725 offset_t absolute_offset)
735 static offset_t serialized_size(offset_t)
738 return ElementOffset::serialized_size_w_padding() * (N - 1)
739 + ElementOffset::serialized_size(0);
753 RTI_FLAT_OFFSET_CHECK_NOT_NULL(
return ElementOffset());
756 return ElementOffset();
765 #endif // RTI_DDS_FLAT_SEQUENCEOFFSETS_HPP_