Skip to content

Extend Stream library to support serializing containers of non-trivial types #278

@DanRStevens

Description

@DanRStevens

Instead of trying to copy the whole vector to the stream at once (only possible for non-trivial types), we could instead iterate over each element of the vector, and serialize the element individually. This is possible if there is an appropriate Write(T& object) method. Such is already the case for std::string, as the Stream library natively supports this. The code of course generalizes to any non-trivial type, given the existence of an appropriate serialization method.

Example vector write method for non-trivially-copyable objects:

  Write(const std::vector<T, A>& vector) {
    for (const auto element : vector) {
     writer.Write(element);  // Call custom serializer for element type
    }
  }

Example usage:

void f(Stream::Writer& writer) {
  std::vector<std::string> nonTrivial;
  // ...
  writer.Write(nonTrivial);
}

In the above example, the serialization code would loop over the vector's elements, and call the custom std::string serialization code for each element.

Combine this with the proposal in Issue #277, and it would also add support for other arbitrary non-trivial types, provided they supply an appropriate T::Write(Stream::Writer& writer) method.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions