mirror of
https://github.com/scylladb/scylladb.git
synced 2026-05-29 19:21:01 +00:00
serializer: Add std::optional support
This commit is contained in:
@@ -27,6 +27,7 @@
|
||||
#include <seastar/core/sstring.hh>
|
||||
#include <unordered_map>
|
||||
#include <experimental/optional>
|
||||
#include <optional>
|
||||
#include "enum_set.hh"
|
||||
#include "utils/managed_bytes.hh"
|
||||
#include "bytes_ostream.hh"
|
||||
|
||||
@@ -540,6 +540,33 @@ void serialize_fragmented(Output& out, FragmentedBuffer&& v) {
|
||||
serializer<bytes>::write_fragmented(out, std::forward<FragmentedBuffer>(v));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
struct serializer<std::optional<T>> {
|
||||
template<typename Input>
|
||||
static std::optional<T> read(Input& in) {
|
||||
std::optional<T> v;
|
||||
auto b = deserialize(in, boost::type<bool>());
|
||||
if (b) {
|
||||
v = deserialize(in, boost::type<T>());
|
||||
}
|
||||
return v;
|
||||
}
|
||||
template<typename Output>
|
||||
static void write(Output& out, const std::optional<T>& v) {
|
||||
serialize(out, bool(v));
|
||||
if (v) {
|
||||
serialize(out, v.value());
|
||||
}
|
||||
}
|
||||
template<typename Input>
|
||||
static void skip(Input& in) {
|
||||
auto present = deserialize(in, boost::type<bool>());
|
||||
if (present) {
|
||||
serializer<T>::skip(in);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct serializer<std::experimental::optional<T>> {
|
||||
template<typename Input>
|
||||
|
||||
Reference in New Issue
Block a user