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)
279 bool add_my_primitive(int32_t value);
284 bool add_my_optional_primitive(int32_t value);
322 FlatMutableBarBuilder build_my_mutable();
429 namespace 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();
738 return RTI_FLAT_MOVE_BUILDER(nested_builder);
741 template <
typename NestedBuilder>
742 NestedBuilder build_element_no_align(rti::xcdr::Stream::Memento& stream_memento)
744 RTI_FLAT_BUILDER_CHECK_VALID(
return NestedBuilder());
745 RTI_FLAT_BUILDER_CHECK_NOT_BOUND(
return NestedBuilder());
749 NestedBuilder nested_builder(nested_tag_t(), *
this, 0);
750 RTI_FLAT_BUILDER_CHECK_CREATE_BUILDER(
752 return NestedBuilder());
754 bind_position_ = stream_memento.discard();
755 return RTI_FLAT_MOVE_BUILDER(nested_builder);
769 unsigned int current_size()
771 return detail::ptrdiff(stream().current_position(), begin_position_);
775 friend class AggregationBuilder;
781 void finish_untyped_impl()
787 parent_builder_->finish_member();
815 template <
typename OffsetType>
816 OffsetType finish_impl()
818 RTI_FLAT_BUILDER_CHECK_VALID(
return OffsetType());
820 unsigned char *begin_pos = this->begin_position_;
821 unsigned char *sample_base = stream().buffer();
822 unsigned char *current_pos = stream().current_position();
823 finish_untyped_impl();
826 (SampleBase *) sample_base,
827 detail::ptrdiff(begin_pos, sample_base),
828 detail::ptrdiff(current_pos, begin_pos));
847 RTI_FLAT_BUILDER_CHECK_VALID(
return);
848 RTI_FLAT_BUILDER_CHECK_CAN_FINISH(
return);
850 parent_builder_->discard_member();
866 return parent_builder_ != NULL;
883 return begin_position_ != NULL;
896 return stream().total_size();
905 parent_stream_ = NULL;
906 parent_builder_ = NULL;
907 bind_position_ = NULL;
908 begin_position_ = NULL;
913 virtual void finish_member()
919 bind_position_ = NULL;
922 void discard_member()
924 RTI_FLAT_BUILDER_CHECK_VALID(
return);
926 stream().current_position(bind_position_);
927 bind_position_ = NULL;
930 rti::xcdr::Stream& stream()
932 if (parent_stream_ != NULL) {
933 return *parent_stream_;
935 return owned_stream_;
939 const rti::xcdr::Stream& stream()
const
941 if (parent_stream_ != NULL) {
942 return *parent_stream_;
944 return owned_stream_;
949 rti::xcdr::Stream owned_stream_;
950 rti::xcdr::Stream *parent_stream_;
951 AbstractBuilder *parent_builder_;
952 unsigned char *begin_position_;
953 unsigned char *bind_position_;
954 bool initialize_on_add_;
956 #ifdef RTI_FLAT_DATA_NO_EXCEPTIONS
970 bool failure = failure_;
988 #endif // RTI_DDS_FLAT_BUILDER_HPP_