11 #ifndef RTI_DDS_FLAT_FLATOFFSETS_HPP_
12 #define RTI_DDS_FLAT_FLATOFFSETS_HPP_
14 #include "xcdr/xcdr_stream.h"
15 #include "xcdr/xcdr_stream_impl.h"
16 #include "xcdr/xcdr_interpreter.h"
18 #include "rti/xcdr/Stream.hpp"
108 #ifdef DOXYGEN_DOCUMENTATION_ONLY
230 int32_t my_primitive()
const;
272 FlatMutableBar::ConstOffset my_mutable()
const;
297 bool my_primitive(int32_t value);
337 FlatMutableBar::Offset my_mutable();
419 int32_t my_primitive()
const;
445 bool my_primitive(int32_t value);
466 namespace rti {
namespace flat {
470 inline rti::flat::offset_t ptrdiff(
unsigned char *a,
unsigned char *b)
472 RTI_FLAT_ASSERT(a - b < RTI_XCDR_MAX_SERIALIZED_SIZE,
return 0);
474 return static_cast<rti::flat::offset_t
>(a - b);
480 template <
typename T,
unsigned int N>
481 class FinalArrayOffset;
511 return sample_ == NULL;
534 unsigned char * get_buffer()
539 return stream_.current_position();
554 const unsigned char * get_buffer()
const
559 return stream_.current_position();
570 RTI_FLAT_OFFSET_CHECK_NOT_NULL(
return 0);
571 return stream_.total_size();
582 return s1.get_buffer() < s2.get_buffer();
593 return s1.get_buffer() > s2.get_buffer();
604 return s1.get_buffer() <= s2.get_buffer();
615 return s1.get_buffer() >= s2.get_buffer();
626 return s1.get_buffer() == s2.get_buffer();
650 offset_t absolute_offset,
651 offset_t serialized_size)
653 absolute_offset_(absolute_offset)
658 RTI_FLAT_CHECK_PRECONDITION(sample != NULL,
return);
660 sample_->initialize_stream(
667 template <
typename U>
668 U deserialize(offset_t member_offset)
const
670 RTI_FLAT_OFFSET_CHECK_NOT_NULL(
return U());
671 RTI_FLAT_ASSERT(stream_.check_size(member_offset + static_cast<offset_t>(
sizeof(U))),
return U());
673 rti::xcdr::Stream::Memento stream_memento(stream_);
674 stream_.skip_fast(member_offset);
675 return stream_.deserialize_fast<U>();
679 template <
typename U>
680 bool serialize(offset_t member_offset, U value)
682 RTI_FLAT_OFFSET_CHECK_NOT_NULL(
return false);
683 RTI_FLAT_ASSERT(stream_.check_size(member_offset + static_cast<offset_t>(
sizeof(U))),
return false);
685 rti::xcdr::Stream::Memento stream_memento(stream_);
687 stream_.skip_fast(member_offset);
688 stream_.serialize_fast<U>(value);
695 template <
typename U>
696 U get_member(offset_t relative_member_offset)
698 RTI_FLAT_OFFSET_CHECK_NOT_NULL(
return U());
703 absolute_offset_ + relative_member_offset);
706 template <
typename U>
707 U get_member(offset_t relative_member_offset)
const
709 RTI_FLAT_OFFSET_CHECK_NOT_NULL(
return U());
714 absolute_offset_ + relative_member_offset);
720 offset_t absolute_offset_;
721 mutable rti::xcdr::Stream stream_;
731 template <
typename T>
734 typedef fixed_size_type_tag_t offset_kind;
736 static offset_t serialized_size_w_padding()
738 RTI_FLAT_ASSERT(T::serialized_size(0) > 0,
return 0);
739 RTI_FLAT_ASSERT(T::required_alignment > 0,
return 0);
741 const offset_t element_size = T::serialized_size(0);
742 if (element_size % T::required_alignment != 0) {
743 return element_size + T::required_alignment
744 - element_size % T::required_alignment;
750 bool is_cpp_compatible()
const
752 return !stream_.needs_byte_swap()
753 && rti::xcdr::has_cpp_friendly_cdr_layout<
754 typename rti::flat::flat_type_traits<T>::flat_type>();
767 FinalOffset(SampleBase *sample, offset_t absolute_offset)
771 T::serialized_size(absolute_offset))
778 template <
typename U>
779 U deserialize(
const offset_t *relative_member_offsets)
const
781 RTI_FLAT_ASSERT(relative_member_offsets != NULL,
return U());
793 return OffsetBase::deserialize<U>(
795 get_value_for_alignment(relative_member_offsets));
799 template <
typename U>
800 bool serialize(
const offset_t *relative_member_offsets, U value)
802 RTI_FLAT_ASSERT(relative_member_offsets != NULL,
return false);
804 return OffsetBase::serialize<U>(
806 get_value_for_alignment(relative_member_offsets), value);
813 template <
typename U>
814 U get_member(
const offset_t *relative_member_offsets)
816 RTI_FLAT_ASSERT(relative_member_offsets != NULL,
return U());
818 return OffsetBase::get_member<U>(
819 get_value_for_alignment(relative_member_offsets));
822 template <
typename U>
823 U get_member(
const offset_t *relative_member_offsets)
const
825 RTI_FLAT_ASSERT(relative_member_offsets != NULL,
return U());
827 return OffsetBase::get_member<U>(
828 get_value_for_alignment(relative_member_offsets));
832 template <
typename U,
unsigned int N>
834 const offset_t *relative_member_offsets,
835 const offset_t *first_element_sizes,
836 offset_t element_size)
840 RTI_FLAT_ASSERT(relative_member_offsets != NULL,
return ArrayType());
841 RTI_FLAT_ASSERT(first_element_sizes != NULL,
return ArrayType());
843 RTI_FLAT_OFFSET_CHECK_NOT_NULL(
return ArrayType());
848 absolute_offset_ + get_value_for_alignment(relative_member_offsets),
850 get_value_for_alignment(first_element_sizes),
855 template <
typename U,
unsigned int N>
857 const offset_t *relative_member_offsets,
858 const offset_t *first_element_sizes,
859 offset_t element_size)
const
863 RTI_FLAT_ASSERT(relative_member_offsets != NULL,
return ArrayType());
864 RTI_FLAT_ASSERT(first_element_sizes != NULL,
return ArrayType());
866 RTI_FLAT_OFFSET_CHECK_NOT_NULL(
return ArrayType());
870 absolute_offset_ + get_value_for_alignment(relative_member_offsets),
872 get_value_for_alignment(first_element_sizes),
883 offset_t get_value_for_alignment(
const offset_t *values_per_alignment)
const
885 return values_per_alignment[absolute_offset_ % 4];
889 struct MutableOffsetHelper {
892 static offset_t calculate_serialized_size(
893 rti::flat::SampleBase *sample,
894 offset_t absolute_offset,
897 RTI_FLAT_ASSERT(sample != NULL,
return 0);
899 return RTIXCdrFlatSample_getMutableSampleSize(
900 sample->get_buffer(),
914 typedef variable_size_type_tag_t offset_kind;
915 typedef MutableOffsetHelper Helper;
924 offset_t absolute_offset,
925 offset_t serialized_size)
933 template <
typename U>
934 U deserialize(member_id_t member_id, U default_val = U())
const
936 RTI_FLAT_OFFSET_CHECK_NOT_NULL(
return U());
938 rti::xcdr::Stream::Memento stream_memento(stream_);
940 offset_t member_size = 0;
941 if (!RTIXCdrStream_findV2MutableSampleMember(
948 return stream_.deserialize_fast<U>();
952 template <
typename U>
953 bool serialize(member_id_t member_id, U value)
955 RTI_FLAT_OFFSET_CHECK_NOT_NULL(
return false);
957 rti::xcdr::Stream::Memento stream_memento(stream_);
959 offset_t member_size = 0;
960 if (!RTIXCdrStream_findV2MutableSampleMember(
967 stream_.serialize_fast<U>(value);
971 template <
typename U>
972 U get_member(member_id_t member_id)
const
974 RTI_FLAT_OFFSET_CHECK_NOT_NULL(
return U());
976 rti::xcdr::Stream::Memento stream_memento(stream_);
978 offset_t member_size = 0;
979 if (!RTIXCdrStream_findV2MutableSampleMember(
986 return get_member_impl<U>(
988 detail::ptrdiff(stream_.current_position(), sample_->get_buffer()),
990 typename U::offset_kind());
994 template <
typename U>
996 offset_t absolute_member_offset,
997 offset_t member_size,
998 variable_size_type_tag_t)
const
1000 RTI_FLAT_ASSERT(absolute_member_offset > 0,
return U());
1003 return U(sample_, absolute_member_offset, member_size);
1006 template <
typename U>
1008 offset_t absolute_member_offset,
1009 offset_t member_size,
1010 fixed_size_type_tag_t)
const
1014 RTI_FLAT_ASSERT(absolute_member_offset > 0,
return U());
1015 RTI_FLAT_ASSERT(member_size == U::serialized_size(0),
return U());
1018 return U(sample_, absolute_member_offset);
1030 template <
typename T>
1033 typedef fixed_size_type_tag_t offset_kind;
1041 offset_t absolute_offset)
1049 bool is_cpp_compatible()
const
1051 return sizeof(T) == 1 || !stream_.needs_byte_swap();
1061 RTI_FLAT_OFFSET_CHECK_NOT_NULL(
return T());
1062 RTI_FLAT_ASSERT(stream_.check_size(
sizeof(T)),
return T());
1064 return stream_.deserialize_fast<T>();
1067 static offset_t serialized_size(offset_t)
1072 static offset_t serialized_size_w_padding()
1074 return serialized_size(0);
1095 template <
typename T>
1104 offset_t absolute_offset)
1118 RTI_FLAT_OFFSET_CHECK_NOT_NULL(
return false);
1119 RTI_FLAT_ASSERT(this->stream_.check_size(
sizeof(T)),
return false);
1121 this->stream_.template serialize_fast<T>(value);
1128 #endif // RTI_DDS_FLAT_FLATOFFSETS_HPP_