|
9 | 9 | #pragma once
|
10 | 10 |
|
11 | 11 | #include <cstdint>
|
| 12 | +#include <compare> |
| 13 | +#include <limits> |
| 14 | +#include <iostream> |
| 15 | +#include <seastar/core/sstring.hh> |
12 | 16 |
|
13 | 17 | 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(); |
17 | 68 | }
|
| 69 | +} //namespace std |
0 commit comments