11#ifndef RTI_DDS_FLAT_BUILDER_HPP_
12#define RTI_DDS_FLAT_BUILDER_HPP_
14#include "xcdr/xcdr_stream.h"
15#include "xcdr/xcdr_stream_impl.h"
16#include "xcdr/xcdr_interpreter.h"
17#include "xcdr/xcdr_interpreter.h"
19#include "rti/xcdr/Stream.hpp"
20#include "rti/flat/ExceptionHelper.hpp"
21#include "rti/flat/SequenceOffsets.hpp"
124#ifdef DOXYGEN_DOCUMENTATION_ONLY
243 unsigned char *buffer,
245 bool initialize_members =
false)
429namespace rti {
namespace flat {
436#if defined(RTI_FLAT_DATA_CXX11_RVALUE_REFERENCES)
437#define RTI_FLAT_BUILDER_DEFINE_MOVE_OPERATIONS_IMPL(TYPE, BASE, PROXY)
438#define RTI_FLAT_BUILDER_DEFINE_MOVE_OPERATIONS(TYPE, BASE)
439#define RTI_FLAT_MOVE_BUILDER(BUILDER) BUILDER
441#define RTI_FLAT_BUILDER_DEFINE_MOVE_OPERATIONS_IMPL(TYPE, BASE, PROXY) \
443 TYPE(PROXY other) throw() \
445 this->move_from(other); \
447 TYPE& operator=(PROXY other) throw() \
449 this->finish_untyped_impl(); \
450 this->move_from(other); \
455 return TYPE(PROXY(*this)); \
459 TYPE& operator=(TYPE&);
462#define RTI_FLAT_BUILDER_DEFINE_MOVE_OPERATIONS(TYPE, BASE) \
463 RTI_FLAT_BUILDER_DEFINE_MOVE_OPERATIONS_IMPL( \
464 TYPE, BASE, UntypedAggregationBuilderMoveProxy)
466#define RTI_FLAT_MOVE_BUILDER(BUILDER) (BUILDER).move()
471 template <
typename Builder1,
typename Builder2>
472 void move_abstract_builder(Builder1& to, Builder2& from)
474 if (from.parent_builder_ == NULL) {
475 to.owned_stream_ = from.owned_stream_;
478 to.parent_stream_ = from.parent_stream_;
479 to.parent_builder_ = from.parent_builder_;
480 to.begin_position_ = from.begin_position_;
481 to.bind_position_ = from.bind_position_;
482 to.initialize_on_add_ = from.initialize_on_add_;
483#ifdef RTI_FLAT_DATA_NO_EXCEPTIONS
484 to.failure_ = from.failure_;
487 from.parent_stream_ = NULL;
488 from.parent_builder_ = NULL;
489 from.bind_position_ = NULL;
490 from.begin_position_ = NULL;
515 : parent_stream_(NULL),
516 parent_builder_(NULL),
517 begin_position_(NULL),
518 bind_position_(NULL),
519 initialize_on_add_(
false)
520#ifdef RTI_FLAT_DATA_NO_EXCEPTIONS
529 AbstractBuilder(
unsigned char *buffer, offset_t size,
bool initialize_members)
530 : parent_stream_(NULL),
531 parent_builder_(NULL),
532 bind_position_(NULL),
533 initialize_on_add_(initialize_members)
534#ifdef RTI_FLAT_DATA_NO_EXCEPTIONS
540 if (!RTIXCdrFlatSample_initializeEncapsulationAndStream(
542 &owned_stream_.c_stream(),
543 RTIXCdrEncapsulationId_getNativePlCdr2(),
545 RTI_FLAT_BUILDER_OUT_OF_RESOURCES_ERROR(
return);
548 begin_position_ = owned_stream_.current_position();
552 template <
typename Builder1,
typename Builder2>
553 friend void detail::move_abstract_builder(Builder1& to, Builder2& from);
555#if defined(RTI_FLAT_DATA_CXX11_RVALUE_REFERENCES)
558 detail::move_abstract_builder(*
this, other);
563 if (
this == &other) {
567 finish_untyped_impl();
569 detail::move_abstract_builder(*
this, other);
575 struct AbstractBuilderMoveProxy {
576 rti::xcdr::Stream owned_stream_;
577 rti::xcdr::Stream *parent_stream_;
579 unsigned char *begin_position_;
580 unsigned char *bind_position_;
581 bool initialize_on_add_;
582#ifdef RTI_FLAT_DATA_NO_EXCEPTIONS
587 void move_from(AbstractBuilderMoveProxy& other)
589 detail::move_abstract_builder(*
this, other);
592 void move_to(AbstractBuilderMoveProxy& other)
594 detail::move_abstract_builder(other, *
this);
597 operator AbstractBuilderMoveProxy ()
throw()
599 AbstractBuilderMoveProxy other;
622 finish_untyped_impl();
625 struct nested_tag_t {};
644 AbstractBuilder& parent,
645 unsigned int alignment)
647 parent_stream_(&parent.stream()),
648 parent_builder_(&parent),
649 begin_position_(NULL),
650 bind_position_(NULL),
651 initialize_on_add_(parent.initialize_on_add_)
652#ifdef RTI_FLAT_DATA_NO_EXCEPTIONS
656 if (alignment != 0) {
657 if (!stream().align(alignment)) {
658 RTI_FLAT_BUILDER_OUT_OF_RESOURCES_ERROR(
return);
661 begin_position_ = stream().current_position();
664 unsigned char * buffer()
666 return stream().buffer();
669 unsigned char * begin_position()
671 return begin_position_;
680 template <
typename OffsetType>
681 OffsetType add_element()
683 RTI_FLAT_BUILDER_CHECK_VALID(
return OffsetType());
684 RTI_FLAT_BUILDER_CHECK_NOT_BOUND(
return OffsetType());
686 offset_t member_size = OffsetType::serialized_size(0);
687 unsigned char *pos = stream().current_position();
688 if (detail::is_fixed_type_w_dheader<OffsetType>::value()) {
689 pos = (
unsigned char *) stream().serialize_dheader();
691 RTI_FLAT_BUILDER_OUT_OF_RESOURCES_ERROR(
return OffsetType());
694 if (!stream().skip(member_size)) {
695 RTI_FLAT_BUILDER_OUT_OF_RESOURCES_ERROR(
return OffsetType());
697 if (detail::is_fixed_type_w_dheader<OffsetType>::value()) {
698 stream().finish_dheader((
char *) pos);
701 if (initialize_on_add_) {
703 (SampleBase *) stream().buffer(),
704 detail::ptrdiff(pos, stream().buffer()));
706 detail::final_offset_initializer<OffsetType>::initialize(offset);
711 (SampleBase *) stream().buffer(),
712 detail::ptrdiff(pos, stream().buffer()));
734 template <
typename NestedBuilder>
735 NestedBuilder build_element(rti::xcdr::Stream::Memento& stream_memento)
737 RTI_FLAT_BUILDER_CHECK_VALID(
return NestedBuilder());
738 RTI_FLAT_BUILDER_CHECK_NOT_BOUND(
return NestedBuilder());
740 NestedBuilder nested_builder(nested_tag_t(), *
this);
741 RTI_FLAT_BUILDER_CHECK_CREATE_BUILDER(
743 return NestedBuilder());
745 bind_position_ = stream_memento.discard();
751 return RTI_FLAT_MOVE_BUILDER(nested_builder);
754 template <
typename NestedBuilder>
755 NestedBuilder build_element_no_align(rti::xcdr::Stream::Memento& stream_memento)
757 RTI_FLAT_BUILDER_CHECK_VALID(
return NestedBuilder());
758 RTI_FLAT_BUILDER_CHECK_NOT_BOUND(
return NestedBuilder());
762 NestedBuilder nested_builder(nested_tag_t(), *
this, 0);
763 RTI_FLAT_BUILDER_CHECK_CREATE_BUILDER(
765 return NestedBuilder());
767 bind_position_ = stream_memento.discard();
773 return RTI_FLAT_MOVE_BUILDER(nested_builder);
787 unsigned int current_size()
789 return detail::ptrdiff(stream().current_position(), begin_position_);
793 friend class AggregationBuilder;
799 void finish_untyped_impl()
805 parent_builder_->finish_member();
833 template <
typename OffsetType>
834 OffsetType finish_impl()
836 RTI_FLAT_BUILDER_CHECK_VALID(
return OffsetType());
838 unsigned char *begin_pos = this->begin_position_;
839 unsigned char *sample_base = stream().buffer();
840 unsigned char *current_pos = stream().current_position();
841 finish_untyped_impl();
844 reinterpret_cast<SampleBase *
>(sample_base),
845 detail::ptrdiff(begin_pos, sample_base),
846 detail::ptrdiff(current_pos, begin_pos));
865 RTI_FLAT_BUILDER_CHECK_VALID(
return);
866 RTI_FLAT_BUILDER_CHECK_CAN_FINISH(
return);
868 parent_builder_->discard_member();
884 return parent_builder_ != NULL;
901 return begin_position_ != NULL;
914 return stream().total_size();
923 parent_stream_ = NULL;
924 parent_builder_ = NULL;
925 bind_position_ = NULL;
926 begin_position_ = NULL;
931 virtual void finish_member()
937 bind_position_ = NULL;
940 void discard_member()
942 RTI_FLAT_BUILDER_CHECK_VALID(
return);
944 stream().current_position(bind_position_);
945 bind_position_ = NULL;
948 rti::xcdr::Stream& stream()
950 if (parent_stream_ != NULL) {
951 return *parent_stream_;
953 return owned_stream_;
957 const rti::xcdr::Stream& stream()
const
959 if (parent_stream_ != NULL) {
960 return *parent_stream_;
962 return owned_stream_;
967 rti::xcdr::Stream owned_stream_;
968 rti::xcdr::Stream *parent_stream_;
969 AbstractBuilder *parent_builder_;
970 unsigned char *begin_position_;
971 unsigned char *bind_position_;
972 bool initialize_on_add_;
974#ifdef RTI_FLAT_DATA_NO_EXCEPTIONS
988 bool failure = failure_;
Represents the Offset to an arbitrary user-defined FlatData final IDL struct.
Definition: Offset.hpp:125
Represents the Builder for an arbitrary user-defined mutable type.
Definition: Builder.hpp:206
Offset finish()
Finishes building a member.
MyFlatMutableBuilder(unsigned char *buffer, int32_t size, bool initialize_members=false)
Construct a Builder with an arbitrary buffer.
Definition: Builder.hpp:242
rti::flat::FinalAlignedArrayOffset< MyFlatFinalOffset, 10 > add_my_final_array()
Adds an array member of final complex elements.
rti::flat::StringBuilder build_my_string()
Begins building a string member.
MyFlatMutable * finish_sample()
Finishes building a sample.
rti::flat::MutableArrayBuilder< FlatMutableBarBuilder, 10 > build_my_mutable_array()
Begins building an array member of mutable complex elements.
MyFlatMutableBuilder()
Creates an invalid Builder.
Definition: Builder.hpp:222
rti::flat::MutableSequenceBuilder< FlatMutableBarBuilder > build_my_mutable_seq()
Begins building a sequence member of mutable complex elements.
FlatMutableBarBuilder build_my_mutable()
Begins building a mutable complex member.
rti::flat::MutableSequenceBuilder< rti::flat::StringBuilder > build_my_string_seq()
Begins building a sequence member of string elements.
rti::flat::PrimitiveArrayOffset< int32_t, 10 > add_my_primitive_array()
Adds a primitive array member.
MyFlatFinalOffset add_my_final()
Adds a final complex member.
rti::flat::PrimitiveSequenceBuilder< int32_t > build_my_primitive_seq()
Begins building a primitive-sequence member.
bool add_my_optional_primitive(int32_t value)
Adds a primitive member.
MyFlatMutableOffset Offset
The related offset type.
Definition: Builder.hpp:211
bool add_my_primitive(int32_t value)
Adds a primitive member.
rti::flat::FinalSequenceBuilder< MyFlatFinalOffset > build_my_final_seq()
Begins building a sequence member of final complex elements.
Represents the Offset to an arbitrary user-defined FlatData mutable IDL struct.
Definition: Offset.hpp:203
Represents the Builder for an arbitrary user-defined mutable IDL union.
Definition: Builder.hpp:361
MyFlatMutableBuilder build_my_mutable(int32_t discriminator=1)
Builds a mutable-struct member.
Offset finish()
Finishes building a member.
MyFlatUnion * finish_sample()
Finishes building a sample.
MyFlatUnionBuilder()
Creates an invalid Builder.
Definition: Builder.hpp:378
MyFlatFinal::Offset add_my_final()
Adds a final-struct member.
bool add_my_primitive(int32_t value)
Adds a primitive member.
MyFlatUnionOffset Offset
The related offset type.
Definition: Builder.hpp:366
Represents the Offset to an arbitrary user-defined FlatData mutable IDL union.
Definition: Offset.hpp:376
Base class of all Builders.
Definition: Builder.hpp:512
bool is_valid() const
Whether this Builder is valid.
Definition: Builder.hpp:899
bool is_nested() const
Returns whether this is a member Builder.
Definition: Builder.hpp:882
rti::xcdr::length_t capacity() const
Returns the total capacity in bytes.
Definition: Builder.hpp:912
bool check_failure()
Checks if the previous operation failed and resets the failure flag.
Definition: Builder.hpp:986
void discard()
Discards a member in process of being built.
Definition: Builder.hpp:863
virtual ~AbstractBuilder()
If this is a member Builder, it calls finish().
Definition: Builder.hpp:620
Base class of struct and union builders.
Definition: AggregationBuilders.hpp:37
Offset to an array of final elements.
Definition: SequenceOffsets.hpp:776
Builds a sequence member of fixed-size elements.
Definition: SequenceBuilders.hpp:623
Builds an array member of variable-size elements.
Definition: SequenceBuilders.hpp:305
Builds a sequence member of variable-size elements.
Definition: SequenceBuilders.hpp:535
Offset to an array of primitive elements.
Definition: SequenceOffsets.hpp:241
Builds a sequence of primitive members.
Definition: SequenceBuilders.hpp:692
The generic definition of FlatData topic-types.
Definition: FlatSample.hpp:148
OffsetType Offset
The related Offset type.
Definition: FlatSample.hpp:155
Builds a string.
Definition: SequenceBuilders.hpp:872
Base class of builders for user-defined mutable unions.
Definition: AggregationBuilders.hpp:335
The RTI namespace.
Definition: AggregationBuilders.hpp:17