载入中...
搜索中...
未找到
StateAccessAdapter.cpp
浏览该文件的文档.
2
3#include <string>
4#include <utility>
5
6namespace eve::tags {
7namespace {
8
9eve::Result<eve::MutationReceipt> failure(eve::DiagnosticCode code, std::string message, std::string path = {}) {
12}
13
14bool isTagKey(std::string_view key) { return key.starts_with("tag.") || key.starts_with("tag:"); }
15
16std::string tagName(std::string_view key) {
17 if (key.starts_with("tag.") || key.starts_with("tag:")) key.remove_prefix(4);
18 return std::string(key);
19}
20
21} // namespace
22
23std::optional<eve::Value> TagStoreStateAdapter::value(std::string_view subject, std::string_view key) const {
24 if (subject.empty() || key.empty() || !isTagKey(key)) return std::nullopt;
25 return eve::Value(store_.hasTag(std::string(subject), tagName(key)));
26}
27
28std::optional<bool> TagStoreStateAdapter::hasTag(std::string_view subject, std::string_view tag) const {
29 if (subject.empty() || tag.empty()) return std::nullopt;
30 return store_.hasTag(std::string(subject), std::string(tag));
31}
32
33std::optional<eve::Value> TagStoreStateAdapter::state(std::string_view subject, std::string_view key) const {
34 return value(subject, key);
35}
36
37eve::Result<eve::MutationReceipt> TagStoreStateAdapter::apply(std::span<const eve::StateMutation> mutations,
39 for (std::size_t index = 0; index < mutations.size(); ++index) {
40 const auto& mutation = mutations[index];
41 if (mutation.persistent)
43 "TagStore mutations are volatile; use StatePatch for persistence",
44 "mutations[" + std::to_string(index) + "]");
45 if (mutation.subject.empty() || mutation.key.empty())
46 return failure(eve::DiagnosticCode::InvalidArgument, "tag mutation requires subject and tag",
47 "mutations[" + std::to_string(index) + "]");
48 if ((mutation.kind == eve::MutationKind::Set) && (!isTagKey(mutation.key) || !mutation.value.isBool()))
49 return failure(eve::DiagnosticCode::InvalidArgument, "tag Set requires a tag.* key and boolean value",
50 "mutations[" + std::to_string(index) + "]");
51 if (mutation.kind == eve::MutationKind::Set || mutation.kind == eve::MutationKind::AddTag ||
52 mutation.kind == eve::MutationKind::RemoveTag)
53 continue;
54 return failure(eve::DiagnosticCode::Unsupported, "TagStore supports Set, AddTag, and RemoveTag only",
55 "mutations[" + std::to_string(index) + "]");
56 }
57
58 TagStore candidate = store_;
59 std::size_t changed = 0;
60 for (const auto& mutation : mutations) {
61 const std::string tag = tagName(mutation.key);
62 bool didChange = false;
63 switch (mutation.kind) {
64 case eve::MutationKind::Set:
65 didChange = mutation.value.asBool() ? candidate.addTag(mutation.subject, tag)
66 : candidate.removeTag(mutation.subject, tag);
67 break;
68 case eve::MutationKind::AddTag: didChange = candidate.addTag(mutation.subject, tag); break;
69 case eve::MutationKind::RemoveTag: didChange = candidate.removeTag(mutation.subject, tag); break;
70 case eve::MutationKind::Remove:
71 case eve::MutationKind::AddNumber: break;
72 }
73 if (didChange) ++changed;
74 }
75 store_ = std::move(candidate);
79}
80
81} // namespace eve::tags
double value
int subject
Definition AnimSmr.cpp:163
std::string message
DiagnosticCode code
std::uint32_t key
const std::string * tag
std::string path
Definition PlayHost.cpp:110
uint32_t index
const VegetationPresetContext & context
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={}, DiagnosticDetails details={}, std::string source={})
Construct an error diagnostic with the standard error severity.
Definition Diagnostic.h:125
Move-only operation result carrying either a value or Status.
Definition Result.h:155
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:164
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:175
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.h:81
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
std::optional< eve::Value > state(std::string_view subject, std::string_view key) const override
Query a persistent/world state value.
eve::Result< eve::MutationReceipt > apply(std::span< const eve::StateMutation > mutations, const eve::MutationContext &context) override
Apply a complete mutation set atomically.
std::optional< eve::Value > value(std::string_view subject, std::string_view key) const override
Query a generic world value.
std::optional< bool > hasTag(std::string_view subject, std::string_view tag) const override
Query exact tag membership.
Deterministic storage for generic tags and capabilities.
Definition TagStore.h:27
bool removeTag(const SubjectId &subject, const std::string &tag)
Removes a tag from a subject.
Definition TagStore.cpp:53
bool hasTag(const SubjectId &subject, const std::string &tag) const
Tests whether a subject has an exact tag.
Definition TagStore.cpp:56
bool addTag(const SubjectId &subject, const std::string &tag)
Adds a tag to a subject.
Definition TagStore.cpp:50
DiagnosticCode
Stable machine-readable diagnostic codes.
Definition Diagnostic.h:47
Correlation metadata shared by a mutation transaction.
Definition StateAccess.h:57
Observable result of an all-or-nothing mutation request.
Definition StateAccess.h:64
TagStore implementation of the common world-state contracts.