From ea0e1ff3b52e6daf3a4018e390d5fda3c3ecfe4e Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Tue, 26 May 2015 15:31:42 +0300 Subject: [PATCH] types.hh: Add data_value class Add a data_value class that also encodes a value type. This makes it easier to use than plain boost::any for comparing two values. Signed-off-by: Pekka Enberg --- types.hh | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/types.hh b/types.hh index fdb47b5969..677282f124 100644 --- a/types.hh +++ b/types.hh @@ -169,6 +169,28 @@ inline int32_t compare_unsigned(bytes_view v1, bytes_view v2) { return (int32_t) (v1.size() - v2.size()); } +class abstract_type; + +using data_type = shared_ptr; + +class data_value { + boost::any _value; + data_type _type; +public: + data_value(boost::any&& value, data_type type) + : _value{std::move(value)} + , _type{type} + { } + const boost::any& value() const { + return _value; + } + data_type type() const { + return _type; + } + friend inline bool operator==(const data_value& x, const data_value& y); + friend inline bool operator!=(const data_value& x, const data_value& y); +}; + class serialized_compare; class abstract_type : public enable_shared_from_this { @@ -198,6 +220,9 @@ public: } } virtual boost::any deserialize(bytes_view v) const = 0; + data_value deserialize_value(bytes_view v) const { + return data_value{deserialize(v), shared_from_this()}; + }; virtual void validate(bytes_view v) const { // FIXME } @@ -246,6 +271,9 @@ public: virtual boost::any compose(const bytes& v) const { return deserialize(v); } + data_value compose_value(const bytes& v) const { + return deserialize_value(v); + } bytes decompose(const boost::any& value) const { bytes b(bytes::initialized_later(), serialized_size(value)); auto i = b.begin(); @@ -284,7 +312,16 @@ public: friend class list_type_impl; }; -using data_type = shared_ptr; +inline bool operator==(const data_value& x, const data_value& y) +{ + return x._type == y._type && x._type->decompose(x._value) == y._type->decompose(y._value); +} + +inline bool operator!=(const data_value& x, const data_value& y) +{ + return !(x == y); +} + using bytes_view_opt = std::experimental::optional; static inline @@ -394,6 +431,9 @@ public: collection_mutation::one merge(collection_mutation::view a, collection_mutation::view b) const; virtual void serialize(const boost::any& value, bytes::iterator& out, serialization_format sf) const = 0; virtual boost::any deserialize(bytes_view v, serialization_format sf) const = 0; + data_value deserialize_value(bytes_view v, serialization_format sf) const { + return data_value{deserialize(v, sf), shared_from_this()}; + } bytes_opt reserialize(serialization_format from, serialization_format to, bytes_view_opt v) const; };