tombstone: Extract out relational operators

This patch extracts out the relational operators in struct tombstone
to a class capable of generating them from a tri-compare function.

Signed-off-by: Duarte Nunes <duarte@scylladb.com>
This commit is contained in:
Duarte Nunes
2017-03-26 23:37:42 +02:00
parent 392403b5b3
commit d216c3dbd2
2 changed files with 69 additions and 25 deletions

View File

@@ -26,12 +26,13 @@
#include "timestamp.hh"
#include "gc_clock.hh"
#include "hashing.hh"
#include "utils/with_relational_operators.hh"
/**
* Represents deletion operation. Can be commuted with other tombstones via apply() method.
* Can be empty.
*/
struct tombstone final {
struct tombstone final : public with_relational_operators<tombstone> {
api::timestamp_type timestamp;
gc_clock::time_point deletion_time;
@@ -58,30 +59,6 @@ struct tombstone final {
}
}
bool operator<(const tombstone& t) const {
return compare(t) < 0;
}
bool operator<=(const tombstone& t) const {
return compare(t) <= 0;
}
bool operator>(const tombstone& t) const {
return compare(t) > 0;
}
bool operator>=(const tombstone& t) const {
return compare(t) >= 0;
}
bool operator==(const tombstone& t) const {
return compare(t) == 0;
}
bool operator!=(const tombstone& t) const {
return compare(t) != 0;
}
explicit operator bool() const {
return timestamp != api::missing_timestamp;
}

View File

@@ -0,0 +1,67 @@
/*
* Copyright (C) 2017 ScyllaDB
*/
/*
* This file is part of Scylla.
*
* Scylla is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Scylla is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Scylla. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <seastar/util/gcc6-concepts.hh>
#include <type_traits>
GCC6_CONCEPT(
template<typename T>
concept bool HasTriCompare =
requires(const T& t) {
{ t.compare(t) } -> int;
} && std::is_same<std::result_of_t<decltype(&T::compare)(T, T)>, int>::value; //FIXME: #1449
)
template<typename T>
class with_relational_operators {
private:
template<typename U>
GCC6_CONCEPT( requires HasTriCompare<U> )
int do_compare(const U& t) const {
return static_cast<const U*>(this)->compare(t);
}
public:
bool operator<(const T& t) const {
return do_compare(t) < 0;
}
bool operator<=(const T& t) const {
return do_compare(t) <= 0;
}
bool operator>(const T& t) const {
return do_compare(t) > 0;
}
bool operator>=(const T& t) const {
return do_compare(t) >= 0;
}
bool operator==(const T& t) const {
return do_compare(t) == 0;
}
bool operator!=(const T& t) const {
return do_compare(t) != 0;
}
};