载入中...
搜索中...
未找到
ResourceRef.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
9#include "common/Identity.h"
10#include "common/Uri.h"
11
12#include <cstdint>
13#include <functional>
14#include <ostream>
15#include <string_view>
16#include <utility>
17#include <variant>
18
19namespace eve {
20
22enum class ResourceRefKind : std::uint8_t {
23 Asset,
24 Uri,
26 Object,
27};
28
30[[nodiscard]] constexpr std::string_view resourceRefKindName(ResourceRefKind kind) noexcept {
31 switch (kind) {
32 case ResourceRefKind::Asset: return "asset";
33 case ResourceRefKind::Uri: return "uri";
34 case ResourceRefKind::Definition: return "definition";
35 case ResourceRefKind::Object: return "object";
36 }
37 return "unknown";
38}
39
41inline std::ostream& operator<<(std::ostream& stream, ResourceRefKind kind) {
43 return stream << resourceRefKindName(kind);
44}
45
54public:
56 [[nodiscard]] static Result<AssetRef> parse(std::string_view text);
57
59 [[nodiscard]] static Result<AssetRef> fromId(PersistentId id);
60
62 [[nodiscard]] static Result<AssetRef> fromUri(const ResourceUri& uri);
63
65 [[nodiscard]] const PersistentId& id() const noexcept { return id_; }
66
68 [[nodiscard]] std::string format() const;
69
71 friend bool operator==(const AssetRef&, const AssetRef&) noexcept = default;
72
73private:
74 explicit AssetRef(PersistentId id) : id_(id) {}
75
76 PersistentId id_;
77};
78
86public:
88 DefinitionRef() = default;
89
91 [[nodiscard]] static Result<DefinitionRef> parse(std::string_view text);
92
94 [[nodiscard]] static Result<DefinitionRef> fromId(LogicalId id);
95
97 [[nodiscard]] const LogicalId& id() const noexcept { return id_; }
98
100 [[nodiscard]] const std::string& format() const noexcept { return id_.format(); }
101
103 friend bool operator==(const DefinitionRef&, const DefinitionRef&) noexcept = default;
104
105private:
106 explicit DefinitionRef(LogicalId id) : id_(std::move(id)) {}
107
108 LogicalId id_;
109};
110
118public:
120 [[nodiscard]] static Result<ObjectRef> parse(std::string_view text);
121
123 [[nodiscard]] static Result<ObjectRef> fromId(PersistentId id);
124
126 [[nodiscard]] const PersistentId& id() const noexcept { return id_; }
127
129 [[nodiscard]] std::string format() const { return id_.format(); }
130
132 friend bool operator==(const ObjectRef&, const ObjectRef&) noexcept = default;
133
134private:
135 explicit ObjectRef(PersistentId id) : id_(id) {}
136
137 PersistentId id_;
138};
139
149public:
151 [[nodiscard]] static Result<ResourceRef> parse(std::string_view text);
152
154 [[nodiscard]] static ResourceRef fromAsset(AssetRef value) { return ResourceRef(std::move(value)); }
155
157 [[nodiscard]] static ResourceRef fromUri(ResourceUri value) { return ResourceRef(std::move(value)); }
158
160 [[nodiscard]] static ResourceRef fromDefinition(DefinitionRef value) { return ResourceRef(std::move(value)); }
161
163 [[nodiscard]] static ResourceRef fromObject(ObjectRef value) { return ResourceRef(std::move(value)); }
164
166 [[nodiscard]] ResourceRefKind kind() const noexcept;
167
177 [[nodiscard]] const AssetRef* asset() const noexcept;
178
188 [[nodiscard]] const ResourceUri* uri() const noexcept;
189
199 [[nodiscard]] const DefinitionRef* definition() const noexcept;
200
210 [[nodiscard]] const ObjectRef* object() const noexcept;
211
213 friend bool operator==(const ResourceRef&, const ResourceRef&) noexcept = default;
214
215private:
216 explicit ResourceRef(AssetRef value) : value_(std::move(value)) {}
217 explicit ResourceRef(ResourceUri value) : value_(std::move(value)) {}
218 explicit ResourceRef(DefinitionRef value) : value_(std::move(value)) {}
219 explicit ResourceRef(ObjectRef value) : value_(std::move(value)) {}
220
221 std::variant<AssetRef, ResourceUri, DefinitionRef, ObjectRef> value_;
222};
223
224} // namespace eve
225
226namespace std {
227
228template <>
230struct hash<eve::AssetRef> {
232 std::size_t operator()(const eve::AssetRef& value) const noexcept {
233 return std::hash<eve::PersistentId>{}(value.id());
234 }
235};
236
237template <>
239struct hash<eve::DefinitionRef> {
241 std::size_t operator()(const eve::DefinitionRef& value) const noexcept {
242 return std::hash<eve::LogicalId>{}(value.id());
243 }
244};
245
246template <>
248struct hash<eve::ObjectRef> {
250 std::size_t operator()(const eve::ObjectRef& value) const noexcept {
251 return std::hash<eve::PersistentId>{}(value.id());
252 }
253};
254
255} // 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
Strong identity values shared by persistence, content and logical-name APIs.
std::string text
TokenKind kind
std::string id
Definition PlayHost.cpp:108
std::string uri
LogicalId definition
Strict, canonical resource URI values shared by engine modules.
Stable asset identity backed by PersistentId.
Definition ResourceRef.h:53
friend bool operator==(const AssetRef &, const AssetRef &) noexcept=default
Compare asset references by persistent identity.
const PersistentId & id() const noexcept
Return the stable asset identity.
Definition ResourceRef.h:65
Stable definition reference backed by LogicalId.
Definition ResourceRef.h:85
DefinitionRef()=default
Constructs an invalid reference for optional/component storage.
friend bool operator==(const DefinitionRef &, const DefinitionRef &) noexcept=default
Compare definition references by logical identity.
const std::string & format() const noexcept
Return canonical namespace:name text.
const LogicalId & id() const noexcept
Return the stable logical definition identity.
Definition ResourceRef.h:97
Human-readable, scoped identifier in the form namespace:name.
Definition Identity.h:411
Stable world/object identity backed by PersistentId.
std::string format() const
Return canonical UUID text without a resource URI scheme.
const PersistentId & id() const noexcept
Return the stable object identity.
friend bool operator==(const ObjectRef &, const ObjectRef &) noexcept=default
Compare object references by persistent identity.
Type-safe union of the reference forms accepted by resource-facing APIs.
static ResourceRef fromObject(ObjectRef value)
Wrap an object reference in the generic reference union.
static ResourceRef fromDefinition(DefinitionRef value)
Wrap a definition reference in the generic reference union.
static ResourceRef fromUri(ResourceUri value)
Wrap a resource URI in the generic reference union.
static ResourceRef fromAsset(AssetRef value)
Wrap an asset reference in the generic reference union.
A semantically named resource locator.
Definition Uri.h:101
Move-only operation result carrying either a value or Status.
Definition Result.h:155
An immutable, canonical URI value.
Definition Uri.h:44
Id128 public API.
Definition Identity.h:112
eve::DefinitionRef DefinitionRef
Compatibility names for the canonical common definition identity.
Definition Definitions.h:27
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16
std::ostream & operator<<(std::ostream &stream, Severity severity)
Writes a severity for diagnostics and test output.
Definition Diagnostic.h:39
ResourceRefKind
The alternative held by a generic ResourceRef.
Definition ResourceRef.h:22
constexpr std::string_view resourceRefKindName(ResourceRefKind kind) noexcept
Returns the stable resource-reference alternative spelling.
Definition ResourceRef.h:30