载入中...
搜索中...
未找到
Identity.h
浏览该文件的文档.
1#pragma once
2
12#include "common/Export.h"
13
14#include <array>
15#include <compare>
16#include <cstddef>
17#include <cstdint>
18#include <functional>
19#include <optional>
20#include <span>
21#include <string>
22#include <string_view>
23#include <utility>
24
25namespace eve {
26
27namespace detail {
28
32struct ContentIdTag {};
34struct AssetGuidTag {};
36struct DocumentIdTag {};
40struct ArtifactIdTag {};
42struct EventIdTag {};
44struct CommandIdTag {};
50struct EffectIdTag {};
51
53using UuidBytes = std::array<std::uint8_t, 16>;
54
66[[nodiscard]] EVENGINE_API std::optional<UuidBytes> parseUuidText(std::string_view text) noexcept;
67
69[[nodiscard]] EVENGINE_API std::string formatUuidBytes(const UuidBytes& bytes);
70
72[[nodiscard]] EVENGINE_API UuidBytes childUuidBytes(const UuidBytes& parent, std::string_view role) noexcept;
73
87public:
95 EVENGINE_API void assign(std::string_view text);
96
102 [[nodiscard]] EVENGINE_API static UuidBytes projectLegacy(std::string_view text) noexcept;
103
104 std::string spelling;
106 bool canonical = false;
107};
108
109template <typename Tag>
111class Id128 {
112public:
113 using Bytes = std::array<std::uint8_t, 16>;
114
116 constexpr Id128() noexcept = default;
117
122 explicit constexpr Id128(Bytes bytes) noexcept : bytes_(bytes) {}
123
133 template <typename OtherTag>
135 [[nodiscard]] static constexpr Id128 fromUuid(const Id128<OtherTag>& value) noexcept {
137 return Id128(value.bytes());
138 }
139
141 [[nodiscard]] static constexpr Id128 nil() noexcept { return {}; }
142
150 [[nodiscard]] static std::optional<Id128> parse(std::string_view text) noexcept {
151 if (const auto bytes = parseUuidText(text)) return Id128(*bytes);
152 return std::nullopt;
153 }
154
160 [[nodiscard]] static std::optional<Id128> fromBytes(std::span<const std::uint8_t> bytes) noexcept {
161 if (bytes.size() != 16) return std::nullopt;
162 Bytes result{};
163 for (std::size_t i = 0; i < result.size(); ++i) result[i] = bytes[i];
165 return Id128(result);
166 }
167
172 [[nodiscard]] std::string format() const { return formatUuidBytes(bytes_); }
173
175 [[nodiscard]] constexpr bool isNil() const noexcept {
176 for (const auto byte : bytes_) {
177 if (byte != 0) return false;
178 }
179 return true;
180 }
181
191 [[nodiscard]] Id128 child(std::string_view role) const noexcept {
192 if (isNil() || role.empty()) return Id128::nil();
194 return Id128(childUuidBytes(bytes_, role));
195 }
196
201 [[nodiscard]] constexpr Bytes bytes() const noexcept { return bytes_; }
202
209 [[nodiscard]] constexpr std::uint64_t hash() const noexcept {
210 std::uint64_t result = 14695981039346656037ull;
211 for (const auto byte : bytes_) {
212 result ^= byte;
213 result *= 1099511628211ull;
214 }
215 return result;
216 }
217
219 friend constexpr bool operator==(const Id128&, const Id128&) noexcept = default;
220
222 friend constexpr auto operator<=>(const Id128& lhs, const Id128& rhs) noexcept { return lhs.bytes_ <=> rhs.bytes_; }
223
224private:
225 [[nodiscard]] static constexpr std::optional<std::uint8_t> hexValue(char value) noexcept {
226 if (value >= '0' && value <= '9') return static_cast<std::uint8_t>(value - '0');
227 if (value >= 'a' && value <= 'f') return static_cast<std::uint8_t>(value - 'a' + 10);
228 if (value >= 'A' && value <= 'F') return static_cast<std::uint8_t>(value - 'A' + 10);
229 return std::nullopt;
230 }
231
232 Bytes bytes_{};
233};
234
235} // namespace detail
236
243template <typename Tag>
245
254
264
273
276
279
282
285
288
291
294
297
311template <typename Tag>
314public:
316 using Bytes = typename Uuid::Bytes;
317
319 UuidIdAdapter() = default;
320 UuidIdAdapter(const UuidIdAdapter&) = default;
322 UuidIdAdapter(UuidIdAdapter&&) noexcept = default;
323 UuidIdAdapter& operator=(UuidIdAdapter&&) noexcept = default;
324
326 explicit UuidIdAdapter(const char* value) : UuidIdAdapter(std::string_view(value ? value : "")) {}
327
329 explicit UuidIdAdapter(std::string value) : UuidIdAdapter(std::string_view(value)) {}
330
332 explicit UuidIdAdapter(std::string_view value) { state_.assign(value); }
333
339 [[nodiscard]] static std::optional<UuidIdAdapter> parse(std::string_view value) {
340 if (value.empty()) return std::nullopt;
342 return UuidIdAdapter(value);
343 }
344
346 [[nodiscard]] static UuidIdAdapter fromUuid(Uuid value) {
348 result.state_.uuid = value.bytes();
349 result.state_.spelling = value.format();
350 result.state_.canonical = true;
351 return result;
352 }
353
355 [[nodiscard]] static UuidIdAdapter fromLegacy(std::string value) {
356 if (value.empty()) return {};
359 result.state_.spelling = std::move(value);
360 result.state_.canonical = false;
361 return result;
362 }
363
365 [[nodiscard]] const std::string& value() const noexcept { return state_.spelling; }
366
368 [[nodiscard]] const std::string& format() const noexcept { return state_.spelling; }
369
371 [[nodiscard]] Uuid uuid() const noexcept { return Uuid(state_.uuid); }
372
374 [[nodiscard]] std::string canonicalFormat() const { return detail::formatUuidBytes(state_.uuid); }
375
377 [[nodiscard]] bool empty() const noexcept { return state_.spelling.empty(); }
378
380 [[nodiscard]] bool isNil() const noexcept { return state_.spelling.empty() || Uuid(state_.uuid).isNil(); }
381
383 [[nodiscard]] bool isCanonicalUuid() const noexcept { return state_.canonical; }
384
386 [[nodiscard]] std::uint64_t hash() const noexcept { return Uuid(state_.uuid).hash(); }
387
389 explicit operator bool() const noexcept { return !empty(); }
390
392 friend bool operator==(const UuidIdAdapter& lhs, const UuidIdAdapter& rhs) noexcept {
393 return lhs.state_.uuid == rhs.state_.uuid;
394 }
395
397 friend auto operator<=>(const UuidIdAdapter& lhs, const UuidIdAdapter& rhs) noexcept {
398 return lhs.state_.uuid <=> rhs.state_.uuid;
399 }
400
401private:
402 detail::UuidIdState state_;
403};
404
415public:
417 LogicalId() = default;
418
425 [[nodiscard]] static std::optional<LogicalId> parse(std::string_view text);
426
433 [[nodiscard]] static std::optional<LogicalId> fromParts(std::string_view namespaceName, std::string_view name);
434
436 [[nodiscard]] bool isValid() const noexcept { return !value_.empty(); }
437
439 [[nodiscard]] const std::string& format() const noexcept { return value_; }
440
442 [[nodiscard]] std::string_view namespaceName() const noexcept;
443
445 [[nodiscard]] std::string_view name() const noexcept;
446
451 [[nodiscard]] std::uint64_t hash() const noexcept;
452
453 friend bool operator==(const LogicalId&, const LogicalId&) noexcept = default;
454
455private:
456 explicit LogicalId(std::string value) : value_(std::move(value)) {}
457
458 std::string value_;
459};
460
461} // namespace eve
462
463namespace std {
464
465template <typename Tag>
467struct hash<eve::detail::Id128<Tag>> {
469 std::size_t operator()(const eve::detail::Id128<Tag>& value) const noexcept {
470 return static_cast<std::size_t>(value.hash());
471 }
472};
473
474template <typename Tag>
476struct hash<eve::UuidIdAdapter<Tag>> {
478 std::size_t operator()(const eve::UuidIdAdapter<Tag>& value) const noexcept {
479 return static_cast<std::size_t>(value.hash());
480 }
481};
482
483template <>
485struct hash<eve::LogicalId> {
487 std::size_t operator()(const eve::LogicalId& value) const noexcept {
488 return static_cast<std::size_t>(value.hash());
489 }
490};
491
492} // namespace std
std::string name
Definition ECS.cpp:154
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
#define EVENGINE_API
宿主(eve / libmain)导出宏;插件从进程导入同一批符号。
Definition Export.h:31
const ColourScope * parent
Poco::JSON::Object::Ptr result
std::uint64_t bytes
Object::Ptr value
std::string hash
int detail
std::string text
Human-readable, scoped identifier in the form namespace:name.
Definition Identity.h:414
const std::string & format() const noexcept
Returns the canonical namespace:name representation.
Definition Identity.h:439
bool isValid() const noexcept
Returns whether this value contains a valid namespace and name.
Definition Identity.h:436
LogicalId()=default
Constructs an empty, invalid logical ID.
UUID-backed identifier adapter for legacy textual boundaries.
Definition Identity.h:313
bool isNil() const noexcept
True when the value is an empty or UUID nil identity.
Definition Identity.h:380
friend bool operator==(const UuidIdAdapter &lhs, const UuidIdAdapter &rhs) noexcept
Operator ==.
Definition Identity.h:392
static UuidIdAdapter fromUuid(Uuid value)
Builds an adapter from an explicit canonical UUID.
Definition Identity.h:346
bool isCanonicalUuid() const noexcept
True when the input was canonical UUID text rather than legacy text.
Definition Identity.h:383
UuidIdAdapter(std::string value)
Constructs from a legacy spelling or canonical UUID text.
Definition Identity.h:329
UuidIdAdapter(const UuidIdAdapter &)=default
const std::string & value() const noexcept
Returns the exact legacy or canonical compatibility spelling.
Definition Identity.h:365
StrongUuid< Tag > Uuid
Definition Identity.h:315
friend auto operator<=>(const UuidIdAdapter &lhs, const UuidIdAdapter &rhs) noexcept
Orders adapter values by their canonical UUID projection.
Definition Identity.h:397
typename Uuid::Bytes Bytes
Definition Identity.h:316
std::string canonicalFormat() const
Returns the canonical UUID projection as lower-case UUID text.
Definition Identity.h:374
Uuid uuid() const noexcept
Returns the canonical UUID projection.
Definition Identity.h:371
UuidIdAdapter(UuidIdAdapter &&) noexcept=default
bool empty() const noexcept
True when no compatibility spelling has been assigned.
Definition Identity.h:377
UuidIdAdapter(std::string_view value)
Constructs from a legacy spelling or canonical UUID text.
Definition Identity.h:332
std::uint64_t hash() const noexcept
Stable hash of the canonical UUID projection.
Definition Identity.h:386
static std::optional< UuidIdAdapter > parse(std::string_view value)
Parses canonical UUID text or a non-empty legacy spelling.
Definition Identity.h:339
UuidIdAdapter & operator=(const UuidIdAdapter &)=default
const std::string & format() const noexcept
Returns the compatibility spelling; use canonicalFormat at persistence boundaries.
Definition Identity.h:368
static UuidIdAdapter fromLegacy(std::string value)
Builds an adapter while explicitly retaining a legacy spelling.
Definition Identity.h:355
UuidIdAdapter()=default
Constructs an empty compatibility identifier.
Id128 public API.
Definition Identity.h:111
std::string format() const
Formats the ID in lower-case canonical UUID text.
Definition Identity.h:172
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:160
std::array< std::uint8_t, 16 > Bytes
Definition Identity.h:113
static constexpr Id128 nil() noexcept
Returns the all-zero nil value.
Definition Identity.h:141
constexpr bool isNil() const noexcept
Returns whether this value is the all-zero nil ID.
Definition Identity.h:175
static std::optional< Id128 > parse(std::string_view text) noexcept
Parses canonical UUID text.
Definition Identity.h:150
friend constexpr auto operator<=>(const Id128 &lhs, const Id128 &rhs) noexcept
Orders UUID bytes in network order for ordered containers.
Definition Identity.h:222
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:191
static constexpr Id128 fromUuid(const Id128< OtherTag > &value) noexcept
Re-tags an existing UUID at an explicit domain boundary.
Definition Identity.h:135
constexpr std::uint64_t hash() const noexcept
Returns a stable non-cryptographic hash for hash tables.
Definition Identity.h:209
constexpr Bytes bytes() const noexcept
Returns the UUID bytes in network order.
Definition Identity.h:201
Tag-free state of a compatibility-aware UUID identity adapter.
Definition Identity.h:86
void assign(std::string_view text)
Assigns from canonical UUID text or from a legacy spelling.
Definition Identity.cpp:188
static UuidBytes projectLegacy(std::string_view text) noexcept
Builds the deterministic UUID projection of a legacy spelling.
Definition Identity.cpp:201
std::array< std::uint8_t, 16 > UuidBytes
Byte representation shared by every tagged 128-bit identity.
Definition Identity.h:53
std::string formatUuidBytes(const UuidBytes &bytes)
Formats UUID bytes as lower-case canonical 8-4-4-4-12 text.
Definition Identity.cpp:161
UuidBytes childUuidBytes(const UuidBytes &parent, std::string_view role) noexcept
Derives a deterministic child UUID from a parent and a role.
Definition Identity.cpp:173
std::optional< UuidBytes > parseUuidText(std::string_view text) noexcept
Parses canonical UUID text without knowing the domain tag.
Definition Identity.cpp:135
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16
ArtifactIdTag public API.
Definition Identity.h:40
AssetGuidTag public API.
Definition Identity.h:34
CommandIdTag public API.
Definition Identity.h:44
ContentIdTag public API.
Definition Identity.h:32
DocumentIdTag public API.
Definition Identity.h:36
EffectIdTag public API.
Definition Identity.h:50
EventIdTag public API.
Definition Identity.h:42
OperationIdTag public API.
Definition Identity.h:48
PersistentIdTag public API.
Definition Identity.h:30
SceneObjectIdTag public API.
Definition Identity.h:38
TransactionIdTag public API.
Definition Identity.h:46