11#ifndef RTI_DDS_FLAT_SEQUENCEBUILDERS_HPP_
12#define RTI_DDS_FLAT_SEQUENCEBUILDERS_HPP_
14#include "rti/flat/Builder.hpp"
37namespace 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);
106 if (!stream().align(ElementOffset::required_alignment)) {
107 RTI_FLAT_BUILDER_OUT_OF_RESOURCES_ERROR(
return);
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_;
227template <
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();
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());
403template <
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(\
486template <
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(\
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 (RTIXCdrUnsignedLong_MAX /
static_cast<unsigned int>(
sizeof(T))
603 RTI_FLAT_BUILDER_OUT_OF_RESOURCES_ERROR(
return *
this);
606 if (!stream().check_size(
static_cast<unsigned int>(
sizeof(T)) * count)) {
607 RTI_FLAT_BUILDER_OUT_OF_RESOURCES_ERROR(
return *
this);
610 if (stream().needs_byte_swap() &&
sizeof(T) > 1) {
611 for (
unsigned int i = 0; i < count; i++) {
612 stream().template serialize_fast<T>(array[i]);
615 stream().serialize_fast((
void *) array, count *
static_cast<unsigned int>(
sizeof(T)));
618 element_count_ += count;
637 RTI_FLAT_BUILDER_CHECK_VALID(
return *
this);
639 if (RTIXCdrUnsignedLong_MAX /
static_cast<unsigned int>(
sizeof(T))
641 RTI_FLAT_BUILDER_OUT_OF_RESOURCES_ERROR(
return *
this);
644 if (!stream().check_size(
static_cast<unsigned int>(
sizeof(T)) * count)) {
645 RTI_FLAT_BUILDER_OUT_OF_RESOURCES_ERROR(
return *
this);
648 for (
unsigned int i = 0; i < count; i++) {
649 stream().template serialize_fast<T>(value);
651 element_count_ += count;
677 RTI_FLAT_BUILDER_CHECK_VALID(
return *
this);
679 if (RTIXCdrUnsignedLong_MAX /
static_cast<unsigned int>(
sizeof(T))
681 RTI_FLAT_BUILDER_OUT_OF_RESOURCES_ERROR(
return *
this);
684 if (!stream().skip(
static_cast<unsigned int>(
sizeof(T)) * count)) {
685 RTI_FLAT_BUILDER_OUT_OF_RESOURCES_ERROR(
return *
this);
688 element_count_ += count;
701 return finish_impl<Offset>();
705 RTI_FLAT_BUILDER_DEFINE_MOVE_OPERATIONS_IMPL(\
738 unsigned int alignment = RTI_XCDR_SEQ_LENGTH_ALIGNMENT)
749 RTI_FLAT_BUILDER_CHECK_VALID(
return *
this);
753 if (element_count_ != 0) {
754 stream().current_position(begin_position());
755 stream().serialize_fast<rti::xcdr::length_t>(0);
759 unsigned int length =
static_cast<unsigned int>(strlen(value)) + 1;
760 add_n(value, length);
772 RTI_FLAT_BUILDER_CHECK_VALID(
return Offset());
774 if (element_count_ == 0) {
777 return finish_impl<Offset>();
782 RTI_FLAT_BUILDER_DEFINE_MOVE_OPERATIONS_IMPL(\
787typedef PrimitiveSequenceBuilder<unsigned char> WStringBuilder;
Base class of all Builders.
Definition: Builder.hpp:512
bool is_valid() const
Whether this Builder is valid.
Definition: Builder.hpp:891
bool is_nested() const
Returns whether this is a member Builder.
Definition: Builder.hpp:874
Base class of all array and sequence builders.
Definition: SequenceBuilders.hpp:52
unsigned int element_count() const
Returns the current number of elements that have been added.
Definition: SequenceBuilders.hpp:122
Base class of Builders for sequence members.
Definition: SequenceBuilders.hpp:304
Builds a sequence member of fixed-size elements.
Definition: SequenceBuilders.hpp:487
Offset finish()
Finishes building the sequence.
Definition: SequenceBuilders.hpp:537
FinalSequenceBuilder & add_n(unsigned int count)
Adds a number of elements at once.
Definition: SequenceBuilders.hpp:525
ElementOffset add_next()
Adds the next element.
Definition: SequenceBuilders.hpp:512
Builds an array member of variable-size elements.
Definition: SequenceBuilders.hpp:228
ElementBuilder build_next()
Begins building the next element.
Definition: SequenceBuilders.hpp:257
Offset finish()
Finishes building the array.
Definition: SequenceBuilders.hpp:275
MutableArrayOffset< typename ElementBuilder::Offset, N > Offset
The related Offset type.
Definition: SequenceBuilders.hpp:233
Offset to an array of variable-size elements.
Definition: SequenceOffsets.hpp:582
Builds a sequence member of variable-size elements.
Definition: SequenceBuilders.hpp:404
SequenceOffset< typename ElementBuilder::Offset > Offset
The related Offset type.
Definition: SequenceBuilders.hpp:409
ElementBuilder build_next()
Begins building the next element.
Definition: SequenceBuilders.hpp:433
Offset finish()
Finishes building the sequence.
Definition: SequenceBuilders.hpp:445
Builds a sequence of primitive members.
Definition: SequenceBuilders.hpp:554
PrimitiveSequenceBuilder & add_n(unsigned int count, T value)
Adds a number of elements with the same value.
Definition: SequenceBuilders.hpp:635
PrimitiveSequenceBuilder & add_n(const T *array, unsigned int count)
Adds all the elements in an array.
Definition: SequenceBuilders.hpp:597
PrimitiveSequenceBuilder & add_n(unsigned int count)
Adds a number of uninitialized elements.
Definition: SequenceBuilders.hpp:675
PrimitiveSequenceBuilder & add_next(T value)
Adds the next element.
Definition: SequenceBuilders.hpp:579
Offset finish()
Finishes building the sequence.
Definition: SequenceBuilders.hpp:699
Offset to a sequence of primitive elements.
Definition: SequenceOffsets.hpp:134
Offset to a sequence of non-primitive elements.
Definition: SequenceOffsets.hpp:473
Builds a string.
Definition: SequenceBuilders.hpp:724
Offset finish()
Finishes building the string.
Definition: SequenceBuilders.hpp:770
StringBuilder & set_string(const char *value)
Sets the string value.
Definition: SequenceBuilders.hpp:747
Offset to a string.
Definition: SequenceOffsets.hpp:263
The RTI namespace.
Definition: AggregationBuilders.hpp:17