载入中...
搜索中...
未找到
StateValue.h
浏览该文件的文档.
1#pragma once
2
3#include "common/Export.h"
4
5#include <cstdint>
6#include <string>
7#include <utility>
8#include <vector>
9
10#include "common/Value.h"
11
12namespace eve {
13
14class StateValue;
15
17[[nodiscard]] EVENGINE_API_FOUNDATION Value toCanonicalStateValue(const StateValue& value);
18
20[[nodiscard]] EVENGINE_API_FOUNDATION StateValue toLegacyStateValue(const Value& value);
21
31public:
33 enum class Kind { Null, Int, Float, Bool, String, Array, Object };
34
36 StateValue() = default;
38 static StateValue null() { return StateValue(); }
40 static StateValue integer(int64_t v);
42 static StateValue number(double v);
44 static StateValue boolean(bool v);
46 static StateValue string(std::string v);
48 static StateValue array() { return StateValue(Kind::Array); }
50 static StateValue object() { return StateValue(Kind::Object); }
51
53 Kind kind() const { return kind_; }
54
55 bool isNull() const { return kind_ == Kind::Null; }
56 bool isInt() const { return kind_ == Kind::Int; }
57 bool isFloat() const { return kind_ == Kind::Float; }
58 bool isBool() const { return kind_ == Kind::Bool; }
59 bool isString() const { return kind_ == Kind::String; }
60 bool isObject() const { return kind_ == Kind::Object; }
61 bool isArray() const { return kind_ == Kind::Array; }
62
64 int64_t asInt() const { return i_; }
66 double asDouble() const { return f_; }
68 bool asBool() const { return b_; }
70 const std::string& asString() const { return s_; }
71
73 void pushBack(StateValue v);
75 size_t arraySize() const { return arr_.size(); }
77 StateValue& at(size_t index) { return arr_.at(index); }
78 const StateValue& at(size_t index) const { return arr_.at(index); }
79
81 void set(const std::string& key, StateValue v);
83 const StateValue* find(const std::string& key) const;
84 StateValue* find(const std::string& key);
86 std::vector<std::string> keys() const;
87
92 const StateValue* get(const std::string& dottedPath) const;
93 StateValue* get(const std::string& dottedPath);
94
101 bool setPath(const std::string& dottedPath, StateValue v);
102
108 bool mergeDefaults(const StateValue& defaults);
109
115 bool operator==(const StateValue& o) const;
116 bool operator!=(const StateValue& o) const { return !(*this == o); }
117
118private:
119 explicit StateValue(Kind kind) : kind_(kind) {}
120
121 Kind kind_ = Kind::Null;
122 int64_t i_ = 0;
123 double f_ = 0.0;
124 bool b_ = false;
125 std::string s_;
126 std::vector<StateValue> arr_;
127 std::vector<std::pair<std::string, StateValue>> obj_;
128};
129
130} // namespace eve
std::vector< Value > defaults
Definition ECS.cpp:49
SQObjectPtr key
Definition ECS.cpp:155
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
float v
Object::Ptr value
double number
std::string kind
bool boolean
std::string string
uint32_t index
Owning, renderer-independent dynamic values.
JSON-compatible state value tree used by state hot reload.
Definition StateValue.h:30
bool isFloat() const
Definition StateValue.h:57
static StateValue object()
Empty object.
Definition StateValue.h:50
Kind kind() const
The kind of the stored value.
Definition StateValue.h:53
bool operator!=(const StateValue &o) const
Definition StateValue.h:116
static StateValue array()
Empty array.
Definition StateValue.h:48
int64_t asInt() const
Integer payload; only valid when kind() == Kind::Int.
Definition StateValue.h:64
bool asBool() const
Boolean payload; only valid when kind() == Kind::Bool.
Definition StateValue.h:68
bool isNull() const
Definition StateValue.h:55
size_t arraySize() const
Element count; only valid on arrays.
Definition StateValue.h:75
bool isString() const
Definition StateValue.h:59
bool isBool() const
Definition StateValue.h:58
bool isObject() const
Definition StateValue.h:60
double asDouble() const
Floating-point payload; only valid when kind() == Kind::Float.
Definition StateValue.h:66
bool isArray() const
Definition StateValue.h:61
const std::string & asString() const
String payload; only valid when kind() == Kind::String.
Definition StateValue.h:70
Kind
Kind public API.
Definition StateValue.h:33
const StateValue & at(size_t index) const
Definition StateValue.h:78
static StateValue null()
Null value.
Definition StateValue.h:38
StateValue()=default
Null value.
StateValue & at(size_t index)
Indexed element (bounds-checked); only valid on arrays.
Definition StateValue.h:77
bool isInt() const
Definition StateValue.h:56
std::variant< std::monostate, std::int64_t, double, std::string, bool > Value
Definition Database.h:27
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16
enum EVENGINE_API_FOUNDATION Float
OT_FLOAT.
Definition Runtime.h:118
enum EVENGINE_API_FOUNDATION Bool
OT_BOOL.
Definition Runtime.h:116
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.
enum EVENGINE_API_FOUNDATION String
OT_STRING.
Definition Runtime.h:119
enum EVENGINE_API_FOUNDATION Array
OT_ARRAY (editable via Runtime array APIs / ReflectedPropertyModel).
Definition Runtime.h:120