载入中...
搜索中...
未找到
RuntimeHandle.h
浏览该文件的文档.
1#pragma once
2
13#include <compare>
14#include <cstddef>
15#include <cstdint>
16#include <functional>
17#include <limits>
18#include <optional>
19
20namespace eve {
21
22namespace detail {
23
34 using index_type = std::uint32_t;
36 using generation_type = std::uint32_t;
37
39 static constexpr index_type invalidIndex = std::numeric_limits<index_type>::max();
41 static constexpr generation_type invalidGeneration = 0;
42
45
47 [[nodiscard]] constexpr bool isValid() const noexcept {
49 }
50
52 [[nodiscard]] constexpr bool isInvalid() const noexcept { return !isValid(); }
53
59 [[nodiscard]] static constexpr std::optional<generation_type> nextGeneration(generation_type current) noexcept {
60 if (current == std::numeric_limits<generation_type>::max()) return std::nullopt;
61 return static_cast<generation_type>(current + 1u);
62 }
63
68 [[nodiscard]] constexpr std::optional<generation_type> bumpedGeneration() const noexcept {
69 if (index == invalidIndex) return std::nullopt;
72 }
73
78 [[nodiscard]] constexpr std::uint64_t packed() const noexcept {
79 return (static_cast<std::uint64_t>(generation) << 32u) | static_cast<std::uint64_t>(index);
80 }
81
87 [[nodiscard]] static constexpr HandleCoordinates fromPacked(std::uint64_t value) noexcept {
88 return HandleCoordinates{static_cast<index_type>(value & 0xffffffffull),
89 static_cast<generation_type>(value >> 32u)};
90 }
91
93 friend constexpr bool operator==(const HandleCoordinates&, const HandleCoordinates&) noexcept = default;
95 friend constexpr auto operator<=>(const HandleCoordinates&, const HandleCoordinates&) noexcept = default;
96};
97
98} // namespace detail
99
114template <typename Tag>
117public:
122
127
129 constexpr RuntimeHandle() noexcept = default;
130
140 : coordinates_{index, generation} {}
141
143 [[nodiscard]] static constexpr RuntimeHandle invalid() noexcept { return {}; }
144
146 [[nodiscard]] constexpr bool isValid() const noexcept { return coordinates_.isValid(); }
147
149 [[nodiscard]] constexpr bool isInvalid() const noexcept { return coordinates_.isInvalid(); }
150
152 [[nodiscard]] constexpr index_type index() const noexcept { return coordinates_.index; }
153
155 [[nodiscard]] constexpr generation_type generation() const noexcept { return coordinates_.generation; }
156
162 [[nodiscard]] static constexpr std::optional<generation_type> nextGeneration(generation_type current) noexcept {
164 }
165
170 [[nodiscard]] constexpr std::optional<RuntimeHandle> nextGeneration() const noexcept {
171 const auto next = coordinates_.bumpedGeneration();
172 if (!next) return std::nullopt;
174 return RuntimeHandle(coordinates_.index, *next);
175 }
176
183 [[nodiscard]] constexpr std::uint64_t packed() const noexcept { return coordinates_.packed(); }
184
190 [[nodiscard]] static constexpr RuntimeHandle fromPacked(std::uint64_t value) noexcept {
191 const auto coordinates = detail::HandleCoordinates::fromPacked(value);
193 return RuntimeHandle(coordinates.index, coordinates.generation);
194 }
195
197 friend constexpr bool operator==(const RuntimeHandle& left, const RuntimeHandle& right) noexcept {
198 return left.coordinates_ == right.coordinates_;
199 }
201 friend constexpr auto operator<=>(const RuntimeHandle& left, const RuntimeHandle& right) noexcept {
202 return left.coordinates_ <=> right.coordinates_;
203 }
204
205private:
206 detail::HandleCoordinates coordinates_;
207};
208
209} // namespace eve
210
211namespace std {
212
214template <typename Tag>
216struct hash<eve::RuntimeHandle<Tag>> {
218 std::size_t operator()(const eve::RuntimeHandle<Tag>& handle) const noexcept {
219 return std::hash<std::uint64_t>{}(handle.packed());
220 }
221};
222
223} // namespace std
double value
std::array< std::uint8_t, 32 > hash
Definition Evpack.cpp:172
HexVec3 left
HexVec3 right
PrimitiveHandle handle
int detail
double current
Process-local slot identity tagged by the registry that owns it.
constexpr bool isInvalid() const noexcept
Returns whether this value is the invalid sentinel.
static constexpr RuntimeHandle fromPacked(std::uint64_t value) noexcept
Reconstructs a handle from an explicit packed boundary value.
constexpr generation_type generation() const noexcept
Returns the owner-local replacement generation.
constexpr bool isValid() const noexcept
Returns whether both coordinates are usable handle values.
constexpr index_type index() const noexcept
Returns the owner-local slot index.
detail::HandleCoordinates::generation_type generation_type
Unsigned owner-local generation type.
constexpr std::optional< RuntimeHandle > nextGeneration() const noexcept
Returns this slot at its next generation.
static constexpr generation_type invalidGeneration
Reserved generation value shared by all invalid handles.
static constexpr std::optional< generation_type > nextGeneration(generation_type current) noexcept
Returns the next generation without unsigned wraparound.
friend constexpr bool operator==(const RuntimeHandle &left, const RuntimeHandle &right) noexcept
Operator ==.
static constexpr index_type invalidIndex
Reserved index value shared by all invalid handles.
detail::HandleCoordinates::index_type index_type
Unsigned owner-local slot index type.
constexpr std::uint64_t packed() const noexcept
Encodes the two coordinates for a legacy integer boundary.
constexpr RuntimeHandle() noexcept=default
Constructs an invalid handle.
friend constexpr auto operator<=>(const RuntimeHandle &left, const RuntimeHandle &right) noexcept
Operator <=>.
static constexpr RuntimeHandle invalid() noexcept
Returns the canonical invalid handle.
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16
Tag-free slot coordinates behind every RuntimeHandle<Tag>.
static constexpr index_type invalidIndex
Reserved index value shared by all invalid handles.
static constexpr HandleCoordinates fromPacked(std::uint64_t value) noexcept
Reconstructs coordinates from an explicit packed boundary value.
constexpr bool isInvalid() const noexcept
Returns whether this value is the invalid sentinel.
static constexpr generation_type invalidGeneration
Reserved generation value shared by all invalid handles.
std::uint32_t generation_type
Unsigned owner-local generation type.
friend constexpr auto operator<=>(const HandleCoordinates &, const HandleCoordinates &) noexcept=default
Operator <=>.
constexpr bool isValid() const noexcept
Returns whether both coordinates are usable handle values.
constexpr std::optional< generation_type > bumpedGeneration() const noexcept
Returns this slot's next generation.
static constexpr std::optional< generation_type > nextGeneration(generation_type current) noexcept
Returns the next generation without unsigned wraparound.
friend constexpr bool operator==(const HandleCoordinates &, const HandleCoordinates &) noexcept=default
Operator ==.
constexpr std::uint64_t packed() const noexcept
Encodes the two coordinates for a legacy integer boundary.
std::uint32_t index_type
Unsigned owner-local slot index type.