summaryrefslogtreecommitdiff
path: root/ddl/lib/RT.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'ddl/lib/RT.hpp')
-rw-r--r--ddl/lib/RT.hpp188
1 files changed, 188 insertions, 0 deletions
diff --git a/ddl/lib/RT.hpp b/ddl/lib/RT.hpp
new file mode 100644
index 0000000..2e1e652
--- /dev/null
+++ b/ddl/lib/RT.hpp
@@ -0,0 +1,188 @@
1#ifndef HEADER_RT_H
2#define HEADER_RT_H
3
4#include <vector>
5#include <map>
6#include <string>
7
8#include "json.hpp"
9
10using json = nlohmann::json;
11
12typedef int Int;
13typedef int Int32;
14typedef unsigned int Word;
15typedef unsigned int Word32;
16typedef float Float;
17typedef bool Bool;
18typedef std::string String;
19
20template<typename T1>
21struct Maybe
22{
23 T1 data;
24 bool valid;
25};
26
27template<typename T>
28struct V2 { T x,y; };
29
30template<typename T>
31struct V3 { T x,y,z; };
32
33template<typename T>
34struct V4 { T x,y,z,w; };
35
36typedef struct V2<Int> V2I;
37typedef struct V2<Word> V2U;
38typedef struct V2<Float> V2F;
39typedef struct V2<Bool> V2B;
40
41typedef struct V3<Int> V3I;
42typedef struct V3<Word> V3U;
43typedef struct V3<Float> V3F;
44typedef struct V3<Bool> V3B;
45
46typedef struct V4<Int> V4I;
47typedef struct V4<Word> V4U;
48typedef struct V4<Float> V4F;
49typedef struct V4<Bool> V4B;
50
51typedef struct V2<V2F> M22F;
52typedef struct V2<V3F> M32F;
53typedef struct V2<V4F> M42F;
54typedef struct V3<V2F> M23F;
55typedef struct V3<V3F> M33F;
56typedef struct V3<V4F> M43F;
57typedef struct V4<V2F> M24F;
58typedef struct V4<V3F> M34F;
59typedef struct V4<V4F> M44F;
60
61
62template<typename T>
63json toJSON(T &v);
64
65template<typename any>
66json toJSON(Maybe<any> &v) {
67 if (v.valid) {
68 return toJSON(v.data);
69 }
70 return json();
71}
72
73template<typename any>
74json toJSON(V2<any> &v) {
75 json obj({});
76 obj["x"] = toJSON(v.x);
77 obj["y"] = toJSON(v.y);
78 return obj;
79}
80
81template<typename any>
82json toJSON(V3<any> &v) {
83 json obj({});
84 obj["x"] = toJSON(v.x);
85 obj["y"] = toJSON(v.y);
86 obj["z"] = toJSON(v.z);
87 return obj;
88}
89
90template<typename any>
91json toJSON(V4<any> &v) {
92 json obj({});
93 obj["x"] = toJSON(v.x);
94 obj["y"] = toJSON(v.y);
95 obj["z"] = toJSON(v.z);
96 obj["w"] = toJSON(v.w);
97 return obj;
98}
99
100template<typename any>
101json toJSON(std::vector<any> &v) {
102 json obj = json::array();
103 for (any i : v) {
104 obj.push_back(toJSON(i));
105 }
106 return obj;
107}
108
109template<typename v>
110json toJSON(std::map<String,v> &value) {
111 json obj({});
112 for(auto i : value) {
113 obj[i.first] = toJSON(i.second);
114 }
115 return obj;
116}
117
118template<typename T>
119struct W {};
120
121template<typename T>
122T fromJSON(W<T> w, json &obj);
123
124template<typename any>
125Maybe<any> fromJSON(W<Maybe<any>> v, json &obj) {
126 Maybe<any> a;
127 if (obj.is_null()) {
128 a.valid = false;
129 } else {
130 a.valid = true;
131 a.data = fromJSON(W<any>(),obj);
132 }
133 return a;
134}
135
136template<typename any>
137V2<any> fromJSON(W<V2<any>> v, json &obj) {
138 V2<any> a;
139 a.x = fromJSON(W<any>(), obj["x"]);
140 a.y = fromJSON(W<any>(), obj["y"]);
141 return a;
142}
143
144template<typename any>
145V3<any> fromJSON(W<V3<any>> v, json &obj) {
146 V3<any> a;
147 a.x = fromJSON(W<any>(), obj["x"]);
148 a.y = fromJSON(W<any>(), obj["y"]);
149 a.z = fromJSON(W<any>(), obj["z"]);
150 return a;
151}
152
153template<typename any>
154V4<any> fromJSON(W<V4<any>> v, json &obj) {
155 V4<any> a;
156 a.x = fromJSON(W<any>(), obj["x"]);
157 a.y = fromJSON(W<any>(), obj["y"]);
158 a.z = fromJSON(W<any>(), obj["z"]);
159 a.w = fromJSON(W<any>(), obj["w"]);
160 return a;
161}
162
163template<typename any>
164std::vector<any> fromJSON(W<std::vector<any>> v, json &obj) {
165 std::vector<any> a;
166 for (json::iterator it = obj.begin(); it != obj.end(); ++it) {
167 a.push_back(fromJSON(W<any>(),*it));
168 }
169 return a;
170}
171
172template<typename v>
173std::map<String,v> fromJSON(W<std::map<String,v>> value, json &obj) {
174 std::map<String,v> a;
175 for (json::iterator it = obj.begin(); it != obj.end(); ++it) {
176 a[it.key()] = fromJSON(W<v>(),it.value());
177 }
178 return a;
179}
180
181/*
182template<typename k, typename v>
183std::map<k,v> fromJSON(W<std::map<k,v>> value, json &obj) {
184 std::map<k,v> a;
185 return a;
186}
187*/
188#endif \ No newline at end of file