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() \
447 TYPE& operator=(PROXY other) throw() \
449 finish_untyped_impl(); \
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();
689 if (!stream().skip(member_size)) {
690 RTI_FLAT_BUILDER_OUT_OF_RESOURCES_ERROR(
return OffsetType());
693 if (initialize_on_add_) {
695 (SampleBase *) stream().buffer(),
696 detail::ptrdiff(pos, stream().buffer()));
698 detail::final_offset_initializer<OffsetType>::initialize(offset);
703 (SampleBase *) stream().buffer(),
704 detail::ptrdiff(pos, stream().buffer()));
726 template <
typename NestedBuilder>
727 NestedBuilder build_element(rti::xcdr::Stream::Memento& stream_memento)
729 RTI_FLAT_BUILDER_CHECK_VALID(
return NestedBuilder());
730 RTI_FLAT_BUILDER_CHECK_NOT_BOUND(
return NestedBuilder());
732 NestedBuilder nested_builder(nested_tag_t(), *
this);
733 RTI_FLAT_BUILDER_CHECK_CREATE_BUILDER(
735 return NestedBuilder());
737 bind_position_ = stream_memento.discard();
743 return RTI_FLAT_MOVE_BUILDER(nested_builder);
746 template <
typename NestedBuilder>
747 NestedBuilder build_element_no_align(rti::xcdr::Stream::Memento& stream_memento)
749 RTI_FLAT_BUILDER_CHECK_VALID(
return NestedBuilder());
750 RTI_FLAT_BUILDER_CHECK_NOT_BOUND(
return NestedBuilder());
754 NestedBuilder nested_builder(nested_tag_t(), *
this, 0);
755 RTI_FLAT_BUILDER_CHECK_CREATE_BUILDER(
757 return NestedBuilder());
759 bind_position_ = stream_memento.discard();
765 return RTI_FLAT_MOVE_BUILDER(nested_builder);
779 unsigned int current_size()
781 return detail::ptrdiff(stream().current_position(), begin_position_);
785 friend class AggregationBuilder;
791 void finish_untyped_impl()
797 parent_builder_->finish_member();
825 template <
typename OffsetType>
826 OffsetType finish_impl()
828 RTI_FLAT_BUILDER_CHECK_VALID(
return OffsetType());
830 unsigned char *begin_pos = this->begin_position_;
831 unsigned char *sample_base = stream().buffer();
832 unsigned char *current_pos = stream().current_position();
833 finish_untyped_impl();
836 reinterpret_cast<SampleBase *
>(sample_base),
837 detail::ptrdiff(begin_pos, sample_base),
838 detail::ptrdiff(current_pos, begin_pos));
857 RTI_FLAT_BUILDER_CHECK_VALID(
return);
858 RTI_FLAT_BUILDER_CHECK_CAN_FINISH(
return);
860 parent_builder_->discard_member();
876 return parent_builder_ != NULL;
893 return begin_position_ != NULL;
906 return stream().total_size();
915 parent_stream_ = NULL;
916 parent_builder_ = NULL;
917 bind_position_ = NULL;
918 begin_position_ = NULL;
923 virtual void finish_member()
929 bind_position_ = NULL;
932 void discard_member()
934 RTI_FLAT_BUILDER_CHECK_VALID(
return);
936 stream().current_position(bind_position_);
937 bind_position_ = NULL;
940 rti::xcdr::Stream& stream()
942 if (parent_stream_ != NULL) {
943 return *parent_stream_;
945 return owned_stream_;
949 const rti::xcdr::Stream& stream()
const
951 if (parent_stream_ != NULL) {
952 return *parent_stream_;
954 return owned_stream_;
959 rti::xcdr::Stream owned_stream_;
960 rti::xcdr::Stream *parent_stream_;
961 AbstractBuilder *parent_builder_;
962 unsigned char *begin_position_;
963 unsigned char *bind_position_;
964 bool initialize_on_add_;
966#ifdef RTI_FLAT_DATA_NO_EXCEPTIONS
980 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:891
bool is_nested() const
Returns whether this is a member Builder.
Definition: Builder.hpp:874
rti::xcdr::length_t capacity() const
Returns the total capacity in bytes.
Definition: Builder.hpp:904
bool check_failure()
Checks if the previous operation failed and resets the failure flag.
Definition: Builder.hpp:978
void discard()
Discards a member in process of being built.
Definition: Builder.hpp:855
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:728
Builds a sequence member of fixed-size elements.
Definition: SequenceBuilders.hpp:487
Builds an array member of variable-size elements.
Definition: SequenceBuilders.hpp:228
Builds a sequence member of variable-size elements.
Definition: SequenceBuilders.hpp:404
Offset to an array of primitive elements.
Definition: SequenceOffsets.hpp:220
Builds a sequence of primitive members.
Definition: SequenceBuilders.hpp:554
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:724
Base class of builders for user-defined mutable unions.
Definition: AggregationBuilders.hpp:333
The RTI namespace.
Definition: AggregationBuilders.hpp:17