1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
#ifndef _JSON_H
#define _JSON_H
#define JSON_NO_IO
#include <nlohmann/json.hpp>
#include "gl.h"
using json = nlohmann::json;
#define json_str_vec4(s, v) sscanf(s, "%f %f %f %f", &(v)[0], &(v)[1], &(v)[2], &(v)[3])
#define json_str_vec3(s, v) sscanf(s, "%f %f %f", &(v)[0], &(v)[1], &(v)[2])
#define json_str_vec2(s, v) sscanf(s, "%f %f", &(v)[0], &(v)[1])
#define parse_fail(obj) do { \
delete obj; \
return nullptr; \
} while (0)
namespace Mauri {
enum JsonValueType
{
INT = 0,
FLOAT,
VEC2,
VEC3,
VEC4,
BOOL,
STRING
};
struct JsonValue
{
void *v;
JsonValueType type;
std::string name;
};
class Parser
{
public:
Parser() = default;
~Parser() = default;
template<typename T>
static auto parse_value(const json &root, const std::string &name, void *out, const T &default_value) -> void
{
*((T *)out) = default_value;
const json &value = (!name.empty() && root.contains(name)) ? root[name] : root;
if constexpr (std::is_same<T, u32>::value)
{
if (value.is_number()) *((u32 *)out) = value.get<u32>();
}
else if constexpr (std::is_same<T, s32>::value)
{
if (value.is_number()) *((s32 *)out) = value.get<s32>();
}
else if constexpr (std::is_same<T, f32>::value)
{
if (value.is_number()) *((f32 *)out) = value.get<f32>();
}
else if constexpr (std::is_same<T, std::string>::value)
{
if (value.is_string()) *((std::string *)out) = value.get<std::string>();
}
else if constexpr (std::is_same<T, vec2>::value)
{
if (value.is_string()) json_str_vec2(value.get<std::string>().c_str(), *((vec2 *)out));
}
else if constexpr (std::is_same<T, vec3>::value)
{
if (value.is_string()) json_str_vec3(value.get<std::string>().c_str(), *((vec3 *)out));
}
else if constexpr (std::is_same<T, vec4>::value)
{
// Some uniforms are saved as a single number.
if (value.is_number()) (*((vec4 *)out))[0] = value.get<f32>();
else if (value.is_string()) json_str_vec4(value.get<std::string>().c_str(), *((vec4 *)out));
}
else if constexpr (std::is_same<T, bool>::value)
{
if (value.is_boolean()) *((bool *)out) = value.get<bool>();
}
}
template<typename T>
auto map_value(const json &root, const std::string &name, void *out, const T &default_value) -> void
{
const json &value = (!name.empty() && root.contains(name)) ? root[name] : root;
std::string user = "root";
if (value.is_object())
{
if (value.contains("user"))
{
const json &user_value = value["user"];
if (user_value.is_string()) user = user_value;
}
this->parse_value<T>(value, "value", out, default_value);
}
else
{
this->parse_value<T>(root, name, out, default_value);
}
JsonValueType type;
if constexpr (std::is_same<T, u32>::value) type = INT;
else if constexpr (std::is_same<T, s32>::value) type = INT;
else if constexpr (std::is_same<T, f32>::value) type = FLOAT;
else if constexpr (std::is_same<T, std::string>::value) type = STRING;
else if constexpr (std::is_same<T, vec2>::value) type = VEC2;
else if constexpr (std::is_same<T, vec3>::value) type = VEC3;
else if constexpr (std::is_same<T, vec4>::value) type = VEC4;
else if constexpr (std::is_same<T, bool>::value) type = BOOL;
this->values[user].emplace_back(JsonValue{out, type, name});
}
std::unordered_map<std::string, std::vector<JsonValue>> values;
};
} // namespace Mauri
#endif // _JSON_H
|