Commit 3c9ca018 authored by Dominik Charousset's avatar Dominik Charousset

Allow serializing type-erased tuples like messages

parent 7b51d55e
......@@ -266,6 +266,11 @@ public:
return *vals_;
}
/// Serializes the content of `x` as if `x` was an instance of `message`. The
/// resulting output of `sink` can then be used to deserialize a `message`
/// even if the serialized object had a different type.
static error save(serializer& sink, const type_erased_tuple& x);
/// @endcond
private:
......
......@@ -156,19 +156,19 @@ error message::load(deserializer& source) {
return none;
}
error message::save(serializer& sink) const {
error message::save(serializer& sink, const type_erased_tuple& x){
if (sink.context() == nullptr)
return sec::no_context;
// build type name
uint16_t zero = 0;
std::string tname = "@<>";
if (empty())
if (x.empty())
return error::eval([&] { return sink.begin_object(zero, tname); },
[&] { return sink.end_object(); });
auto& types = sink.context()->system().types();
auto n = size();
auto n = x.size();
for (size_t i = 0; i < n; ++i) {
auto rtti = cvals()->type(i);
auto rtti = x.type(i);
auto ptr = types.portable_name(rtti);
if (ptr == nullptr) {
std::cerr << "[ERROR]: cannot serialize message because a type was "
......@@ -185,7 +185,7 @@ error message::save(serializer& sink) const {
}
auto save_loop = [&]() -> error {
for (size_t i = 0; i < n; ++i) {
auto e = cvals()->save(i, sink);
auto e = x.save(i, sink);
if (e)
return e;
}
......@@ -196,6 +196,10 @@ error message::save(serializer& sink) const {
[&] { return sink.end_object(); });
}
error message::save(serializer& sink) const {
return save(sink, *this);
}
// -- factories ----------------------------------------------------------------
message message::copy(const type_erased_tuple& xs) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment