diff --git a/dds/DCPS/Xcdr2ValueWriter.cpp b/dds/DCPS/Xcdr2ValueWriter.cpp index 29bf1cd9621..f0b19cf50c8 100644 --- a/dds/DCPS/Xcdr2ValueWriter.cpp +++ b/dds/DCPS/Xcdr2ValueWriter.cpp @@ -7,18 +7,18 @@ namespace OpenDDS { namespace DCPS { // Called whenever we encounter a struct, union, sequence, or array. -// When that happens, create a new instance of Metadata that stores the states of +// When that happens, create a new instance of SerializedSizeState that stores the states of // the byte stream and the information of the type encountered, including its kind // and extensibility that helps decide when a header, e.g. Dheader or Emheader, is // required. If a header is needed, a position in the size cache is also stored so // that the actual size will be written when it is determined (see end_complex). void Xcdr2ValueWriter::begin_ssize_complex(Extensibility extensibility, CollectionKind coll_kind) { - Metadata metadata(extensibility, coll_kind); + SerializedSizeState state(extensibility, coll_kind); bool must_cache_size = true; - if (!state_.empty()) { - const Extensibility enclosing_ek = state_.top().extensibility; + if (!size_states_.empty()) { + const Extensibility enclosing_ek = size_states_.top().extensibility; if (enclosing_ek == FINAL || enclosing_ek == APPENDABLE) { if (extensibility == FINAL) { // We don't need to compute and cache the size for final extensibility type. @@ -26,18 +26,18 @@ void Xcdr2ValueWriter::begin_ssize_complex(Extensibility extensibility, Collecti // for which the size is required, for example, by a Dheader or a Emheader. // Copy the current size from the enclosing type so that the total size // can be built up cumulatively by this type. - metadata.total_size = state_.top().total_size; + state.total_size = size_states_.top().total_size; if (coll_kind == SEQUENCE_KIND) { // Sequence length - primitive_serialized_size_ulong(encoding_, metadata.total_size); + primitive_serialized_size_ulong(encoding_, state.total_size); } must_cache_size = false; } else { // The alignment in front of the Dheader is accounted to the size of the // containing type. Then we can compute the size of this type separately. - encoding_.align(state_.top().total_size, uint32_cdr_size); - serialized_size_delimiter(encoding_, metadata.total_size); + encoding_.align(size_states_.top().total_size, uint32_cdr_size); + serialized_size_delimiter(encoding_, state.total_size); if (coll_kind == SEQUENCE_KIND) { // Sequence length - primitive_serialized_size_ulong(encoding_, metadata.total_size); + primitive_serialized_size_ulong(encoding_, state.total_size); } } } else { // Enclosing type is mutable. @@ -49,10 +49,10 @@ void Xcdr2ValueWriter::begin_ssize_complex(Extensibility extensibility, Collecti // For members which are not struct, union, sequence, or array, the size // of the member is cached directly in the write_* methods. if (extensibility == APPENDABLE || extensibility == MUTABLE) { - serialized_size_delimiter(encoding_, metadata.total_size); + serialized_size_delimiter(encoding_, state.total_size); } if (coll_kind == SEQUENCE_KIND) { // Sequence length - primitive_serialized_size_ulong(encoding_, metadata.total_size); + primitive_serialized_size_ulong(encoding_, state.total_size); } } } else { @@ -60,20 +60,20 @@ void Xcdr2ValueWriter::begin_ssize_complex(Extensibility extensibility, Collecti // Clear the size cache in case it has data from a previous vwrite call. size_cache_.clear(); if (extensibility == APPENDABLE || extensibility == MUTABLE) { - serialized_size_delimiter(encoding_, metadata.total_size); + serialized_size_delimiter(encoding_, state.total_size); } } if (must_cache_size) { size_cache_.push_back(0); - metadata.cache_pos = size_cache_.size() - 1; + state.cache_pos = size_cache_.size() - 1; } - state_.push(metadata); + size_states_.push(state); } void Xcdr2ValueWriter::end_ssize_complex() { - Metadata& state = state_.top(); + SerializedSizeState& state = size_states_.top(); const Extensibility extensibility = state.extensibility; if (extensibility == MUTABLE) { @@ -82,24 +82,24 @@ void Xcdr2ValueWriter::end_ssize_complex() const size_t total_size = state.total_size; const size_t pos = state.cache_pos; - state_.pop(); + size_states_.pop(); - if (!state_.empty()) { - const Extensibility enclosing_ek = state_.top().extensibility; + if (!size_states_.empty()) { + const Extensibility enclosing_ek = size_states_.top().extensibility; if (enclosing_ek == FINAL || enclosing_ek == APPENDABLE) { if (extensibility == FINAL) { // Since the computed total size was built on top of the size of the // containing type, we can now copy it back to the entry of the containing type. - state_.top().total_size = total_size; + size_states_.top().total_size = total_size; } else { // The total size is ready, now update the total size of the containing type. // Also update the size of this type in the size cache. size_cache_[pos] = total_size; - state_.top().total_size += total_size; + size_states_.top().total_size += total_size; } } else { // Enclosing type is mutable. size_cache_[pos] = total_size; - state_.top().total_size += total_size; + size_states_.top().total_size += total_size; } } else { // We have finished working through the whole thing. @@ -109,7 +109,7 @@ void Xcdr2ValueWriter::end_ssize_complex() void Xcdr2ValueWriter::begin_ssize_aggregated_member(bool optional, bool present) { - Metadata& state = state_.top(); + SerializedSizeState& state = size_states_.top(); const Extensibility extensibility = state.extensibility; size_t& total_size = state.total_size; size_t& mutable_running_total = state.mutable_running_total; @@ -273,9 +273,11 @@ void Xcdr2ValueWriter::end_union_member() return; } -// Array can be treated similar to final/appendable struct with each element is a member. -// If the element type is primitive, it is similar to final struct. -// If the element type is not primitive, it is similar to appendable struct with Dheader. +// Array and sequence can be treated similar to final/appendable struct. +// If the element type is primitive, it is similar to a final struct with no delimiter. +// If the element type is not primitive, it is similar to an appendable struct with a delimiter. +// In both case, elements are serialized back-to-back similarly to final/appendable struct. +// One difference is that sequence has a length field ahead of the elements. void Xcdr2ValueWriter::begin_array(XTypes::TypeKind elem_tk) { // TODO(sonndinh): Make sure this works for both of these cases: @@ -289,7 +291,7 @@ void Xcdr2ValueWriter::begin_array(XTypes::TypeKind elem_tk) // called for the outermost dimension of the array. const bool primitive_elem_kind = XTypes::is_primitive(elem_tk); if (mode_ == SERIALIZATION_SIZE_MODE) { - if (state_.top().collection_kind != ARRAY_KIND && !primitive_elem_kind) { + if (size_states_.top().collection_kind != ARRAY_KIND && !primitive_elem_kind) { arr_exten = APPENDABLE; } } else { @@ -306,11 +308,6 @@ void Xcdr2ValueWriter::end_array() end_complex(); } -// Sequence is similar to final or appendable struct with each element is a member. -// If element type is primitive, it can be treated as final struct which has no Dheader. -// If element type is not primitive, it can be treated as appendable struct which has Dheader. -// In both case, elements are serialized back-to-back similarly to final/appendable struct. -// One difference is that sequence has length ahead of all the elements. void Xcdr2ValueWriter::begin_sequence(XTypes::TypeKind elem_tk, ACE_CDR::ULong length) { Extensibility seq_exten = FINAL; @@ -341,9 +338,9 @@ void Xcdr2ValueWriter::end_element() void Xcdr2ValueWriter::write_boolean(ACE_CDR::Boolean value) { if (mode_ == SERIALIZATION_SIZE_MODE) { - primitive_serialized_size_boolean(encoding_, state_.top().total_size); + primitive_serialized_size_boolean(encoding_, size_states_.top().total_size); - if (state_.top().extensibility == MUTABLE) { + if (size_states_.top().extensibility == MUTABLE) { size_cache_.push_back(boolean_cdr_size); } } else { @@ -354,9 +351,9 @@ void Xcdr2ValueWriter::write_boolean(ACE_CDR::Boolean value) void Xcdr2ValueWriter::write_byte(ACE_CDR::Octet value) { if (mode_ == SERIALIZATION_SIZE_MODE) { - primitive_serialized_size_octet(encoding_, state_.top().total_size); + primitive_serialized_size_octet(encoding_, size_states_.top().total_size); - if (state_.top().extensibility == MUTABLE) { + if (size_states_.top().extensibility == MUTABLE) { size_cache_.push_back(byte_cdr_size); } } else { @@ -368,9 +365,9 @@ void Xcdr2ValueWriter::write_byte(ACE_CDR::Octet value) void Xcdr2ValueWriter::write_int8(ACE_CDR::Int8 value) { if (mode_ == SERIALIZATION_SIZE_MODE) { - primitive_serialized_size_int8(encoding_, state_.top().total_size); + primitive_serialized_size_int8(encoding_, size_states_.top().total_size); - if (state_.top().extensibility == MUTABLE) { + if (size_states_.top().extensibility == MUTABLE) { size_cache_.push_back(int8_cdr_size); } } else { @@ -381,9 +378,9 @@ void Xcdr2ValueWriter::write_int8(ACE_CDR::Int8 value) void Xcdr2ValueWriter::write_uint8(ACE_CDR::UInt8 value) { if (mode_ == SERIALIZATION_SIZE_MODE) { - primitive_serialized_size_uint8(encoding_, state_.top().total_size); + primitive_serialized_size_uint8(encoding_, size_states_.top().total_size); - if (state_.top().extensibility == MUTABLE) { + if (size_states_.top().extensibility == MUTABLE) { size_cache_.push_back(uint8_cdr_size); } } else { @@ -395,9 +392,9 @@ void Xcdr2ValueWriter::write_uint8(ACE_CDR::UInt8 value) void Xcdr2ValueWriter::write_int16(ACE_CDR::Short value) { if (mode_ == SERIALIZATION_SIZE_MODE) { - primitive_serialized_size(encoding_, state_.top().total_size, value); + primitive_serialized_size(encoding_, size_states_.top().total_size, value); - if (state_.top().extensibility == MUTABLE) { + if (size_states_.top().extensibility == MUTABLE) { size_cache_.push_back(int16_cdr_size); } } else { @@ -408,9 +405,9 @@ void Xcdr2ValueWriter::write_int16(ACE_CDR::Short value) void Xcdr2ValueWriter::write_uint16(ACE_CDR::UShort value) { if (mode_ == SERIALIZATION_SIZE_MODE) { - primitive_serialized_size(encoding_, state_.top().total_size, value); + primitive_serialized_size(encoding_, size_states_.top().total_size, value); - if (state_.top().extensibility == MUTABLE) { + if (size_states_.top().extensibility == MUTABLE) { size_cache_.push_back(uint16_cdr_size); } } else { @@ -421,9 +418,9 @@ void Xcdr2ValueWriter::write_uint16(ACE_CDR::UShort value) void Xcdr2ValueWriter::write_int32(ACE_CDR::Long value) { if (mode_ == SERIALIZATION_SIZE_MODE) { - primitive_serialized_size(encoding_, state_.top().total_size, value); + primitive_serialized_size(encoding_, size_states_.top().total_size, value); - if (state_.top().extensibility == MUTABLE) { + if (size_states_.top().extensibility == MUTABLE) { size_cache_.push_back(int32_cdr_size); } } else { @@ -434,9 +431,9 @@ void Xcdr2ValueWriter::write_int32(ACE_CDR::Long value) void Xcdr2ValueWriter::write_uint32(ACE_CDR::ULong value) { if (mode_ == SERIALIZATION_SIZE_MODE) { - primitive_serialized_size(encoding_, state_.top().total_size, value); + primitive_serialized_size(encoding_, size_states_.top().total_size, value); - if (state_.top().extensibility == MUTABLE) { + if (size_states_.top().extensibility == MUTABLE) { size_cache_.push_back(uint32_cdr_size); } } else { @@ -447,9 +444,9 @@ void Xcdr2ValueWriter::write_uint32(ACE_CDR::ULong value) void Xcdr2ValueWriter::write_int64(ACE_CDR::LongLong value) { if (mode_ == SERIALIZATION_SIZE_MODE) { - primitive_serialized_size(encoding_, state_.top().total_size, value); + primitive_serialized_size(encoding_, size_states_.top().total_size, value); - if (state_.top().extensibility == MUTABLE) { + if (size_states_.top().extensibility == MUTABLE) { size_cache_.push_back(int64_cdr_size); } } else { @@ -460,9 +457,9 @@ void Xcdr2ValueWriter::write_int64(ACE_CDR::LongLong value) void Xcdr2ValueWriter::write_uint64(ACE_CDR::ULongLong value) { if (mode_ == SERIALIZATION_SIZE_MODE) { - primitive_serialized_size(encoding_, state_.top().total_size, value); + primitive_serialized_size(encoding_, size_states_.top().total_size, value); - if (state_.top().extensibility == MUTABLE) { + if (size_states_.top().extensibility == MUTABLE) { size_cache_.push_back(uint64_cdr_size); } } else { @@ -473,9 +470,9 @@ void Xcdr2ValueWriter::write_uint64(ACE_CDR::ULongLong value) void Xcdr2ValueWriter::write_float32(ACE_CDR::Float value) { if (mode_ == SERIALIZATION_SIZE_MODE) { - primitive_serialized_size(encoding_, state_.top().total_size, value); + primitive_serialized_size(encoding_, size_states_.top().total_size, value); - if (state_.top().extensibility == MUTABLE) { + if (size_states_.top().extensibility == MUTABLE) { size_cache_.push_back(float32_cdr_size); } } else { @@ -486,9 +483,9 @@ void Xcdr2ValueWriter::write_float32(ACE_CDR::Float value) void Xcdr2ValueWriter::write_float64(ACE_CDR::Double value) { if (mode_ == SERIALIZATION_SIZE_MODE) { - primitive_serialized_size(encoding_, state_.top().total_size, value); + primitive_serialized_size(encoding_, size_states_.top().total_size, value); - if (state_.top().extensibility == MUTABLE) { + if (size_states_.top().extensibility == MUTABLE) { size_cache_.push_back(float64_cdr_size); } } else { @@ -499,9 +496,9 @@ void Xcdr2ValueWriter::write_float64(ACE_CDR::Double value) void Xcdr2ValueWriter::write_float128(ACE_CDR::LongDouble value) { if (mode_ == SERIALIZATION_SIZE_MODE) { - primitive_serialized_size(encoding_, state_.top().total_size, value); + primitive_serialized_size(encoding_, size_states_.top().total_size, value); - if (state_.top().extensibility == MUTABLE) { + if (size_states_.top().extensibility == MUTABLE) { size_cache_.push_back(float128_cdr_size); } } else { @@ -517,9 +514,9 @@ void Xcdr2ValueWriter::write_fixed(const OpenDDS::FaceTypes::Fixed& /*value*/) void Xcdr2ValueWriter::write_char8(ACE_CDR::Char value) { if (mode_ == SERIALIZATION_SIZE_MODE) { - primitive_serialized_size_char(encoding_, state_.top().total_size); + primitive_serialized_size_char(encoding_, size_states_.top().total_size); - if (state_.top().extensibility == MUTABLE) { + if (size_states_.top().extensibility == MUTABLE) { size_cache_.push_back(char8_cdr_size); } } else { @@ -530,9 +527,9 @@ void Xcdr2ValueWriter::write_char8(ACE_CDR::Char value) void Xcdr2ValueWriter::write_char16(ACE_CDR::WChar value) { if (mode_ == SERIALIZATION_SIZE_MODE) { - primitive_serialized_size_wchar(encoding_, state_.top().total_size); + primitive_serialized_size_wchar(encoding_, size_states_.top().total_size); - if (state_.top().extensibility == MUTABLE) { + if (size_states_.top().extensibility == MUTABLE) { size_cache_.push_back(char16_cdr_size); } } else { @@ -543,13 +540,13 @@ void Xcdr2ValueWriter::write_char16(ACE_CDR::WChar value) void Xcdr2ValueWriter::write_string(const ACE_CDR::Char* value, size_t length) { if (mode_ == SERIALIZATION_SIZE_MODE) { - size_t& size = state_.top().total_size; + size_t& size = size_states_.top().total_size; primitive_serialized_size_ulong(encoding_, size); if (value) { size += length + 1; // Include null termination } - if (state_.top().extensibility == MUTABLE) { + if (size_states_.top().extensibility == MUTABLE) { // It's safe to do this since before every member of a mutable type, // the total_size variable is set to zero. size_cache_.push_back(size); @@ -562,13 +559,13 @@ void Xcdr2ValueWriter::write_string(const ACE_CDR::Char* value, size_t length) void Xcdr2ValueWriter::write_wstring(const ACE_CDR::WChar* value, size_t length) { if (mode_ == SERIALIZATION_SIZE_MODE) { - size_t& size = state_.top().total_size; + size_t& size = size_states_.top().total_size; primitive_serialized_size_ulong(encoding_, size); if (value) { size += length * char16_cdr_size; // Not include null termination } - if (state_.top().extensibility == MUTABLE) { + if (size_states_.top().extensibility == MUTABLE) { size_cache_.push_back(size); } } else { @@ -581,7 +578,7 @@ void Xcdr2ValueWriter::write_enum(const char* /*name*/, ACE_CDR::Long value, { bool invalid_int_type = false; if (mode_ == SERIALIZATION_SIZE_MODE) { - size_t& size = state_.top().total_size; + size_t& size = size_states_.top().total_size; switch (as_int) { case XTypes::TK_INT8: primitive_serialized_size_int8(encoding_, size); @@ -597,7 +594,7 @@ void Xcdr2ValueWriter::write_enum(const char* /*name*/, ACE_CDR::Long value, break; } if (!invalid_int_type) { - if (state_.top().extensibility == MUTABLE) { + if (size_states_.top().extensibility == MUTABLE) { size_cache_.push_back(size); } } diff --git a/dds/DCPS/Xcdr2ValueWriter.h b/dds/DCPS/Xcdr2ValueWriter.h index 1fead58b64e..fdd4cc7b412 100644 --- a/dds/DCPS/Xcdr2ValueWriter.h +++ b/dds/DCPS/Xcdr2ValueWriter.h @@ -108,8 +108,8 @@ class OpenDDS_Dcps_Export Xcdr2ValueWriter : public ValueWriter { // Maintain the states necessary to compute and cache the sizes of // the top-level type or its nested members. - struct Metadata { - Metadata(Extensibility exten, CollectionKind ck) + struct SerializedSizeState { + SerializedSizeState(Extensibility exten, CollectionKind ck) : extensibility(exten) , total_size(0) , mutable_running_total(0) @@ -133,7 +133,7 @@ class OpenDDS_Dcps_Export Xcdr2ValueWriter : public ValueWriter { size_t cache_pos; }; - std::stack state_; + std::stack size_states_; // Record the total size of the top-level type and its members if needed. // The size of a member is only recorded if it is required by a header,