载入中...
搜索中...
未找到
Identity.h
浏览该文件的文档.
1#pragma once
2
12#include "common/Export.h"
13
14#include <array>
15#include <chrono>
16#include <compare>
17#include <cstddef>
18#include <cstdint>
19#include <functional>
20#include <optional>
21#include <span>
22#include <string>
23#include <string_view>
24#include <utility>
25
26namespace eve {
27
28namespace detail {
29
33struct ContentIdTag {};
35struct AssetGuidTag {};
37struct DocumentIdTag {};
41struct ArtifactIdTag {};
43struct EventIdTag {};
45struct CommandIdTag {};
51struct EffectIdTag {};
52
54using UuidBytes = std::array<std::uint8_t, 16>;
55
67[[nodiscard]] EVENGINE_API std::optional<UuidBytes> parseUuidText(std::string_view text) noexcept;
68
70[[nodiscard]] EVENGINE_API std::string formatUuidBytes(const UuidBytes& bytes);
71
73[[nodiscard]] EVENGINE_API UuidBytes childUuidBytes(const UuidBytes& parent, std::string_view role) noexcept;
74
88public:
96 EVENGINE_API void assign(std::string_view text);
97
103 [[nodiscard]] EVENGINE_API static UuidBytes projectLegacy(std::string_view text) noexcept;
104
105 std::string spelling;
107 bool canonical = false;
108};
109
110template <typename Tag>
112class Id128 {
113public:
114 using Bytes = std::array<std::uint8_t, 16>;
115
117 constexpr Id128() noexcept = default;
118
123 explicit constexpr Id128(Bytes bytes) noexcept : bytes_(bytes) {}
124
134 template <typename OtherTag>
136 [[nodiscard]] static constexpr Id128 fromUuid(const Id128<OtherTag>& value) noexcept {
138 return Id128(value.bytes());
139 }
140
142 [[nodiscard]] static constexpr Id128 nil() noexcept { return {}; }
143
151 [[nodiscard]] static std::optional<Id128> parse(std::string_view text) noexcept {
152 if (const auto bytes = parseUuidText(text)) return Id128(*bytes);
153 return std::nullopt;
154 }
155
161 [[nodiscard]] static std::optional<Id128> fromBytes(std::span<const std::uint8_t> bytes) noexcept {
162 if (bytes.size() != 16) return std::nullopt;
163 Bytes result{};
164 for (std::size_t i = 0; i < result.size(); ++i) result[i] = bytes[i];
166 return Id128(result);
167 }
168
173 [[nodiscard]] std::string format() const { return formatUuidBytes(bytes_); }
174
176 [[nodiscard]] constexpr bool isNil() const noexcept {
177 for (const auto byte : bytes_) {
178 if (byte != 0) return false;
179 }
180 return true;
181 }
182
192 [[nodiscard]] Id128 child(std::string_view role) const noexcept {
193 if (isNil() || role.empty()) return Id128::nil();
195 return Id128(childUuidBytes(bytes_, role));
196 }
197
202 [[nodiscard]] constexpr Bytes bytes() const noexcept { return bytes_; }
203
210 [[nodiscard]] constexpr std::uint64_t hash() const noexcept {
211 std::uint64_t result = 14695981039346656037ull;
212 for (const auto byte : bytes_) {
213 result ^= byte;
214 result *= 1099511628211ull;
215 }
216 return result;
217 }
218
220 friend constexpr bool operator==(const Id128&, const Id128&) noexcept = default;
221
223 friend constexpr auto operator<=>(const Id128& lhs, const Id128& rhs) noexcept { return lhs.bytes_ <=> rhs.bytes_; }
224
225private:
226 [[nodiscard]] static constexpr std::optional<std::uint8_t> hexValue(char value) noexcept {
227 if (value >= '0' && value <= '9') return static_cast<std::uint8_t>(value - '0');
228 if (value >= 'a' && value <= 'f') return static_cast<std::uint8_t>(value - 'a' + 10);
229 if (value >= 'A' && value <= 'F') return static_cast<std::uint8_t>(value - 'A' + 10);
230 return std::nullopt;
231 }
232
233 Bytes bytes_{};
234};
235
236} // namespace detail
237
244template <typename Tag>
246
255
265
274
277
280
283
286
289
292
295
298
312template <typename Tag>
315public:
317 using Bytes = typename Uuid::Bytes;
318
320 UuidIdAdapter() = default;
321
323 explicit UuidIdAdapter(const char* value) : UuidIdAdapter(std::string_view(value ? value : "")) {}
324
326 explicit UuidIdAdapter(std::string value) : UuidIdAdapter(std::string_view(value)) {}
327
329 explicit UuidIdAdapter(std::string_view value) { state_.assign(value); }
330
336 [[nodiscard]] static std::optional<UuidIdAdapter> parse(std::string_view value) {
337 if (value.empty()) return std::nullopt;
339 return UuidIdAdapter(value);
340 }
341
343 [[nodiscard]] static UuidIdAdapter fromUuid(Uuid value) {
344 UuidIdAdapter result;
345 result.state_.uuid = value.bytes();
346 result.state_.spelling = value.format();
347 result.state_.canonical = true;
348 return result;
349 }
350
352 [[nodiscard]] static UuidIdAdapter fromLegacy(std::string value) {
353 if (value.empty()) return {};
354 UuidIdAdapter result;
356 result.state_.spelling = std::move(value);
357 result.state_.canonical = false;
358 return result;
359 }
360
362 [[nodiscard]] const std::string& value() const noexcept { return state_.spelling; }
363
365 [[nodiscard]] const std::string& format() const noexcept { return state_.spelling; }
366
368 [[nodiscard]] Uuid uuid() const noexcept { return Uuid(state_.uuid); }
369
371 [[nodiscard]] std::string canonicalFormat() const { return detail::formatUuidBytes(state_.uuid); }
372
374 [[nodiscard]] bool empty() const noexcept { return state_.spelling.empty(); }
375
377 [[nodiscard]] bool isNil() const noexcept { return state_.spelling.empty() || Uuid(state_.uuid).isNil(); }
378
380 [[nodiscard]] bool isCanonicalUuid() const noexcept { return state_.canonical; }
381
383 [[nodiscard]] std::uint64_t hash() const noexcept { return Uuid(state_.uuid).hash(); }
384
386 explicit operator bool() const noexcept { return !empty(); }
387
389 friend bool operator==(const UuidIdAdapter& lhs, const UuidIdAdapter& rhs) noexcept {
390 return lhs.state_.uuid == rhs.state_.uuid;
391 }
392
394 friend auto operator<=>(const UuidIdAdapter& lhs, const UuidIdAdapter& rhs) noexcept {
395 return lhs.state_.uuid <=> rhs.state_.uuid;
396 }
397
398private:
399 detail::UuidIdState state_;
400};
401
412public:
414 LogicalId() = default;
415
422 [[nodiscard]] static std::optional<LogicalId> parse(std::string_view text);
423
430 [[nodiscard]] static std::optional<LogicalId> fromParts(std::string_view namespaceName, std::string_view name);
431
433 [[nodiscard]] bool isValid() const noexcept { return !value_.empty(); }
434
436 [[nodiscard]] const std::string& format() const noexcept { return value_; }
437
439 [[nodiscard]] std::string_view namespaceName() const noexcept;
440
442 [[nodiscard]] std::string_view name() const noexcept;
443
448 [[nodiscard]] std::uint64_t hash() const noexcept;
449
450 friend bool operator==(const LogicalId&, const LogicalId&) noexcept = default;
451
452private:
453 explicit LogicalId(std::string value) : value_(std::move(value)) {}
454
455 std::string value_;
456};
457
466using UuidEntropySource = std::function<bool(std::span<std::uint8_t> bytes)>;
467
469using UuidClock = std::function<std::chrono::system_clock::time_point()>;
470
479public:
485 explicit UuidV7Generator(UuidEntropySource entropy, UuidClock clock = {});
486
491 [[nodiscard]] std::optional<PersistentId> generate() const;
492
498 [[nodiscard]] std::optional<PersistentId> generate(std::chrono::system_clock::time_point timestamp) const;
499
500private:
501 UuidEntropySource entropy_;
502 UuidClock clock_;
503};
504
511[[nodiscard]] EVENGINE_API_FOUNDATION std::optional<PersistentId> generateUuidV7(
512 std::chrono::system_clock::time_point timestamp, const UuidEntropySource& entropy);
513
514} // namespace eve
515
516namespace std {
517
518template <typename Tag>
520struct hash<eve::detail::Id128<Tag>> {
522 std::size_t operator()(const eve::detail::Id128<Tag>& value) const noexcept {
523 return static_cast<std::size_t>(value.hash());
524 }
525};
526
527template <typename Tag>
529struct hash<eve::UuidIdAdapter<Tag>> {
531 std::size_t operator()(const eve::UuidIdAdapter<Tag>& value) const noexcept {
532 return static_cast<std::size_t>(value.hash());
533 }
534};
535
536template <>
538struct hash<eve::LogicalId> {
540 std::size_t operator()(const eve::LogicalId& value) const noexcept {
541 return static_cast<std::size_t>(value.hash());
542 }
543};
544
545} // namespace std
double value
std::array< std::uint8_t, 32 > hash
Definition Evpack.cpp:172
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
#define EVENGINE_API
宿主(eve / libmain)导出宏;插件从进程导入同一批符号。
Definition Export.h:31
std::string text
std::int32_t parent
std::uint64_t bytes
std::string name
int detail
Human-readable, scoped identifier in the form namespace:name.
Definition Identity.h:411
const std::string & format() const noexcept
Returns the canonical namespace:name representation.
Definition Identity.h:436
bool isValid() const noexcept
Returns whether this value contains a valid namespace and name.
Definition Identity.h:433
LogicalId()=default
Constructs an empty, invalid logical ID.
UUID-backed identifier adapter for legacy textual boundaries.
Definition Identity.h:314
bool isNil() const noexcept
True when the value is an empty or UUID nil identity.
Definition Identity.h:377
UuidIdAdapter(const char *value)
Constructs from a legacy spelling or canonical UUID text.
Definition Identity.h:323
friend bool operator==(const UuidIdAdapter &lhs, const UuidIdAdapter &rhs) noexcept
Operator ==.
Definition Identity.h:389
static UuidIdAdapter fromUuid(Uuid value)
Builds an adapter from an explicit canonical UUID.
Definition Identity.h:343
bool isCanonicalUuid() const noexcept
True when the input was canonical UUID text rather than legacy text.
Definition Identity.h:380
UuidIdAdapter(std::string value)
Constructs from a legacy spelling or canonical UUID text.
Definition Identity.h:326
const std::string & value() const noexcept
Returns the exact legacy or canonical compatibility spelling.
Definition Identity.h:362
StrongUuid< Tag > Uuid
Definition Identity.h:316
friend auto operator<=>(const UuidIdAdapter &lhs, const UuidIdAdapter &rhs) noexcept
Orders adapter values by their canonical UUID projection.
Definition Identity.h:394
typename Uuid::Bytes Bytes
Definition Identity.h:317
std::string canonicalFormat() const
Returns the canonical UUID projection as lower-case UUID text.
Definition Identity.h:371
Uuid uuid() const noexcept
Returns the canonical UUID projection.
Definition Identity.h:368
bool empty() const noexcept
True when no compatibility spelling has been assigned.
Definition Identity.h:374
UuidIdAdapter(std::string_view value)
Constructs from a legacy spelling or canonical UUID text.
Definition Identity.h:329
std::uint64_t hash() const noexcept
Stable hash of the canonical UUID projection.
Definition Identity.h:383
static std::optional< UuidIdAdapter > parse(std::string_view value)
Parses canonical UUID text or a non-empty legacy spelling.
Definition Identity.h:336
const std::string & format() const noexcept
Returns the compatibility spelling; use canonicalFormat at persistence boundaries.
Definition Identity.h:365
static UuidIdAdapter fromLegacy(std::string value)
Builds an adapter while explicitly retaining a legacy spelling.
Definition Identity.h:352
UuidIdAdapter()=default
Constructs an empty compatibility identifier.
Injectable UUIDv7 generator.
Definition Identity.h:478
Id128 public API.
Definition Identity.h:112
std::string format() const
Formats the ID in lower-case canonical UUID text.
Definition Identity.h:173
friend constexpr bool operator==(const Id128 &, const Id128 &) noexcept=default
Operator ==.
static std::optional< Id128 > fromBytes(std::span< const std::uint8_t > bytes) noexcept
Constructs an ID from exactly 16 bytes.
Definition Identity.h:161
std::array< std::uint8_t, 16 > Bytes
Definition Identity.h:114
static constexpr Id128 nil() noexcept
Returns the all-zero nil value.
Definition Identity.h:142
constexpr bool isNil() const noexcept
Returns whether this value is the all-zero nil ID.
Definition Identity.h:176
static std::optional< Id128 > parse(std::string_view text) noexcept
Parses canonical UUID text.
Definition Identity.h:151
friend constexpr auto operator<=>(const Id128 &lhs, const Id128 &rhs) noexcept
Orders UUID bytes in network order for ordered containers.
Definition Identity.h:223
constexpr Id128() noexcept=default
Constructs the nil value.
Id128 child(std::string_view role) const noexcept
Derives a deterministic child UUID for a hierarchical identity.
Definition Identity.h:192
static constexpr Id128 fromUuid(const Id128< OtherTag > &value) noexcept
Re-tags an existing UUID at an explicit domain boundary.
Definition Identity.h:136
constexpr std::uint64_t hash() const noexcept
Returns a stable non-cryptographic hash for hash tables.
Definition Identity.h:210
constexpr Bytes bytes() const noexcept
Returns the UUID bytes in network order.
Definition Identity.h:202
Tag-free state of a compatibility-aware UUID identity adapter.
Definition Identity.h:87
void assign(std::string_view text)
Assigns from canonical UUID text or from a legacy spelling.
Definition Identity.cpp:187
static UuidBytes projectLegacy(std::string_view text) noexcept
Builds the deterministic UUID projection of a legacy spelling.
Definition Identity.cpp:200
std::array< std::uint8_t, 16 > UuidBytes
Byte representation shared by every tagged 128-bit identity.
Definition Identity.h:54
std::string formatUuidBytes(const UuidBytes &bytes)
Formats UUID bytes as lower-case canonical 8-4-4-4-12 text.
Definition Identity.cpp:160
UuidBytes childUuidBytes(const UuidBytes &parent, std::string_view role) noexcept
Derives a deterministic child UUID from a parent and a role.
Definition Identity.cpp:172
std::optional< UuidBytes > parseUuidText(std::string_view text) noexcept
Parses canonical UUID text without knowing the domain tag.
Definition Identity.cpp:134
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16
std::function< std::chrono::system_clock::time_point()> UuidClock
Clock callback used by the injectable UUIDv7 generator.
Definition Identity.h:469
std::function< bool(std::span< std::uint8_t > bytes)> UuidEntropySource
Entropy callback used by UUIDv7 generation.
Definition Identity.h:466
std::optional< PersistentId > generateUuidV7(std::chrono::system_clock::time_point timestamp, const UuidEntropySource &entropy)
Constructs a UUIDv7 without owning a clock or generator object.
Definition Identity.cpp:89
ArtifactIdTag public API.
Definition Identity.h:41
AssetGuidTag public API.
Definition Identity.h:35
CommandIdTag public API.
Definition Identity.h:45
ContentIdTag public API.
Definition Identity.h:33
DocumentIdTag public API.
Definition Identity.h:37
EffectIdTag public API.
Definition Identity.h:51
EventIdTag public API.
Definition Identity.h:43
OperationIdTag public API.
Definition Identity.h:49
PersistentIdTag public API.
Definition Identity.h:31
SceneObjectIdTag public API.
Definition Identity.h:39
TransactionIdTag public API.
Definition Identity.h:47