diff options
Diffstat (limited to 'ddl/lib/RT.hpp')
-rw-r--r-- | ddl/lib/RT.hpp | 188 |
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 | |||
10 | using json = nlohmann::json; | ||
11 | |||
12 | typedef int Int; | ||
13 | typedef int Int32; | ||
14 | typedef unsigned int Word; | ||
15 | typedef unsigned int Word32; | ||
16 | typedef float Float; | ||
17 | typedef bool Bool; | ||
18 | typedef std::string String; | ||
19 | |||
20 | template<typename T1> | ||
21 | struct Maybe | ||
22 | { | ||
23 | T1 data; | ||
24 | bool valid; | ||
25 | }; | ||
26 | |||
27 | template<typename T> | ||
28 | struct V2 { T x,y; }; | ||
29 | |||
30 | template<typename T> | ||
31 | struct V3 { T x,y,z; }; | ||
32 | |||
33 | template<typename T> | ||
34 | struct V4 { T x,y,z,w; }; | ||
35 | |||
36 | typedef struct V2<Int> V2I; | ||
37 | typedef struct V2<Word> V2U; | ||
38 | typedef struct V2<Float> V2F; | ||
39 | typedef struct V2<Bool> V2B; | ||
40 | |||
41 | typedef struct V3<Int> V3I; | ||
42 | typedef struct V3<Word> V3U; | ||
43 | typedef struct V3<Float> V3F; | ||
44 | typedef struct V3<Bool> V3B; | ||
45 | |||
46 | typedef struct V4<Int> V4I; | ||
47 | typedef struct V4<Word> V4U; | ||
48 | typedef struct V4<Float> V4F; | ||
49 | typedef struct V4<Bool> V4B; | ||
50 | |||
51 | typedef struct V2<V2F> M22F; | ||
52 | typedef struct V2<V3F> M32F; | ||
53 | typedef struct V2<V4F> M42F; | ||
54 | typedef struct V3<V2F> M23F; | ||
55 | typedef struct V3<V3F> M33F; | ||
56 | typedef struct V3<V4F> M43F; | ||
57 | typedef struct V4<V2F> M24F; | ||
58 | typedef struct V4<V3F> M34F; | ||
59 | typedef struct V4<V4F> M44F; | ||
60 | |||
61 | |||
62 | template<typename T> | ||
63 | json toJSON(T &v); | ||
64 | |||
65 | template<typename any> | ||
66 | json toJSON(Maybe<any> &v) { | ||
67 | if (v.valid) { | ||
68 | return toJSON(v.data); | ||
69 | } | ||
70 | return json(); | ||
71 | } | ||
72 | |||
73 | template<typename any> | ||
74 | json toJSON(V2<any> &v) { | ||
75 | json obj({}); | ||
76 | obj["x"] = toJSON(v.x); | ||
77 | obj["y"] = toJSON(v.y); | ||
78 | return obj; | ||
79 | } | ||
80 | |||
81 | template<typename any> | ||
82 | json 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 | |||
90 | template<typename any> | ||
91 | json 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 | |||
100 | template<typename any> | ||
101 | json 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 | |||
109 | template<typename v> | ||
110 | json 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 | |||
118 | template<typename T> | ||
119 | struct W {}; | ||
120 | |||
121 | template<typename T> | ||
122 | T fromJSON(W<T> w, json &obj); | ||
123 | |||
124 | template<typename any> | ||
125 | Maybe<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 | |||
136 | template<typename any> | ||
137 | V2<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 | |||
144 | template<typename any> | ||
145 | V3<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 | |||
153 | template<typename any> | ||
154 | V4<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 | |||
163 | template<typename any> | ||
164 | std::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 | |||
172 | template<typename v> | ||
173 | std::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 | /* | ||
182 | template<typename k, typename v> | ||
183 | std::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 | ||