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.
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 forstd::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:
Example usage:
In the above example, the serialization code would loop over the vector's elements, and call the custom
std::stringserialization 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.