Tramway SDK
value.h
Go to the documentation of this file.
1// Tramway Drifting and Dungeon Exploration Simulator SDK Runtime
2
3#ifndef TRAM_SDK_FRAMEWORK_VALUE_H
4#define TRAM_SDK_FRAMEWORK_VALUE_H
5
6#include <cassert>
7
8#include <framework/type.h>
9#include <framework/uid.h>
10#include <framework/math.h>
11#include <framework/logging.h>
12
13namespace tram {
14
15class ValuePtr;
16
18class Value {
19public:
20 Value() : vec4_value({0, 0, 0, 0}) { type = TYPE_UNDEFINED; }
21 ~Value() {}
22
23 Value(const Value& other) : vec4_value(other.vec4_value) /* hehe */ { type = other.type; }
24
25 Value& operator=(const Value& other) {
26 vec4_value = other.vec4_value;
27 type = other.type; return *this;
28 }
29
30 bool operator==(const Value& other) const {
31 switch (type) {
32 case TYPE_BOOL:
33 if (other.GetType() != TYPE_BOOL) return false; else return bool_value == other.bool_value;
34 case TYPE_INT32:
35 case TYPE_UINT32:
36 if (!other.IsInt() && !other.IsFloat()) return false; else return GetInt() == other.GetInt();
37 case TYPE_FLOAT32:
38 if (!other.IsInt() && !other.IsFloat()) return false; else return GetFloat() == other.GetFloat();
39 case TYPE_NAME:
40 if (other.GetType() != TYPE_NAME) return false; else return name_value == other.name_value;
41 case TYPE_STRING:
42 return false; // TODO: fix this
43 case TYPE_VEC2:
44 if (other.GetType() != TYPE_VEC2) return false; else return vec2_value == other.vec2_value;
45 case TYPE_VEC3:
46 if (other.GetType() != TYPE_VEC3) return false; else return vec3_value == other.vec3_value;
47 case TYPE_VEC4:
48 if (other.GetType() != TYPE_VEC4) return false; else return vec4_value == other.vec4_value;
49 case TYPE_QUAT:
50 if (other.GetType() != TYPE_QUAT) return false; else return quat_value == other.quat_value;
51 default: return false;
52 }
53 }
54
55 Value(bool value) : bool_value(value) { type = TYPE_BOOL; }
56 Value(name_t value) : name_value(value) { type = TYPE_NAME; }
57 Value(const char* value) : string_value(value) { type = TYPE_STRING; }
58
59 Value(int8_t value) : int32_value(value) { type = TYPE_INT32; }
60 Value(int16_t value) : int32_value(value) { type = TYPE_INT32; }
61 Value(int64_t value) : int32_value(value) { type = TYPE_INT32; }
62
63 Value(uint8_t value) : uint32_value(value) { type = TYPE_UINT32; }
64 Value(uint16_t value) : uint32_value(value) { type = TYPE_UINT32; }
65 Value(uint64_t value) : uint32_value(value) { type = TYPE_UINT32; }
66
67 Value(double value) : float_value(value) { type = TYPE_FLOAT32; }
68
69 Value(int32_t value) : int32_value(value) { type = TYPE_INT32; }
70
71 Value(uint32_t value) : uint32_value(value) { type = TYPE_UINT32; }
72
73 Value(vec2 value) : vec2_value(value) { type = TYPE_VEC2; }
74 Value(vec3 value) : vec3_value(value) { type = TYPE_VEC3; }
75 Value(vec4 value) : vec4_value(value) { type = TYPE_VEC4; }
76
77 Value(quat value) : quat_value(value) { type = TYPE_QUAT; }
78
79 Value(float value) : float_value(value) { type = TYPE_FLOAT32; }
80
81 operator bool() const { AssertType(TYPE_BOOL); return bool_value; }
82
83 operator name_t() const {
84 if (type == TYPE_STRING) {
85 return (name_t)string_value;
86 }
87
89
90 return name_value;
91 }
92 operator const char*() const {
93 if (type == TYPE_NAME) {
94 return name_value;
95 }
96
98
99 return string_value;
100 }
101
102
103 operator int8_t() const { return (int32_t)*this; }
104 operator int16_t() const { return (int32_t)*this; }
105 operator int64_t() const { return (int32_t)*this; }
106
107 operator uint8_t() const { return (uint32_t)*this; }
108 operator uint16_t() const { return (uint32_t)*this; }
109 operator uint64_t() const { return (uint32_t)*this; }
110
111 operator double() const { return (float)*this; }
112
113 operator int32_t() const { AssertType(TYPE_INT32); return int32_value; }
114
115 //operator uint16_t() const { if (type != TYPE_UINT16) {__asm__ volatile("int $0x03");} /*assert(type == TYPE_UINT16);*/ return uint16_value; }
116 operator uint32_t() const { AssertType(TYPE_UINT32); return uint32_value; }
117
118 operator float() const { AssertType(TYPE_FLOAT32); return float_value; }
119
120 operator vec2() const { AssertType(TYPE_VEC2); return vec2_value; }
121 operator vec3() const { AssertType(TYPE_VEC3); return vec3_value; }
122 operator vec4() const { AssertType(TYPE_VEC4); return vec4_value; }
123
124 operator quat() const { AssertType(TYPE_QUAT); return quat_value; }
125
126
127
128 inline bool IsBool() const { return type == TYPE_BOOL; }
129 inline bool IsInt() const { return type == TYPE_INT32 || type == TYPE_UINT32; }
130 inline bool IsFloat() const { return type == TYPE_FLOAT32; }
131 inline bool IsName() const { return type == TYPE_NAME; }
132 inline bool IsString() const { return type == TYPE_STRING; }
133 inline bool IsVec2() const { return type == TYPE_VEC2; }
134 inline bool IsVec3() const { return type == TYPE_VEC3; }
135 inline bool IsVec4() const { return type == TYPE_VEC4; }
136 inline bool IsQuat() const { return type == TYPE_QUAT; }
137
138 inline int GetInt() const {
139 switch (type) {
140 case TYPE_INT32: return int32_value;
141 case TYPE_UINT32: return int32_value;
142 case TYPE_FLOAT32: return float_value;
143 default: return 0;
144 }
145 }
146
147 inline float GetFloat() const {
148 switch (type) {
149 case TYPE_INT32: return int32_value;
150 case TYPE_UINT32: return uint32_value;
151 case TYPE_FLOAT32: return float_value;
152 default: return 0;
153 }
154 }
155
156 inline std::string ToString() {
157 switch (type) {
158 case TYPE_UNDEFINED: return "undefined";
159 case TYPE_BOOL: return bool_value ? "true" : "false";
160 case TYPE_INT32: return std::to_string(int32_value);
161 case TYPE_UINT32: return std::to_string(uint32_value);
162 case TYPE_FLOAT32: return std::to_string(float_value);
163 case TYPE_NAME: return name_value;
164 case TYPE_STRING: return string_value;
165 case TYPE_VEC2: return std::to_string(vec2_value.x) + ";" + std::to_string(vec2_value.y);
166 case TYPE_VEC3: return std::to_string(vec3_value.x) + ";" + std::to_string(vec3_value.y) + ";" + std::to_string(vec3_value.z);
167 case TYPE_VEC4: return std::to_string(vec4_value.x) + ";" + std::to_string(vec4_value.y) + ";" + std::to_string(vec4_value.z) + ";" + std::to_string(vec4_value.w);
168 case TYPE_QUAT: return std::to_string(quat_value.x) + ";" + std::to_string(quat_value.y) + ";" + std::to_string(quat_value.z) + ";" + std::to_string(quat_value.w);
169 default: return "unknown";
170 }
171
172 }
173
174 inline Type GetType() const { return type; }
175protected:
177
178 void AssertType(Type type) const {
179 if (this->type != type) {
180 Log(Severity::CRITICAL_ERROR, System::CORE, "Value of type {} used in a {} context", TypeToString(this->type), TypeToString(type));
181 }
182 }
183
184 union {
187 const char* string_value;
188 int32_t int32_value;
189 uint32_t uint32_value;
195 };
196
197 friend class ValuePtr;
198};
199
200class ValuePtr {
201public:
202 ValuePtr() : uint32_ptr(nullptr) { type = TYPE_UNDEFINED; }
204
205 ValuePtr(const ValuePtr& other) : vec4_ptr(other.vec4_ptr) { type = other.type; }
206
207 ValuePtr(Value& other) {
208 type = other.GetType();
209
210 switch (type) {
211 case TYPE_BOOL: bool_ptr = &other.bool_value; break;
212 case TYPE_INT32: int32_ptr = &other.int32_value; break;
213 case TYPE_UINT32: uint32_ptr = &other.uint32_value; break;
214 case TYPE_FLOAT32: float_ptr = &other.float_value; break;
215 case TYPE_NAME: name_ptr = &other.name_value; break;
216 case TYPE_STRING: string_ptr = &other.string_value; break;
217 case TYPE_VEC2: vec2_ptr = &other.vec2_value; break;
218 case TYPE_VEC3: vec3_ptr = &other.vec3_value; break;
219 case TYPE_VEC4: vec4_ptr = &other.vec4_value; break;
220 case TYPE_QUAT: quat_ptr = &other.quat_value; break;
221 default: assert(false);
222 }
223 }
224
225 operator Value() const {
226 switch (type) {
227 case TYPE_UNDEFINED: return Value();
228 case TYPE_BOOL: return *bool_ptr;
229 case TYPE_INT32: return *int32_ptr;
230 case TYPE_UINT32: return *uint32_ptr;
231 case TYPE_FLOAT32: return *float_ptr;
232 case TYPE_NAME: return *name_ptr;
233 case TYPE_STRING: return *string_ptr;
234 case TYPE_VEC2: return *vec2_ptr;
235 case TYPE_VEC3: return *vec3_ptr;
236 case TYPE_VEC4: return *vec4_ptr;
237 case TYPE_QUAT: return *quat_ptr;
238 default: assert(false); return Value();
239 }
240 }
241
242 void SetValue(const Value& other) {
243 assert(type == other.GetType());
244
245 switch (type) {
246 case TYPE_BOOL: *bool_ptr = (bool)other; break;
247 case TYPE_INT32: *int32_ptr = (int32_t)other; break;
248 case TYPE_UINT32: *uint32_ptr = (uint32_t)other; break;
249 case TYPE_FLOAT32: *float_ptr = (float)other; break;
250 case TYPE_NAME: *name_ptr = (name_t)other; break;
251 case TYPE_STRING: *string_ptr = (const char*)other; break;
252 case TYPE_VEC2: *vec2_ptr = (vec2)other; break;
253 case TYPE_VEC3: *vec3_ptr = (vec3)other; break;
254 case TYPE_VEC4: *vec4_ptr = (vec4)other; break;
255 case TYPE_QUAT: *quat_ptr = (quat)other; break;
256 default: assert(false);
257 }
258 }
259
260 ValuePtr(bool* ptr) : bool_ptr(ptr) { type = TYPE_BOOL; }
261 ValuePtr(name_t* ptr) : name_ptr(ptr) { type = TYPE_NAME; }
262 ValuePtr(const char** ptr) : string_ptr(ptr) { type = TYPE_STRING; }
263
264 ValuePtr(int32_t* ptr) : int32_ptr(ptr) { type = TYPE_INT32; }
265
266 ValuePtr(uint32_t* ptr) : uint32_ptr(ptr) { type = TYPE_UINT32; }
267
268 ValuePtr(vec2* ptr) : vec2_ptr(ptr) { type = TYPE_VEC2; }
269 ValuePtr(vec3* ptr) : vec3_ptr(ptr) { type = TYPE_VEC3; }
270 ValuePtr(vec4* ptr) : vec4_ptr(ptr) { type = TYPE_VEC4; }
271
272 ValuePtr(quat* ptr) : quat_ptr(ptr) { type = TYPE_QUAT; }
273
274 ValuePtr(float* ptr) : float_ptr(ptr) { type = TYPE_FLOAT32; }
275
276 operator bool*() const { assert(type == TYPE_BOOL); return bool_ptr; }
277 operator name_t*() const { assert(type == TYPE_NAME); return name_ptr; }
278 operator const char**() const { assert(type == TYPE_STRING); return string_ptr; }
279
280 operator int32_t*() const { assert(type == TYPE_INT32); return int32_ptr; }
281
282 operator uint32_t*() const { assert(type == TYPE_UINT32); return uint32_ptr; }
283
284 operator float*() const { assert(type == TYPE_FLOAT32); return float_ptr; }
285
286 operator vec2*() const { assert(type == TYPE_VEC2); return vec2_ptr; }
287 operator vec3*() const { assert(type == TYPE_VEC3); return vec3_ptr; }
288 operator vec4*() const { assert(type == TYPE_VEC4); return vec4_ptr; }
289
290 operator quat*() const { assert(type == TYPE_QUAT); return quat_ptr; }
291
292 inline bool IsBool() const { return type == TYPE_BOOL; }
293 inline bool IsInt() const { return type == TYPE_INT32 || type == TYPE_UINT32; }
294 inline bool IsFloat() const { return type == TYPE_FLOAT32; }
295
296 inline Type GetType() const { return type; }
297private:
298 Type type;
299
300 union {
301 bool* bool_ptr;
303 const char** string_ptr;
304 int32_t* int32_ptr;
305 uint32_t* uint32_ptr;
306 float* float_ptr;
311 };
312};
313
315public:
316 ValueArray(const Value* first, size_t count) : first_field(first), field_count(count) {}
317 const Value& operator [](size_t n) const { assert(n < field_count); return first_field[n]; }
318 const size_t size() const { return field_count; }
319private:
320 const Value* first_field;
321 size_t field_count;
322};
323
324typedef Value value_t;
326
327}
328
329#endif // TRAM_SDK_FRAMEWORK_VALUE_H
Definition: value.h:314
const Value & operator[](size_t n) const
Definition: value.h:317
const size_t size() const
Definition: value.h:318
ValueArray(const Value *first, size_t count)
Definition: value.h:316
Generic, type-safe union.
Definition: value.h:18
bool IsQuat() const
Definition: value.h:136
int32_t int32_value
Definition: value.h:188
bool operator==(const Value &other) const
Definition: value.h:30
bool bool_value
Definition: value.h:185
Value(double value)
Definition: value.h:67
Value(uint32_t value)
Definition: value.h:71
Value(int16_t value)
Definition: value.h:60
int GetInt() const
Definition: value.h:138
Value(vec3 value)
Definition: value.h:74
void AssertType(Type type) const
Definition: value.h:178
Value & operator=(const Value &other)
Definition: value.h:25
vec4 vec4_value
Definition: value.h:193
Value(vec4 value)
Definition: value.h:75
bool IsBool() const
Definition: value.h:128
Value()
Definition: value.h:20
~Value()
Definition: value.h:21
Value(const char *value)
Definition: value.h:57
vec2 vec2_value
Definition: value.h:191
Value(int32_t value)
Definition: value.h:69
std::string ToString()
Definition: value.h:156
name_t name_value
Definition: value.h:186
quat quat_value
Definition: value.h:194
Value(quat value)
Definition: value.h:77
float GetFloat() const
Definition: value.h:147
Value(name_t value)
Definition: value.h:56
vec3 vec3_value
Definition: value.h:192
uint32_t uint32_value
Definition: value.h:189
const char * string_value
Definition: value.h:187
bool IsVec4() const
Definition: value.h:135
Value(int64_t value)
Definition: value.h:61
Value(bool value)
Definition: value.h:55
Value(uint8_t value)
Definition: value.h:63
Value(const Value &other)
Definition: value.h:23
bool IsName() const
Definition: value.h:131
bool IsInt() const
Definition: value.h:129
Value(uint16_t value)
Definition: value.h:64
bool IsVec3() const
Definition: value.h:134
Value(int8_t value)
Definition: value.h:59
float float_value
Definition: value.h:190
Value(float value)
Definition: value.h:79
bool IsString() const
Definition: value.h:132
Value(vec2 value)
Definition: value.h:73
bool IsFloat() const
Definition: value.h:130
Type GetType() const
Definition: value.h:174
bool IsVec2() const
Definition: value.h:133
Type type
Definition: value.h:176
Value(uint64_t value)
Definition: value.h:65
Definition: value.h:200
uint32_t * uint32_ptr
Definition: value.h:305
ValuePtr(vec3 *ptr)
Definition: value.h:269
ValuePtr(bool *ptr)
Definition: value.h:260
bool IsInt() const
Definition: value.h:293
int32_t * int32_ptr
Definition: value.h:304
bool IsFloat() const
Definition: value.h:294
ValuePtr(vec2 *ptr)
Definition: value.h:268
ValuePtr(quat *ptr)
Definition: value.h:272
void SetValue(const Value &other)
Definition: value.h:242
bool * bool_ptr
Definition: value.h:301
ValuePtr()
Definition: value.h:202
quat * quat_ptr
Definition: value.h:310
ValuePtr(float *ptr)
Definition: value.h:274
ValuePtr(vec4 *ptr)
Definition: value.h:270
name_t * name_ptr
Definition: value.h:302
vec4 * vec4_ptr
Definition: value.h:309
Type GetType() const
Definition: value.h:296
const char ** string_ptr
Definition: value.h:303
vec2 * vec2_ptr
Definition: value.h:307
ValuePtr(const ValuePtr &other)
Definition: value.h:205
~ValuePtr()
Definition: value.h:203
ValuePtr(Value &other)
Definition: value.h:207
vec3 * vec3_ptr
Definition: value.h:308
bool IsBool() const
Definition: value.h:292
ValuePtr(int32_t *ptr)
Definition: value.h:264
ValuePtr(uint32_t *ptr)
Definition: value.h:266
ValuePtr(const char **ptr)
Definition: value.h:262
ValuePtr(name_t *ptr)
Definition: value.h:261
float * float_ptr
Definition: value.h:306
@ CORE
Definition: system.h:14
Serialization, i.e.
glm::vec4 vec4
Definition: math.h:15
Type
Type enumeration for serialization, etc.
Definition: type.h:12
@ TYPE_BOOL
Definition: type.h:14
@ TYPE_STRING
Definition: type.h:16
@ TYPE_VEC2
Definition: type.h:24
@ TYPE_UINT32
Definition: type.h:22
@ TYPE_INT32
Definition: type.h:19
@ TYPE_VEC3
Definition: type.h:25
@ TYPE_QUAT
Definition: type.h:27
@ TYPE_VEC4
Definition: type.h:26
@ TYPE_UNDEFINED
Definition: type.h:13
@ TYPE_NAME
Definition: type.h:15
@ TYPE_FLOAT32
Definition: type.h:23
glm::vec2 vec2
Definition: math.h:16
glm::vec3 vec3
Definition: math.h:11
glm::quat quat
Definition: math.h:12
void Log(Severity severity, System::system_t system, const std::string_view &format, Args &&... args)
Definition: logging.h:79
Value value_t
Definition: event.h:21
const char * TypeToString(Type type)
Converts type enum to string.
Definition: type.cpp:8
ValueArray valuearray_t
Definition: value.h:325
UID name_t
Definition: uid.h:43
Interned string type.
Definition: uid.h:10