Skip to content

Proposed Changes to ValueReader Interface

Son Dinh edited this page Mar 8, 2024 · 2 revisions

Supported serialization formats

Currently, supported serialization formats for ValueReader/ValueWriter are XCDR2, JSON. Support for other XCDR versions (XCDR1, XCDR3) should not require further changes to the ValueReader/ValueWriter interfaces.

vread function

Current generated vread function has the following style:

struct Sample {
  @id(1) long member1;
  @id(2) short member2;
};

bool vread(ValueReader& vr, Sample& sample)
{
  ListMemberHelper helper;
  vr.begin_struct();
  XTypes::MemberId id;
  while (vr.begin_struct_member(id, helper)) {
    switch (id) {
    case 1: {
      // read member1
      // ...
    }
    case 2: {
      // read member2
      // ...
    }
    }
    vr.end_struct_member();
  }
  vr.end_struct();
}

The return value of begin_struct_member is used to check if there is no further member to read. But it also indicates if there is an error when reading the stream.

Proposed change: Add a new method members_remaining that returns true if there are more members to read and false otherwise. Internally, it checks if the logical read pointer has reached the end of the stream. For XCDR2 appendable and mutable, this is done by comparing the Serializer's read pointer with the end of the stream. For XCDR2 final, the size of the sample is not known, so members_remaining can be no-op; the generated vread (or a dynamic vread) knows the number of members so it doesn't need to rely on members_remaining.

The style of the vread function is similar with a small tweak:

bool vread(ValueReader& vr, Sample& sample)
{
  ListMemberHelper helper;
  vr.begin_struct();
  XTypes::MemberId id;
  while (vr.members_remaining()) {
    vr.begin_struct_member(id, helper)
    switch (id) {
    case 1: {
      // read member1
      // ...
    }
    case 2: {
      // read member2
      // ...
    }
    }
    vr.end_struct_member();
  }
  vr.end_struct();
}

ValueReader Interface

Some methods need to change to support XCDR2.

bool begin_struct(Extensibility extensibility);
bool begin_union(Extensibility extensibility);
bool begin_array(XTypes::TypeKind elem_kind);
bool begin_sequence(XTypes::TypeKind elem_kind);

begin_struct and begin_union receive the Extensibility of the type so that they know if there is a delimiter header. begin_array and begin_sequence need the kind of the element type to decide if there is a delimiter header.

Add methods to read bitmask:

virtual bool read_bitmask(ACE_CDR::ULongLong& value, const BitmaskHelper& helper) = 0;

template <typename T>
bool read_bitmask(T& value, const BitmaskHelper& helper);