/* * Copyright (C) 2020-present ScyllaDB */ /* * SPDX-License-Identifier: AGPL-3.0-or-later */ #pragma once #include #include #include "utils/UUID.hh" namespace raft { namespace internal { template class tagged_uint64 { uint64_t _val; public: tagged_uint64() : _val(0) {} explicit tagged_uint64(uint64_t v) : _val(v) {} tagged_uint64(const tagged_uint64&) = default; tagged_uint64(tagged_uint64&&) = default; tagged_uint64& operator=(const tagged_uint64&) = default; auto operator<=>(const tagged_uint64&) const = default; explicit operator bool() const { return _val != 0; } uint64_t get_value() const { return _val; } operator uint64_t() const { return get_value(); } tagged_uint64& operator++() { // pre increment ++_val; return *this; } tagged_uint64 operator++(int) { // post increment uint64_t v = _val++; return tagged_uint64(v); } tagged_uint64& operator--() { // pre decrement --_val; return *this; } tagged_uint64 operator--(int) { // post decrement uint64_t v = _val--; return tagged_uint64(v); } tagged_uint64 operator+(const tagged_uint64& o) const { return tagged_uint64(_val + o._val); } tagged_uint64 operator-(const tagged_uint64& o) const { return tagged_uint64(_val - o._val); } friend std::ostream& operator<<(std::ostream& os, const tagged_uint64& u) { os << u._val; return os; } }; template struct tagged_id { utils::UUID id; bool operator==(const tagged_id& o) const { return id == o.id; } bool operator<(const tagged_id& o) const { return id < o.id; } explicit operator bool() const { // The default constructor sets the id to nil, which is // guaranteed to not match any valid id. return id != utils::UUID(); } static tagged_id create_random_id() { return tagged_id{utils::make_random_uuid()}; } explicit tagged_id(const utils::UUID& uuid) : id(uuid) {} tagged_id() = default; }; template std::ostream& operator<<(std::ostream& os, const tagged_id& id) { os << id.id; return os; } } // end of namespace internal } // end of namespace raft namespace std { template struct hash> { size_t operator()(const raft::internal::tagged_id& id) const { return hash()(id.id); } }; template struct hash> { size_t operator()(const raft::internal::tagged_uint64& val) const { return hash()(val); } }; } // end of namespace std