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