feat(library/normalize): add templates for serializing optional types

This commit is contained in:
Leonardo de Moura 2015-02-06 11:59:30 -08:00
parent 2e626b29fb
commit 1043cc8b48

View file

@ -13,6 +13,7 @@ Author: Leonardo de Moura
#include "util/list.h"
#include "util/buffer.h"
#include "util/int64.h"
#include "util/optional.h"
namespace lean {
/**
@ -100,4 +101,35 @@ template<typename T>
list<T> read_list(deserializer & d) {
return read_list<T>(d, [](deserializer & d) { T r; d >> r; return r; });
}
template<typename T>
serializer & write_optional(serializer & s, optional<T> const & a) {
if (a)
s << true << *a;
else
s << false;
return s;
}
template<typename T>
optional<T> read_optional(deserializer & d) {
if (d.read_bool()) {
T r;
d >> r;
return optional<T>(r);
} else {
return optional<T>();
}
}
template<typename T>
serializer & operator<<(serializer & s, optional<T> const & a) {
return write_optional<T>(s, a);
}
template<typename T>
deserializer & operator>>(deserializer & d, optional<T> & a) {
a = read_optional<T>(d);
return d;
}
}