From dfd206b3a364f488aa43ba612c90556fd3d0f65d Mon Sep 17 00:00:00 2001 From: Asias He Date: Wed, 18 Apr 2018 14:02:40 +0800 Subject: [PATCH] serializer: Add std::optional support --- serializer.hh | 1 + serializer_impl.hh | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/serializer.hh b/serializer.hh index 6b56040927..562c772c08 100644 --- a/serializer.hh +++ b/serializer.hh @@ -27,6 +27,7 @@ #include #include #include +#include #include "enum_set.hh" #include "utils/managed_bytes.hh" #include "bytes_ostream.hh" diff --git a/serializer_impl.hh b/serializer_impl.hh index d6b8468408..a11b6161bc 100644 --- a/serializer_impl.hh +++ b/serializer_impl.hh @@ -540,6 +540,33 @@ void serialize_fragmented(Output& out, FragmentedBuffer&& v) { serializer::write_fragmented(out, std::forward(v)); } +template +struct serializer> { + template + static std::optional read(Input& in) { + std::optional v; + auto b = deserialize(in, boost::type()); + if (b) { + v = deserialize(in, boost::type()); + } + return v; + } + template + static void write(Output& out, const std::optional& v) { + serialize(out, bool(v)); + if (v) { + serialize(out, v.value()); + } + } + template + static void skip(Input& in) { + auto present = deserialize(in, boost::type()); + if (present) { + serializer::skip(in); + } + } +}; + template struct serializer> { template