11 #ifndef RTI_DDS_FLAT_AGGREGATIONBUILDERS_HPP_
12 #define RTI_DDS_FLAT_AGGREGATIONBUILDERS_HPP_
14 #include "rti/flat/BuilderHelper.hpp"
15 #include "rti/flat/Builder.hpp"
17 namespace rti {
namespace flat {
40 dheader_position_(NULL),
41 emheader_position_(NULL)
47 unsigned char *initial_buffer,
49 bool initialize_members) :
51 dheader_position_(NULL),
52 emheader_position_(NULL)
54 dheader_position_ = stream().serialize_dheader();
55 if (dheader_position_ == NULL) {
56 RTI_FLAT_BUILDER_OUT_OF_RESOURCES_ERROR(invalidate());
67 void set_encapsulation_impl(RTIXCdrEncapsulationId encapsulation_id)
70 RTI_FLAT_CHECK_PRECONDITION(!
is_nested(),
return);
72 char *current_position = (
char *) owned_stream_.current_position();
75 RTI_FLAT_CHECK_PRECONDITION(
76 current_position == dheader_position_ + RTI_XCDR_DHEADER_SIZE,
79 owned_stream_.skip_back(
80 RTI_XCDR_DHEADER_SIZE + RTI_XCDR_ENCAPSULATION_HEADER_SIZE);
82 if (!RTIXCdrFlatSample_initializeEncapsulationAndStream(
83 (
char *) owned_stream_.buffer(),
84 &owned_stream_.c_stream(),
86 owned_stream_.total_size())) {
87 RTI_FLAT_BUILDER_OUT_OF_RESOURCES_ERROR(
return);
90 stream().serialize_dheader();
95 #if defined(RTI_FLAT_DATA_CXX11_RVALUE_REFERENCES)
100 if (
this == &other) {
104 finish_untyped_impl();
106 AbstractBuilder::operator=(static_cast<AbstractBuilder&&>(other));
108 dheader_position_ = other.dheader_position_;
109 emheader_position_ = other.emheader_position_;
116 struct UntypedAggregationBuilderMoveProxy : AbstractBuilderMoveProxy {
117 char * dheader_position_;
118 char * emheader_position_;
121 operator UntypedAggregationBuilderMoveProxy ()
throw()
123 UntypedAggregationBuilderMoveProxy other;
129 void move_from(UntypedAggregationBuilderMoveProxy& other)
131 AbstractBuilder::move_from(other);
132 dheader_position_ = other.dheader_position_;
133 emheader_position_ = other.emheader_position_;
136 void move_to(UntypedAggregationBuilderMoveProxy& other)
138 AbstractBuilder::move_to(other);
139 other.dheader_position_ = dheader_position_;
140 other.emheader_position_ = emheader_position_;
149 finish_untyped_impl();
152 template <
typename T>
153 bool add_primitive_member(member_id_t
id, T value)
155 RTI_FLAT_BUILDER_CHECK_VALID(
return false);
157 rti::xcdr::Stream::Memento stream_memento(stream());
159 if (!begin_emheader(
id, detail::primitive_lc_code<T>::value)) {
160 RTI_FLAT_BUILDER_OUT_OF_RESOURCES_ERROR(
return false);
164 if (!stream().serialize_no_align(value)) {
165 RTI_FLAT_BUILDER_OUT_OF_RESOURCES_ERROR(
return false);
168 stream_memento.discard();
172 template <
typename OffsetType>
173 OffsetType add_member(member_id_t
id)
175 RTI_FLAT_BUILDER_CHECK_VALID(
return OffsetType());
178 rti::xcdr::Stream::Memento stream_memento(stream());
179 return add_member<OffsetType>(id, stream_memento);
182 template <
typename OffsetType>
183 OffsetType add_member(
185 rti::xcdr::Stream::Memento& stream_memento)
189 if (!begin_emheader(
id, 4)) {
193 OffsetType result = AbstractBuilder::add_element<OffsetType>();
196 stream_memento.discard();
200 template <
typename NestedBuilder>
201 NestedBuilder build_member(member_id_t
id)
203 RTI_FLAT_BUILDER_CHECK_VALID(
return NestedBuilder());
206 rti::xcdr::Stream::Memento stream_memento(stream());
207 return build_member<NestedBuilder>(id, stream_memento);
210 template <
typename NestedBuilder>
211 NestedBuilder build_member(
213 rti::xcdr::Stream::Memento& stream_memento)
215 if (!begin_emheader(
id, detail::lc_code<NestedBuilder>::value)) {
216 return NestedBuilder();
219 return AbstractBuilder::build_element_no_align<NestedBuilder>(
223 unsigned char* finish_sample_impl()
225 RTI_FLAT_BUILDER_CHECK_VALID(
return NULL);
226 RTI_FLAT_BUILDER_CHECK_NOT_BOUND(
return NULL);
227 RTI_FLAT_BUILDER_CHECK_CAN_FINISH_SAMPLE(
return NULL);
229 if (!stream().finish_dheader(dheader_position_)) {
230 RTI_FLAT_BUILDER_OUT_OF_RESOURCES_ERROR(
return NULL);
233 unsigned char *return_sample = stream().buffer();
236 return return_sample;
240 void finish_untyped_impl()
243 stream().finish_dheader(dheader_position_);
244 AbstractBuilder::finish_untyped_impl();
248 template <
typename OffsetType>
249 OffsetType finish_impl()
251 RTI_FLAT_BUILDER_CHECK_VALID(
return OffsetType());
252 RTI_FLAT_BUILDER_CHECK_CAN_FINISH(
return OffsetType());
254 stream().finish_dheader(dheader_position_);
255 return AbstractBuilder::finish_impl<OffsetType>();
260 unsigned char * discard_sample_impl()
262 RTI_FLAT_BUILDER_CHECK_VALID(
return NULL);
263 RTI_FLAT_BUILDER_CHECK_NOT_BOUND(
return NULL);
264 RTI_FLAT_BUILDER_CHECK_CAN_FINISH_SAMPLE(
return NULL);
266 unsigned char *buffer = this->buffer();
279 AbstractBuilder::finish_member();
284 bool begin_emheader(member_id_t
id, RTIXCdrUnsignedLong lc)
286 RTI_FLAT_BUILDER_CHECK_NOT_BOUND(
return false);
288 if (!stream().serialize_emheader(emheader_position_,
id, lc)) {
289 RTI_FLAT_BUILDER_OUT_OF_RESOURCES_ERROR(
return false);
295 void finish_emheader()
297 if (emheader_position_ != NULL) {
298 stream().finish_emheader(emheader_position_);
299 emheader_position_ = NULL;
307 dheader_position_(NULL),
308 emheader_position_(NULL)
310 dheader_position_ = stream().serialize_dheader();
311 if (dheader_position_ == NULL) {
312 RTI_FLAT_BUILDER_OUT_OF_RESOURCES_ERROR(invalidate());
317 char * dheader_position_;
318 char * emheader_position_;
332 template <
typename Discriminator>
341 unsigned char *initial_buffer,
343 bool initialize_members) :
348 template <
typename T>
349 bool add_primitive_member(member_id_t
id, Discriminator disc, T value)
351 RTI_FLAT_BUILDER_CHECK_VALID(
return false);
355 move_to_discriminator();
356 rti::xcdr::Stream::Memento stream_memento(stream());
358 if (!add_discriminator(disc)) {
362 if (!AggregationBuilder::add_primitive_member(
id, value)) {
366 stream_memento.discard();
370 template <
typename OffsetType>
371 OffsetType add_member(member_id_t
id, Discriminator disc)
373 RTI_FLAT_BUILDER_CHECK_VALID(
return OffsetType());
375 move_to_discriminator();
376 rti::xcdr::Stream::Memento stream_memento(stream());
377 if (!add_discriminator(disc)) {
378 RTI_FLAT_BUILDER_OUT_OF_RESOURCES_ERROR(
return OffsetType());
381 return AggregationBuilder::add_member<OffsetType>(id, stream_memento);
384 template <
typename NestedBuilder>
385 NestedBuilder build_member(member_id_t
id, Discriminator disc)
387 RTI_FLAT_BUILDER_CHECK_VALID(
return NestedBuilder());
389 move_to_discriminator();
390 rti::xcdr::Stream::Memento stream_memento(stream());
391 add_discriminator(disc);
393 return AggregationBuilder::build_member<NestedBuilder>(id, stream_memento);
403 void move_to_discriminator()
405 stream().current_position(begin_position() + RTI_XCDR_DHEADER_SIZE);
408 bool add_discriminator(Discriminator disc)
410 return AggregationBuilder::add_primitive_member(0, disc);
417 #endif // RTI_DDS_FLAT_AGGREGATIONBUILDERS_HPP_