Skip to content

Commit d7c90b5

Browse files
author
Michael Livshin
committed
sstables: make generation_type an actual separate type
Now that `generation_type` is used properly (at least in some places), we turn to the compiler to help keep the generation/value separation intact. Fixes scylladb#10796. Signed-off-by: Michael Livshin <[email protected]>
1 parent ab13127 commit d7c90b5

File tree

1 file changed

+55
-3
lines changed

1 file changed

+55
-3
lines changed

sstables/generation_type.hh

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,61 @@
99
#pragma once
1010

1111
#include <cstdint>
12+
#include <compare>
13+
#include <limits>
14+
#include <iostream>
15+
#include <seastar/core/sstring.hh>
1216

1317
namespace sstables {
14-
using generation_type = int64_t;
15-
constexpr int64_t generation_value(generation_type generation) { return generation; }
16-
constexpr generation_type generation_from_value(int64_t value) { return value; }
18+
19+
class generation_type {
20+
int64_t _value;
21+
public:
22+
generation_type() = delete;
23+
24+
explicit constexpr generation_type(int64_t value) noexcept: _value(value) {}
25+
constexpr int64_t value() const noexcept { return _value; }
26+
27+
constexpr bool operator==(const generation_type& other) const noexcept { return _value == other._value; }
28+
constexpr std::strong_ordering operator<=>(const generation_type& other) const noexcept { return _value <=> other._value; }
29+
};
30+
31+
constexpr generation_type generation_from_value(int64_t value) {
32+
return generation_type{value};
33+
}
34+
constexpr int64_t generation_value(generation_type generation) {
35+
return generation.value();
36+
}
37+
38+
} //namespace sstables
39+
40+
namespace seastar {
41+
template <typename string_type = sstring>
42+
string_type to_sstring(sstables::generation_type generation) {
43+
return to_sstring(sstables::generation_value(generation));
44+
}
45+
} //namespace seastar
46+
47+
namespace std {
48+
template <>
49+
struct hash<sstables::generation_type> {
50+
constexpr size_t operator()(const sstables::generation_type& generation) const noexcept {
51+
return hash<int64_t>{}(generation.value());
52+
}
53+
};
54+
55+
// for min_max_tracker
56+
template <>
57+
struct numeric_limits<sstables::generation_type> : public numeric_limits<int64_t> {
58+
static constexpr sstables::generation_type min() noexcept {
59+
return sstables::generation_type{numeric_limits<int64_t>::min()};
60+
}
61+
static constexpr sstables::generation_type max() noexcept {
62+
return sstables::generation_type{numeric_limits<int64_t>::max()};
63+
}
64+
};
65+
66+
[[maybe_unused]] static ostream& operator<<(ostream& s, const sstables::generation_type& generation) {
67+
return s << generation.value();
1768
}
69+
} //namespace std

0 commit comments

Comments
 (0)