载入中...
搜索中...
未找到
Value.h
浏览该文件的文档.
1#pragma once
2
8#include "common/Export.h"
9#include "common/Result.h"
10
11#include <cstddef>
12#include <cstdint>
13#include <initializer_list>
14#include <map>
15#include <string>
16#include <string_view>
17#include <utility>
18#include <variant>
19#include <vector>
20
21namespace eve {
22
32public:
33 using Array = std::vector<Value>;
34 using Object = std::map<std::string, Value>;
35 using Storage = std::variant<std::monostate, bool, std::int64_t, double, std::string, Array, Object>;
36
38 enum class Type : std::uint8_t {
39 Null = 0,
40 Bool,
41 Int64,
42 Double,
43 String,
44 Array,
45 Object,
47 Integer = Int64,
49 Number = Double,
51 Int = Int64,
53 Float = Double,
54 };
55
57 using Kind = Type;
58
60 Value() noexcept;
62 Value(std::nullptr_t);
64 Value(bool value);
66 Value(std::int64_t value);
68 Value(int value);
70 Value(double value);
72 Value(float value);
74 Value(const char* value);
76 Value(std::string value);
81
83 ~Value() noexcept;
85 Value(const Value& other);
87 Value(Value&& other) noexcept;
89 Value& operator=(const Value& other);
91 Value& operator=(Value&& other) noexcept;
92
94 Type type() const noexcept { return static_cast<Type>(storage_.index()); }
96 Type kind() const noexcept { return type(); }
98 bool isNull() const noexcept { return type() == Type::Null; }
100 bool isBool() const noexcept { return type() == Type::Bool; }
102 bool isInt64() const noexcept { return type() == Type::Int64; }
104 bool isDouble() const noexcept { return type() == Type::Double; }
106 bool isString() const noexcept { return type() == Type::String; }
108 bool isArray() const noexcept { return type() == Type::Array; }
110 bool isObject() const noexcept { return type() == Type::Object; }
111
113 static Value null() { return Value(); }
115 static Value integer(std::int64_t value) { return Value(value); }
117 static Value number(double value) { return Value(value); }
119 static Value boolean(bool value) { return Value(value); }
121 static Value string(std::string value) { return Value(std::move(value)); }
123 static Value array(Array value);
125 static Value object(Object value);
134 [[nodiscard]] static Value array(std::initializer_list<Value> values);
143 [[nodiscard]] static Value object(std::initializer_list<Object::value_type> members);
144
146 std::int64_t asInt() const;
148 double asDouble() const;
150 bool asBool() const;
152 const std::string& asString() const;
153
155 void pushBack(Value value);
157 std::size_t arraySize() const;
159 Value& at(std::size_t index);
161 const Value& at(std::size_t index) const;
163 void set(const std::string& key, Value value);
173 Value* find(const std::string& key) noexcept;
183 const Value* find(const std::string& key) const noexcept;
185 std::vector<std::string> keys() const;
186
188 bool isNumeric() const noexcept { return isInt64() || isDouble(); }
190 std::string typeName() const;
192 std::string toString() const;
193
195 const Storage& storage() const noexcept { return storage_; }
197 Storage& storage() noexcept { return storage_; }
198
208 template <class T>
209 const T* getIf() const noexcept {
210 return std::get_if<T>(&storage_);
211 }
212
222 template <class T>
223 T* getIf() noexcept {
224 return std::get_if<T>(&storage_);
225 }
226
234 [[nodiscard]] static Result<Value> fromJson(std::string_view json);
235
243 [[nodiscard]] Result<std::string> toJson() const;
244
246 bool operator==(const Value&) const;
247
248private:
249 Storage storage_;
250};
251
252} // namespace eve
253
254// One compiled owner for the canonical containers. Member templates remain
255// available in consumers; their non-template operations share foundation code.
256#if defined(_WIN32) && defined(EVENGINE_MODULE_DLL) && !defined(EVENGINE_EXPORTS_FOUNDATION)
257extern template class __declspec(dllimport) std::vector<eve::Value>;
258extern template class __declspec(dllimport) std::map<std::string, eve::Value>;
259#else
260extern template class std::vector<eve::Value>;
261extern template class std::map<std::string, eve::Value>;
262#endif
263
264// Foundation shares common Windows DLL getters. The variant and directly
265// borrowed pointers retain their original layout, lifetime and noexcept contract.
266#if defined(_WIN32) && defined(EVENGINE_MODULE_DLL) && !defined(EVENGINE_EXPORTS_FOUNDATION)
267extern template __declspec(dllimport) std::monostate* eve::Value::getIf<std::monostate>() noexcept;
268extern template __declspec(dllimport) const std::monostate* eve::Value::getIf<std::monostate>() const noexcept;
269extern template __declspec(dllimport) bool* eve::Value::getIf<bool>() noexcept;
270extern template __declspec(dllimport) const bool* eve::Value::getIf<bool>() const noexcept;
271extern template __declspec(dllimport) std::int64_t* eve::Value::getIf<std::int64_t>() noexcept;
272extern template __declspec(dllimport) const std::int64_t* eve::Value::getIf<std::int64_t>() const noexcept;
273extern template __declspec(dllimport) double* eve::Value::getIf<double>() noexcept;
274extern template __declspec(dllimport) const double* eve::Value::getIf<double>() const noexcept;
275extern template __declspec(dllimport) std::string* eve::Value::getIf<std::string>() noexcept;
276extern template __declspec(dllimport) const std::string* eve::Value::getIf<std::string>() const noexcept;
277extern template __declspec(dllimport) eve::Value::Array* eve::Value::getIf<eve::Value::Array>() noexcept;
278extern template __declspec(dllimport) const eve::Value::Array* eve::Value::getIf<eve::Value::Array>() const noexcept;
279extern template __declspec(dllimport) eve::Value::Object* eve::Value::getIf<eve::Value::Object>() noexcept;
280extern template __declspec(dllimport) const eve::Value::Object* eve::Value::getIf<eve::Value::Object>() const noexcept;
281#endif
SQObjectPtr key
Definition ECS.cpp:155
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
std::set< std::string > values
Object::Ptr value
Move-only, checked operation results for the common layer.
Json object
std::map< std::string, std::span< const std::uint8_t > > members
std::size_t at
uint32_t index
Move-only operation result carrying either a value or Status.
Definition Result.h:176
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
bool isInt64() const noexcept
Return true when this value is an Int64.
Definition Value.h:102
static Value null()
Compatibility factory for a null value.
Definition Value.h:113
const Storage & storage() const noexcept
Return the immutable variant storage for visitors.
Definition Value.h:195
Type kind() const noexcept
Compatibility spelling for the former dialogue data tree.
Definition Value.h:96
std::map< std::string, Value > Object
Definition Value.h:34
bool isNumeric() const noexcept
Return whether this value is an integer or finite number.
Definition Value.h:188
bool isArray() const noexcept
Return true when this value is an array.
Definition Value.h:108
static Value number(double value)
Compatibility factory for a Double value.
Definition Value.h:117
bool isString() const noexcept
Return true when this value is a string.
Definition Value.h:106
static Value string(std::string value)
Compatibility factory for a string value.
Definition Value.h:121
Type
Runtime kind of the owned value.
Definition Value.h:38
Value() noexcept
Construct a null value.
std::vector< Value > Array
Definition Value.h:33
Storage & storage() noexcept
Return mutable variant storage for owning edits.
Definition Value.h:197
static Value integer(std::int64_t value)
Compatibility factory for an Int64 value.
Definition Value.h:115
std::variant< std::monostate, bool, std::int64_t, double, std::string, Array, Object > Storage
Definition Value.h:35
bool isNull() const noexcept
Return true when this value is null.
Definition Value.h:98
static Value boolean(bool value)
Compatibility factory for a boolean value.
Definition Value.h:119
const T * getIf() const noexcept
Return a typed pointer, or nullptr when the kind differs.
Definition Value.h:209
bool isBool() const noexcept
Return true when this value is a boolean.
Definition Value.h:100
bool isObject() const noexcept
Return true when this value is an object.
Definition Value.h:110
bool isDouble() const noexcept
Return true when this value is a Double.
Definition Value.h:104
T * getIf() noexcept
Return a mutable typed pointer, or nullptr when the kind differs.
Definition Value.h:223
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
enum EVENGINE_API_FOUNDATION Integer
OT_INTEGER.
Definition Runtime.h:117
enum EVENGINE_API_FOUNDATION String
OT_STRING.
Definition Runtime.h:119