From 0bd44deca9fbe9b4c2b490201f2467a5b6cab492 Mon Sep 17 00:00:00 2001 From: Avi Kivity Date: Wed, 11 Mar 2015 14:28:07 +0200 Subject: [PATCH] db: add abstract_type::as_less_comparator() Returns a binary predicate that allows comparing two values belonging to the type, suitable for STL containers and algorithms. --- types.hh | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/types.hh b/types.hh index 7556a64ec9..9c29be09a4 100644 --- a/types.hh +++ b/types.hh @@ -51,6 +51,8 @@ inline int32_t compare_unsigned(bytes_view v1, bytes_view v2) { return (int32_t) (v1.size() - v2.size()); } +class serialized_compare; + class abstract_type : public enable_shared_from_this { sstring _name; public: @@ -58,6 +60,8 @@ public: virtual ~abstract_type() {} virtual void serialize(const boost::any& value, std::ostream& out) = 0; virtual bool less(bytes_view v1, bytes_view v2) = 0; + // returns a callable that can be called with two byte_views, and calls this->less() on them. + serialized_compare as_less_comparator(); virtual size_t hash(bytes_view v) = 0; virtual bool equal(bytes_view v1, bytes_view v2) { if (is_byte_order_equal()) { @@ -365,6 +369,12 @@ public: } }; +inline +serialized_compare +abstract_type::as_less_comparator() { + return serialized_compare(shared_from_this()); +} + using key_compare = serialized_compare; // FIXME: add missing types