forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
internal.hh
108 lines (95 loc) · 2.73 KB
/
internal.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* Copyright (C) 2020-present ScyllaDB
*/
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#pragma once
#include <ostream>
#include <functional>
#include "utils/UUID.hh"
namespace raft {
namespace internal {
template<typename Tag>
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<Tag>& u) {
os << u._val;
return os;
}
};
template<typename Tag>
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<typename Tag>
std::ostream& operator<<(std::ostream& os, const tagged_id<Tag>& id) {
os << id.id;
return os;
}
} // end of namespace internal
} // end of namespace raft
namespace std {
template<typename Tag>
struct hash<raft::internal::tagged_id<Tag>> {
size_t operator()(const raft::internal::tagged_id<Tag>& id) const {
return hash<utils::UUID>()(id.id);
}
};
template<typename Tag>
struct hash<raft::internal::tagged_uint64<Tag>> {
size_t operator()(const raft::internal::tagged_uint64<Tag>& val) const {
return hash<uint64_t>()(val);
}
};
} // end of namespace std