载入中...
搜索中...
未找到
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() = default;
62 Value(std::nullptr_t) {}
64 Value(bool value) : storage_(value) {}
66 Value(std::int64_t value) : storage_(value) {}
68 Value(int value) : storage_(static_cast<std::int64_t>(value)) {}
70 Value(double value) : storage_(value) {}
72 Value(float value) : storage_(static_cast<double>(value)) {}
74 Value(const char* value) : storage_(std::string(value ? value : "")) {}
76 Value(std::string value) : storage_(std::move(value)) {}
78 Value(Array value) : storage_(std::move(value)) {}
80 Value(Object value) : storage_(std::move(value)) {}
81
83 Type type() const noexcept { return static_cast<Type>(storage_.index()); }
85 Type kind() const noexcept { return type(); }
87 bool isNull() const noexcept { return type() == Type::Null; }
89 bool isBool() const noexcept { return type() == Type::Bool; }
91 bool isInt64() const noexcept { return type() == Type::Int64; }
93 bool isDouble() const noexcept { return type() == Type::Double; }
95 bool isString() const noexcept { return type() == Type::String; }
97 bool isArray() const noexcept { return type() == Type::Array; }
99 bool isObject() const noexcept { return type() == Type::Object; }
100
102 static Value null() { return Value(); }
104 static Value integer(std::int64_t value) { return Value(value); }
106 static Value number(double value) { return Value(value); }
108 static Value boolean(bool value) { return Value(value); }
110 static Value string(std::string value) { return Value(std::move(value)); }
112 static Value array(Array value) { return Value(std::move(value)); }
114 static Value object(Object value) { return Value(std::move(value)); }
115
117 std::int64_t asInt() const;
119 double asDouble() const;
121 bool asBool() const;
123 const std::string& asString() const;
124
126 void pushBack(Value value);
128 std::size_t arraySize() const;
130 Value& at(std::size_t index);
132 const Value& at(std::size_t index) const;
134 void set(const std::string& key, Value value);
144 Value* find(const std::string& key) noexcept;
154 const Value* find(const std::string& key) const noexcept;
156 std::vector<std::string> keys() const;
157
159 bool isNumeric() const noexcept { return isInt64() || isDouble(); }
161 std::string typeName() const;
163 std::string toString() const;
164
166 const Storage& storage() const noexcept { return storage_; }
168 Storage& storage() noexcept { return storage_; }
169
179 template <class T>
180 const T* getIf() const noexcept {
181 return std::get_if<T>(&storage_);
182 }
183
193 template <class T>
194 T* getIf() noexcept {
195 return std::get_if<T>(&storage_);
196 }
197
205 [[nodiscard]] static Result<Value> fromJson(std::string_view json);
206
214 [[nodiscard]] Result<std::string> toJson() const;
215
217 bool operator==(const Value&) const = default;
218
219private:
220 Storage storage_;
221};
222
223} // namespace eve
double value
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
std::uint32_t key
Move-only, checked operation results for the common layer.
std::string string
uint32_t index
std::size_t at
Move-only operation result carrying either a value or Status.
Definition Result.h:155
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
Value(std::nullptr_t)
Construct a null value from nullptr.
Definition Value.h:62
Value(Object value)
Construct an object value by taking its members.
Definition Value.h:80
bool isInt64() const noexcept
Return true when this value is an Int64.
Definition Value.h:91
static Value null()
Compatibility factory for a null value.
Definition Value.h:102
const Storage & storage() const noexcept
Return the immutable variant storage for visitors.
Definition Value.h:166
Type kind() const noexcept
Compatibility spelling for the former dialogue data tree.
Definition Value.h:85
Value(Array value)
Construct an array value by taking its elements.
Definition Value.h:78
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:159
Value(std::int64_t value)
Construct an Int64 value without narrowing.
Definition Value.h:66
bool isArray() const noexcept
Return true when this value is an array.
Definition Value.h:97
Value(bool value)
Construct a boolean value.
Definition Value.h:64
Value(double value)
Construct a Double value; non-finite values serialize as errors.
Definition Value.h:70
static Value number(double value)
Compatibility factory for a Double value.
Definition Value.h:106
Value()=default
Construct a null value.
bool isString() const noexcept
Return true when this value is a string.
Definition Value.h:95
static Value string(std::string value)
Compatibility factory for a string value.
Definition Value.h:110
Type
Runtime kind of the owned value.
Definition Value.h:38
static Value array(Array value)
Compatibility factory for an array value.
Definition Value.h:112
Value(int value)
Construct an integer value using the canonical Int64 type.
Definition Value.h:68
std::vector< Value > Array
Definition Value.h:33
Storage & storage() noexcept
Return mutable variant storage for owning edits.
Definition Value.h:168
static Value integer(std::int64_t value)
Compatibility factory for an Int64 value.
Definition Value.h:104
Value(const char *value)
Construct a string value, copying the supplied text.
Definition Value.h:74
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:87
static Value boolean(bool value)
Compatibility factory for a boolean value.
Definition Value.h:108
static Value object(Object value)
Compatibility factory for an object value.
Definition Value.h:114
const T * getIf() const noexcept
Return a typed pointer, or nullptr when the kind differs.
Definition Value.h:180
bool isBool() const noexcept
Return true when this value is a boolean.
Definition Value.h:89
bool isObject() const noexcept
Return true when this value is an object.
Definition Value.h:99
Value(float value)
Construct a Double value from a float.
Definition Value.h:72
bool operator==(const Value &) const =default
Compare two values recursively, including their numeric kinds.
Value(std::string value)
Construct a string value by taking the supplied text.
Definition Value.h:76
bool isDouble() const noexcept
Return true when this value is a Double.
Definition Value.h:93
Type type() const noexcept
Return the active value kind.
Definition Value.h:83
T * getIf() noexcept
Return a mutable typed pointer, or nullptr when the kind differs.
Definition Value.h:194
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