33class LA_SCENE_API Value
36 using Array = std::vector<Value>;
38 using Object = std::map<std::string, Value>;
39 using Buffer = std::vector<unsigned char>;
40 using variant_type = std::variant<bool, int, double, std::string, Buffer, Array, Object>;
54 template <
typename T, std::
size_t index = 0>
57 if constexpr (index == std::variant_size_v<variant_type>) {
59 }
else if constexpr (std::is_same_v<std::variant_alternative_t<index, variant_type>, T>) {
66 static constexpr size_t bool_index() {
return variant_index<bool>(); }
67 static constexpr size_t int_index() {
return variant_index<int>(); }
68 static constexpr size_t real_index() {
return variant_index<double>(); }
69 static constexpr size_t string_index() {
return variant_index<std::string>(); }
70 static constexpr size_t buffer_index() {
return variant_index<Buffer>(); }
71 static constexpr size_t array_index() {
return variant_index<Array>(); }
72 static constexpr size_t object_index() {
return variant_index<Object>(); }
74 static Value create_buffer() {
return Value(Buffer()); }
75 static Value create_array() {
return Value(Array()); }
76 static Value create_object() {
return Value(Object()); }
79 explicit Value(
bool b) { value = b; }
80 explicit Value(
int i) { value = i; }
81 explicit Value(
double n) { value = n; }
82 explicit Value(std::string s) { value = std::move(s); }
83 explicit Value(std::string_view s) { value = std::string(s); }
84 explicit Value(
const char* s) { value = std::string(s); }
85 explicit Value(span<unsigned char> s)
88 std::memcpy(vec.data(), s.data(), s.size());
89 value = std::move(vec);
91 explicit Value(
const Buffer& v) { value = v; }
92 explicit Value(Buffer&& v) { value = std::move(v); }
93 explicit Value(
const Array& a) { value = a; }
94 explicit Value(Array&& a) { value = std::move(a); }
95 explicit Value(
const Object& o) { value = o; }
96 explicit Value(Object&& o) { value = std::move(o); }
101 return std::holds_alternative<T>(value);
103 bool is_bool()
const {
return is_type<bool>(); }
104 bool is_int()
const {
return is_type<int>(); }
105 bool is_real()
const {
return is_type<double>(); }
106 bool is_number()
const {
return is_int() || is_real(); }
107 bool is_string()
const {
return is_type<std::string>(); }
108 bool is_buffer()
const {
return is_type<Buffer>(); }
109 bool is_array()
const {
return is_type<Array>(); }
110 bool is_object()
const {
return is_type<Object>(); }
111 size_t get_type_index()
const {
return value.index(); }
113 template <
typename T>
116 return std::get<T>(value);
118 template <
typename T>
121 return std::get<T>(value);
123 bool get_bool()
const {
return get<bool>(); }
124 int get_int()
const {
return get<int>(); }
125 double get_real()
const {
return get<double>(); }
126 const std::string& get_string()
const {
return get<std::string>(); }
127 std::string get_string() {
return get<std::string>(); }
128 const Buffer& get_buffer()
const {
return get<Buffer>(); }
129 Buffer& get_buffer() {
return get<Buffer>(); }
130 const Array& get_array()
const {
return get<Array>(); }
131 Array& get_array() {
return get<Array>(); }
132 const Object& get_object()
const {
return get<Object>(); }
133 Object& get_object() {
return get<Object>(); }
135 template <
typename T>
140 void set_bool(
bool b) { value = b; }
141 void set_int(
int i) { value = i; }
142 void set_real(
double n) { value = n; }
145 const Value& operator[](
size_t idx)
const
148 return get_array()[idx];
150 Value& operator[](
size_t idx)
153 return get_array()[idx];
157 bool has(
const std::string& key)
const {
return get_object().find(key) != get_object().end(); }
158 const Value& operator[](
const std::string& key)
const
161 return get_object().find(key)->second;
163 Value& operator[](
const std::string& key)
166 return get_object().find(key)->second;
172 if (is_string())
return get_string().size();
173 if (is_buffer())
return get_buffer().size();
174 if (is_array())
return get_array().size();
175 if (is_object())
return get_object().size();
std::unordered_map< std::string, std::any > user_data
A map of extensions as user-defined objects, stored in an std::any.
Definition SceneExtension.h:204
std::unordered_map< std::string, Value > data
A map of extensions as json-like Value objects.
Definition SceneExtension.h:198