载入中...
搜索中...
未找到
EffectTypes.cpp
浏览该文件的文档.
2
3#include <algorithm>
4#include <cmath>
5#include <utility>
6
7namespace eve::effects {
8 // namespace
9
10void EffectPayload::setString(const std::string& key, const std::string& value) {
11 if (!key.empty()) values_[key] = eve::Value(value);
12}
13
14void EffectPayload::setNumber(const std::string& key, double value) {
15 if (key.empty() || !std::isfinite(value)) return;
16 values_[key] = eve::Value(value);
17}
18
19void EffectPayload::setBool(const std::string& key, bool value) {
20 if (!key.empty()) values_[key] = eve::Value(value);
21}
22
23void EffectPayload::setNull(const std::string& key) {
24 if (!key.empty()) values_[key] = eve::Value();
25}
26
27eve::Result<void> EffectPayload::setJson(const std::string& key, const std::string& json) {
28 if (key.empty()) return eve::Result<void>::failure(eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument, "effect payload key must not be empty"));
29 auto parsed = eve::Value::fromJson(json);
30 if (!parsed.ok()) return eve::Result<void>::failure(parsed.status());
31 values_[key] = std::move(parsed).takeValue();
33}
34
35bool EffectPayload::has(const std::string& key) const { return values_.contains(key); }
36
38 if (key.empty()) return eve::Result<void>::failure(eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument, "effect payload key must not be empty"));
39 const bool removed = values_.erase(key) != 0;
41}
42
43std::string EffectPayload::getJson(const std::string& key) const {
44 const auto it = values_.find(key);
45 if (it == values_.end()) return {};
46 auto result = it->second.toJson();
47 if (!result) return {};
48 return std::move(result).takeValue();
49}
50
51std::string EffectPayload::toJson() const {
52 auto result = eve::Value(values_).toJson();
53 if (!result) return {};
54 return std::move(result).takeValue();
55}
56
57const eve::Value::Object& EffectPayload::object() const noexcept { return values_; }
58
59void EffectPayload::clear() { values_.clear(); }
60
62 if (id.empty()) return eve::Result<void>::failure(eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument, "effect definition id must not be empty"));
63 if (!std::isfinite(duration)) return eve::Result<void>::failure(eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument, "effect definition duration must be finite"));
64 if (!std::isfinite(period) || period < 0.0)
65 return eve::Result<void>::failure(eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument, "effect definition period must be finite and non-negative"));
66 if (!std::isfinite(magnitude)) return eve::Result<void>::failure(eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument, "effect definition magnitude must be finite"));
67 if (stackCount == 0) return eve::Result<void>::failure(eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument, "effect definition stack count must be positive"));
70 return eve::Result<void>::failure(eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument, "effect definition stack count exceeds its maximum"));
71 }
73}
74
76 if (tag.empty()) return eve::Result<void>::failure(eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument, "effect tag must not be empty"));
77 const auto it = std::lower_bound(tags.begin(), tags.end(), tag);
79 tags.insert(it, tag);
81}
82
84 const auto it = std::lower_bound(tags.begin(), tags.end(), tag);
86 tags.erase(it);
88}
89
90bool EffectInstance::hasTag(const std::string& tag) const { return std::binary_search(tags.begin(), tags.end(), tag); }
91
92int EffectInstance::tagCount() const { return static_cast<int>(tags.size()); }
93
94std::string EffectInstance::tagAt(int index) const {
95 return index < 0 || static_cast<std::size_t>(index) >= tags.size() ? std::string{}
96 : tags[static_cast<std::size_t>(index)];
97}
98
99std::string policyName(StackPolicy policy) {
100 switch (policy) {
101 case StackPolicy::Replace: return "replace";
102 case StackPolicy::Stack: return "stack";
103 case StackPolicy::Refresh: return "refresh";
104 }
105 return "unknown";
106}
107
108bool parsePolicy(const std::string& name, StackPolicy& policy) {
109 if (name == "replace")
110 policy = StackPolicy::Replace;
111 else if (name == "stack")
112 policy = StackPolicy::Stack;
113 else if (name == "refresh")
114 policy = StackPolicy::Refresh;
115 else
116 return false;
117 return true;
118}
119
121 switch (kind) {
122 case EffectEventKind::Applied: return "applied";
123 case EffectEventKind::Refreshed: return "refreshed";
124 case EffectEventKind::Stacked: return "stacked";
125 case EffectEventKind::Periodic: return "periodic";
126 case EffectEventKind::Expired: return "expired";
127 case EffectEventKind::Removed: return "removed";
128 }
129 return "unknown";
130}
131
132} // namespace eve::effects
double value
Definition, policy, instance and lifecycle value types for effects.
std::uint32_t key
TokenKind kind
std::string name
const std::string * tag
int removed
uint32_t index
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
Result< std::string > toJson() const
Serialize this value as deterministic compact JSON.
Definition Value.cpp:67
static Result< Value > fromJson(std::string_view json)
Parse one strict JSON value into an owning Value.
Definition Value.cpp:57
std::map< std::string, Value > Object
Definition Value.h:34
std::string toJson() const
Serializes all fields as a deterministic JSON object.
bool has(const std::string &key) const
Returns whether a field exists.
void setBool(const std::string &key, bool value)
Sets a JSON boolean field.
void setString(const std::string &key, const std::string &value)
Sets a JSON string field.
void setNull(const std::string &key)
Sets a JSON null field.
eve::Result< void > setJson(const std::string &key, const std::string &json)
Parses JSON text and stores its typed Value at the field.
const eve::Value::Object & object() const noexcept
Returns the read-only canonical Value object owned by this payload.
std::string getJson(const std::string &key) const
Serializes a field's typed Value to JSON text, or returns empty when absent/invalid.
eve::Result< void > erase(const std::string &key)
Removes a field; an absent field is an explicit NoOp result.
void setNumber(const std::string &key, double value)
Sets a JSON number field.
void clear()
Removes every field.
StackPolicy
Compatibility policy used by the original effects facade.
Definition EffectTypes.h:28
bool parsePolicy(const std::string &name, StackPolicy &policy)
Parses a lowercase legacy stack policy name.
std::string eventKindName(EffectEventKind kind)
Returns the stable lowercase name of an event kind.
std::string policyName(StackPolicy policy)
Returns the stable lowercase name of a legacy stack policy.
EffectEventKind
Lifecycle event kind emitted by an effect container.
double period
Period in seconds for domain executor settlement; zero disables periodic ticks.
eve::Result< void > validate() const
Validates definition fields without changing any state.
std::string tagAt(int index) const
Returns a tag by lexical index, or an empty string.
eve::Result< void > removeTag(const std::string &tag)
Removes a tag; an absent tag is an explicit NoOp result.
eve::Result< void > addTag(const std::string &tag)
Adds a tag while retaining deterministic lexical order.
bool hasTag(const std::string &tag) const
Returns whether this effect has a tag.
int tagCount() const
Returns the number of tags.
std::vector< std::string > tags
StackCountPolicy stackCount
Definition EffectTypes.h:55