11 #ifndef RTI_DDS_FLAT_SEQUENCEBUILDERS_HPP_
12 #define RTI_DDS_FLAT_SEQUENCEBUILDERS_HPP_
14 #include "rti/flat/Builder.hpp"
37 namespace rti {
namespace flat {
61 unsigned int alignment)
66 template <
typename ElementBuilder>
67 ElementBuilder build_next()
69 rti::xcdr::Stream::Memento stream_memento(stream());
70 return build_element<ElementBuilder>(stream_memento);
73 template <
typename ElementOffset>
74 ElementOffset add_next()
76 RTI_FLAT_BUILDER_CHECK_VALID(
return ElementOffset());
79 rti::xcdr::Stream::Memento stream_memento(stream());
81 if (!stream().align(ElementOffset::required_alignment)) {
82 RTI_FLAT_BUILDER_OUT_OF_RESOURCES_ERROR(
return ElementOffset());
85 ElementOffset element = add_element<ElementOffset>();
86 if (element.is_null()) {
87 return ElementOffset();
91 stream_memento.discard();
97 template <
typename ElementOffset>
98 void add_n(
unsigned int count)
100 RTI_FLAT_BUILDER_CHECK_VALID(
return ElementOffset());
106 if (!stream().align(ElementOffset::required_alignment)) {
107 RTI_FLAT_BUILDER_OUT_OF_RESOURCES_ERROR(
return ElementOffset());
110 offset_t elements_size = ElementOffset::serialized_size(0)
111 + ElementOffset::serialized_size_w_padding() * (count - 1);
112 if (!stream().skip(elements_size)) {
113 RTI_FLAT_BUILDER_OUT_OF_RESOURCES_ERROR(
return);
116 element_count_ += count;
124 return element_count_;
127 #if defined(RTI_FLAT_DATA_CXX11_RVALUE_REFERENCES)
131 if (
this == &other) {
135 finish_untyped_impl();
137 AbstractBuilder::operator=(static_cast<AbstractBuilder&&>(other));
139 element_count_ = other.element_count_;
140 other.element_count_ = 0;
147 struct AbstractListBuilderMoveProxy : AbstractBuilderMoveProxy {
148 unsigned int element_count_;
151 operator AbstractListBuilderMoveProxy () throw()
153 AbstractListBuilderMoveProxy other;
159 void move_from(AbstractListBuilderMoveProxy& other)
161 AbstractBuilder::move_from(other);
162 element_count_ = other.element_count_;
165 void move_to(AbstractListBuilderMoveProxy& other)
167 AbstractBuilder::move_to(other);
168 other.element_count_ = element_count_;
174 friend class AbstractBuilder;
176 virtual void finish_member()
178 RTI_FLAT_BUILDER_CHECK_VALID(
return);
184 AbstractBuilder::finish_member();
188 unsigned int element_count_;
227 template <
typename ElementBuilder,
unsigned int N>
245 unsigned int alignment = 0)
260 RTI_FLAT_BUILDER_PRECONDITION_ERROR(
261 "Array builder build_next: too many elements",
262 return ElementBuilder());
265 return AbstractListBuilder::build_next<ElementBuilder>();
277 RTI_FLAT_BUILDER_CHECK_CAN_FINISH(
return Offset());
280 RTI_FLAT_BUILDER_PRECONDITION_ERROR(
281 "Cannot finish array builder: too few elements",
285 return finish_impl<Offset>();
288 RTI_FLAT_BUILDER_DEFINE_MOVE_OPERATIONS_IMPL(\
313 unsigned int alignment)
317 if (!stream().check_size(
sizeof(rti::xcdr::length_t))) {
318 RTI_FLAT_BUILDER_OUT_OF_RESOURCES_ERROR(invalidate());
322 stream().serialize_fast<rti::xcdr::length_t>(0);
330 finish_untyped_impl();
333 #if defined(RTI_FLAT_DATA_CXX11_RVALUE_REFERENCES)
337 finish_untyped_impl();
338 AbstractListBuilder::operator=(static_cast<AbstractListBuilder&&>(other));
344 void finish_untyped_impl()
351 AbstractListBuilder::finish_untyped_impl();
355 template <
typename Offset>
358 RTI_FLAT_BUILDER_CHECK_VALID(
return Offset());
359 RTI_FLAT_BUILDER_CHECK_CAN_FINISH(
return Offset());
362 return AbstractListBuilder::finish_impl<Offset>();
369 rti::xcdr::Stream::Memento stream_memento(stream());
370 stream().current_position(begin_position());
371 stream().serialize_fast<rti::xcdr::length_t>(
element_count());
403 template <
typename ElementBuilder>
421 unsigned int alignment = RTI_XCDR_DHEADER_ALIGNMENT)
435 return AbstractListBuilder::build_next<ElementBuilder>();
447 return finish_impl<Offset>();
450 RTI_FLAT_BUILDER_DEFINE_MOVE_OPERATIONS_IMPL(\
486 template <
typename ElementOffset>
501 unsigned int alignment = RTI_XCDR_SEQ_LENGTH_ALIGNMENT)
514 return AbstractListBuilder::add_next<ElementOffset>();
527 AbstractListBuilder::add_n<ElementOffset>(count);
539 return finish_impl<Offset>();
542 RTI_FLAT_BUILDER_DEFINE_MOVE_OPERATIONS_IMPL(\
553 template <
typename T>
568 unsigned int alignment = RTI_XCDR_SEQ_LENGTH_ALIGNMENT)
581 RTI_FLAT_BUILDER_CHECK_VALID(
return *
this);
583 if (!stream().
template serialize<T>(value)) {
584 RTI_FLAT_BUILDER_OUT_OF_RESOURCES_ERROR(
return *
this);
599 RTI_FLAT_BUILDER_CHECK_VALID(
return *
this);
601 if (!stream().check_size(static_cast<unsigned int>(
sizeof(T)) * count)) {
602 RTI_FLAT_BUILDER_OUT_OF_RESOURCES_ERROR(
return *
this);
605 if (stream().needs_byte_swap() &&
sizeof(T) > 1) {
606 for (
unsigned int i = 0; i < count; i++) {
607 stream().template serialize_fast<T>(array[i]);
610 stream().serialize_fast((
void *) array, count * static_cast<unsigned int>(
sizeof(T)));
613 element_count_ += count;
625 RTI_FLAT_BUILDER_CHECK_VALID(
return *
this);
627 if (!stream().check_size(
sizeof(T) * count)) {
628 RTI_FLAT_BUILDER_OUT_OF_RESOURCES_ERROR(
return *
this);
631 for (
unsigned int i = 0; i < count; i++) {
632 stream().template serialize_fast<T>(value);
634 element_count_ += count;
646 return finish_impl<Offset>();
650 RTI_FLAT_BUILDER_DEFINE_MOVE_OPERATIONS_IMPL(\
683 unsigned int alignment = RTI_XCDR_SEQ_LENGTH_ALIGNMENT)
694 RTI_FLAT_BUILDER_CHECK_VALID(
return *
this);
698 if (element_count_ != 0) {
699 stream().current_position(begin_position());
700 stream().serialize_fast<rti::xcdr::length_t>(0);
704 unsigned int length =
static_cast<unsigned int>(strlen(value)) + 1;
705 add_n(value, length);
717 RTI_FLAT_BUILDER_CHECK_VALID(
return Offset());
719 if (element_count_ == 0) {
722 return finish_impl<Offset>();
727 RTI_FLAT_BUILDER_DEFINE_MOVE_OPERATIONS_IMPL(\
732 typedef PrimitiveSequenceBuilder<unsigned char> WStringBuilder;
736 #endif // RTI_DDS_FLAT_SEQUENCEBUILDERS_HPP_