载入中...
搜索中...
未找到
StateValue.cpp
浏览该文件的文档.
1#include "common/StateValue.h"
2
3#include <cctype>
4#include <utility>
5
6namespace eve {
7namespace {
8
13bool parseSegment(const std::string& seg, std::string& key, std::vector<size_t>& indexes) {
14 key.clear();
15 indexes.clear();
16 if (seg.empty()) return false;
17
18 size_t i = 0;
19 if (seg[0] != '[') {
20 while (i < seg.size() && seg[i] != '[') key.push_back(seg[i++]);
21 }
22 if (key.empty() && (i >= seg.size())) return false;
23
24 while (i < seg.size()) {
25 if (seg[i] != '[') return false;
26 const size_t close = seg.find(']', i);
27 if (close == std::string::npos) return false;
28 const std::string idx = seg.substr(i + 1, close - i - 1);
29 if (idx.empty() || idx.find_first_not_of("0123456789") != std::string::npos) return false;
30 indexes.push_back(static_cast<size_t>(std::stoull(idx)));
31 i = close + 1;
32 }
33 return true;
34}
35
36} // namespace
37
39 switch (value.kind()) {
41 case StateValue::Kind::Int: return Value::integer(value.asInt());
42 case StateValue::Kind::Float: return Value::number(value.asDouble());
43 case StateValue::Kind::Bool: return Value::boolean(value.asBool());
44 case StateValue::Kind::String: return Value::string(value.asString());
46 Value::Array array;
47 array.reserve(value.arraySize());
48 for (size_t i = 0; i < value.arraySize(); ++i) array.push_back(toCanonicalStateValue(value.at(i)));
49 return Value::array(std::move(array));
50 }
53 for (const auto& key : value.keys()) object.emplace(key, toCanonicalStateValue(*value.find(key)));
54 return Value::object(std::move(object));
55 }
56 }
57 return Value::null();
58}
59
61 switch (value.type()) {
63 case Value::Type::Int64: return StateValue::integer(value.asInt());
64 case Value::Type::Double: return StateValue::number(value.asDouble());
65 case Value::Type::Bool: return StateValue::boolean(value.asBool());
66 case Value::Type::String: return StateValue::string(value.asString());
67 case Value::Type::Array: {
69 for (size_t i = 0; i < value.arraySize(); ++i) array.pushBack(toLegacyStateValue(value.at(i)));
70 return array;
71 }
74 for (const auto& [key, member] : *value.getIf<Value::Object>()) object.set(key, toLegacyStateValue(member));
75 return object;
76 }
77 }
78 return StateValue::null();
79}
80
83 sv.i_ = v;
84 return sv;
85}
86
89 sv.f_ = v;
90 return sv;
91}
92
95 sv.b_ = v;
96 return sv;
97}
98
101 sv.s_ = std::move(v);
102 return sv;
103}
104
105void StateValue::pushBack(StateValue v) { arr_.push_back(std::move(v)); }
106
107void StateValue::set(const std::string& key, StateValue v) {
108 for (auto& kv : obj_) {
109 if (kv.first == key) {
110 kv.second = std::move(v);
111 return;
112 }
113 }
114 obj_.emplace_back(key, std::move(v));
115}
116
117const StateValue* StateValue::find(const std::string& key) const {
118 for (const auto& kv : obj_)
119 if (kv.first == key) return &kv.second;
120 return nullptr;
121}
122
123StateValue* StateValue::find(const std::string& key) {
124 for (auto& kv : obj_)
125 if (kv.first == key) return &kv.second;
126 return nullptr;
127}
128
129std::vector<std::string> StateValue::keys() const {
130 std::vector<std::string> out;
131 out.reserve(obj_.size());
132 for (const auto& kv : obj_) out.push_back(kv.first);
133 return out;
134}
135
136const StateValue* StateValue::get(const std::string& dottedPath) const {
137 const StateValue* cur = this;
138 size_t start = 0;
139 while (true) {
140 const size_t dot = dottedPath.find('.', start);
141 const bool last = (dot == std::string::npos);
142 const std::string seg = dottedPath.substr(start, last ? std::string::npos : dot - start);
143
144 std::string key;
145 std::vector<size_t> indexes;
146 if (!parseSegment(seg, key, indexes)) return nullptr;
147 if (!key.empty()) {
148 cur = cur->find(key);
149 if (!cur) return nullptr;
150 }
151 for (const size_t idx : indexes) {
152 if (!cur->isArray() || idx >= cur->arr_.size()) return nullptr;
153 cur = &cur->arr_[idx];
154 }
155 if (last) return cur;
156 start = dot + 1;
157 }
158}
159
160StateValue* StateValue::get(const std::string& dottedPath) {
161 return const_cast<StateValue*>(static_cast<const StateValue*>(this)->get(dottedPath));
162}
163
164bool StateValue::setPath(const std::string& dottedPath, StateValue v) {
165 std::vector<std::string> segs;
166 size_t start = 0;
167 while (true) {
168 const size_t dot = dottedPath.find('.', start);
169 if (dot == std::string::npos) {
170 segs.push_back(dottedPath.substr(start));
171 break;
172 }
173 segs.push_back(dottedPath.substr(start, dot - start));
174 start = dot + 1;
175 }
176 if (segs.empty() || (segs.size() == 1 && segs[0].empty())) return false;
177
178 StateValue* cur = this;
179 for (size_t si = 0; si + 1 < segs.size(); ++si) {
180 std::string key;
181 std::vector<size_t> indexes;
182 // Intermediate segments must be plain object keys; array traversal is
183 // only supported on the final segment.
184 if (!parseSegment(segs[si], key, indexes) || key.empty() || !indexes.empty()) return false;
185 if (!cur->isObject()) return false;
186 StateValue* next = cur->find(key);
187 if (!next) {
188 cur->set(key, StateValue::object());
189 next = cur->find(key);
190 }
191 cur = next;
192 }
193
194 std::string key;
195 std::vector<size_t> indexes;
196 if (!parseSegment(segs.back(), key, indexes)) return false;
197 if (!indexes.empty()) {
198 if (!key.empty()) {
199 if (!cur->isObject()) return false;
200 const StateValue* arr = cur->find(key);
201 if (!arr || !arr->isArray()) return false;
202 cur = cur->find(key);
203 }
204 if (!cur->isArray() || indexes.size() != 1 || indexes[0] >= cur->arr_.size()) return false;
205 cur->arr_[indexes[0]] = std::move(v);
206 return true;
207 }
208 if (!cur->isObject() || key.empty()) return false;
209 cur->set(key, std::move(v));
210 return true;
211}
212
214 if (!isObject() || !defaults.isObject()) return false;
215 bool changed = false;
216 for (const auto& kv : defaults.obj_) {
217 StateValue* mine = find(kv.first);
218 if (!mine) {
219 set(kv.first, kv.second);
220 changed = true;
221 } else if (mine->isObject() && kv.second.isObject()) {
222 changed = mine->mergeDefaults(kv.second) || changed;
223 }
224 }
225 return changed;
226}
227
228bool StateValue::operator==(const StateValue& o) const {
229 if (kind_ != o.kind_) return false;
230 switch (kind_) {
231 case Kind::Null: return true;
232 case Kind::Int: return i_ == o.i_;
233 case Kind::Float: return f_ == o.f_;
234 case Kind::Bool: return b_ == o.b_;
235 case Kind::String: return s_ == o.s_;
236 case Kind::Array: return arr_ == o.arr_;
237 case Kind::Object:
238 if (obj_.size() != o.obj_.size()) return false;
239 for (const auto& [key, value] : obj_) {
240 const auto* other = o.find(key);
241 if (!other || value != *other) return false;
242 }
243 return true;
244 }
245 return false;
246}
247
248} // namespace eve
Duration start
bool changed
std::vector< Value > defaults
Definition ECS.cpp:49
SQObjectPtr key
Definition ECS.cpp:155
float v
int idx
Object::Ptr value
ScriptStructure out
Json object
float(ui::Theme::* member)[4]
JSON-compatible state value tree used by state hot reload.
Definition StateValue.h:30
static StateValue object()
Empty object.
Definition StateValue.h:50
static StateValue boolean(bool v)
Boolean value.
static StateValue array()
Empty array.
Definition StateValue.h:48
bool setPath(const std::string &dottedPath, StateValue v)
Set a value at a dotted path, creating missing intermediate objects along the way.
static StateValue integer(int64_t v)
Integer value.
bool mergeDefaults(const StateValue &defaults)
Fill missing members from defaults; object members merge recursively, existing values are never overw...
void pushBack(StateValue v)
Append an element; only valid on arrays.
const StateValue * get(const std::string &dottedPath) const
Navigate a dotted path ("a.b[2].c").
bool isObject() const
Definition StateValue.h:60
bool isArray() const
Definition StateValue.h:61
bool operator==(const StateValue &o) const
Compare typed values; object key order is ignored, array order is significant. @cost Scalars are cons...
static StateValue number(double v)
Floating-point value.
const StateValue * find(const std::string &key) const
Look up key; nullptr when absent.
static StateValue null()
Null value.
Definition StateValue.h:38
std::vector< std::string > keys() const
Member names in insertion order.
static StateValue string(std::string v)
String value.
void set(const std::string &key, StateValue v)
Insert or replace key; only valid on objects.
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
static Value null()
Compatibility factory for a null value.
Definition Value.h:113
std::map< std::string, Value > Object
Definition Value.h:34
static Value number(double value)
Compatibility factory for a Double value.
Definition Value.h:117
static Value string(std::string value)
Compatibility factory for a string value.
Definition Value.h:121
std::vector< Value > Array
Definition Value.h:33
static Value array(Array value)
Compatibility factory for an array value.
Definition Value.cpp:128
static Value integer(std::int64_t value)
Compatibility factory for an Int64 value.
Definition Value.h:115
static Value boolean(bool value)
Compatibility factory for a boolean value.
Definition Value.h:119
static Value object(Object value)
Compatibility factory for an object value.
Definition Value.cpp:129
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16
StateValue toLegacyStateValue(const Value &value)
Project a canonical value into the legacy reload snapshot format.
Value toCanonicalStateValue(const StateValue &value)
Copy a legacy reload tree into the canonical owning value protocol.