Skip to content

Commit

Permalink
Update names
Browse files Browse the repository at this point in the history
  • Loading branch information
sonndinh committed Jan 31, 2024
1 parent cadb24e commit 52e7267
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 70 deletions.
131 changes: 64 additions & 67 deletions dds/DCPS/Xcdr2ValueWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,37 @@ 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.
// Instead, what we need is the total size of the closest ancestor type
// 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.
Expand All @@ -49,31 +49,31 @@ 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 {
// We are starting from the top-level type.
// 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) {
Expand All @@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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:
Expand All @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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);
Expand All @@ -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 {
Expand All @@ -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);
Expand All @@ -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);
}
}
Expand Down
Loading

0 comments on commit 52e7267

Please sign in to comment.