-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththing_data.hpp
More file actions
276 lines (241 loc) · 11 KB
/
thing_data.hpp
File metadata and controls
276 lines (241 loc) · 11 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#ifndef THING_DATA_H
#define THING_DATA_H
#include <Nostalgia/things/thinkers/thinker.hpp>
#include <Nostalgia/things/resources/resource.hpp>
#define ASSERT_THING_VARIABLE(ThingVarName, InVarNames, ReturnOnFail...) \
Farg<ThingVariable> ThingVarName{_get_variable({inNames...})}; \
if(!ThingVarName) { return ReturnOnFail; }
namespace TheatreFile
{
enum class ThingVarType
{ String, Bool, Number, BitMask, Enum, Child, Parent, ID, None };
struct ThingVariable
{
enum NumberType : int
{ NIL=0, FLOAT, INT, VEC2, VEC3, VEC4 };
// The name of the variable, or, if this is a Child/Parent variable, the name of the
// Child/Parent Thing.
std::string name{};
// The variable's value, represented as a string, or, if this is a Child/Parent
// variable, the type-name of the Child/Parent Thing.
std::string value{};
// The variable's type.
ThingVarType type{ThingVarType::None};
// If the variable is a reference, this is the type of the Thing it is referencing.
PID thing_type{ThingType::Invalid};
// If the variable is a number, this is the type of number it is.
NumberType number_type{NIL};
// Whether this variable should be ignored by the editor or not
bool editor_ignored{false};
void clear()
{ *this = ThingVariable{}; }
bool invalid() const noexcept
{ return type == ThingVarType::None or name.empty(); }
bool operator!() const noexcept
{ return invalid(); }
std::string get_log() const noexcept;
std::string get_parsable_string() const noexcept;
};
using ThingVarArray = std::vector<ThingVariable>;
struct ThingData
{
private:
Farg<ThingVariable> _get_variable(std::initializer_list<std::string>) const;
Error _get_id_variable(ID&, Farg<ThingVariable>, ThingVarType) const;
Shared<Resource> _try_get_resource(Sarg inName) const;
Shared<Thinker> _try_get_thinker(Sarg inName) const;
public:
PID type{};
std::string name{};
ThingVarArray variables{};
ThingVariable parent_variable{};
ThingVarArray children_variables{};
std::string get_log() const noexcept;
std::string get_parsable_string() const noexcept;
bool invalid() const;
void clear();
ID get_parent() const;
IdSet_t get_children() const;
Error remove_variable(Sarg inName);
Error remove_child(Sarg inName);
void set_variable(Sarg inValue, Sarg inName, bool inIsIgnoredByEditor = false);
Error set_variable(ID inValue, Sarg inName, bool inIsIgnoredByEditor = false);
void set_variable(bool inValue, Sarg inName, bool inIsIgnoredByEditor = false);
Error set_variable(Shared<FileData> inValue, Sarg inName, bool inIsIgnoredByEditor = false);
void set_variable(BitMask inValue, Sarg inName, bool inIsIgnoredByEditor = false);
Error set_enum_variable(Sarg inEnumName, Sarg inName, bool inIsIgnoredByEditor = false);
template<NumberOrGLM T>
void set_variable(Farg<T> inValue, Sarg inName, bool inIsIgnoredByEditor = false)
{
ThingVariable::NumberType _number_type{ThingVariable::NIL};
if constexpr(std::same_as<T, float> or std::same_as<T, double>)
{ _number_type = ThingVariable::FLOAT; }
if constexpr(std::same_as<T, long> or std::same_as<T, int>)
{ _number_type = ThingVariable::INT; }
if constexpr(std::same_as<T, glm::vec2>)
{ _number_type = ThingVariable::VEC2; }
if constexpr(std::same_as<T, glm::vec3>)
{ _number_type = ThingVariable::VEC3; }
if constexpr(std::same_as<T, glm::vec4> or std::same_as<T, glm::quat>)
{ _number_type = ThingVariable::VEC4; }
FAUTO __find_name{inName};
auto found_it{std::find_if(variables.begin(), variables.end(),
[__find_name](Farg<ThingVariable> var_it)
{ return not var_it.name.compare(__find_name); })};
if(found_it != variables.end())
{
found_it->value = NumToString(inValue);
found_it->number_type = _number_type;
found_it->editor_ignored = inIsIgnoredByEditor;
return;
}
variables.emplace_back(inName,
NumToString(inValue),
ThingVarType::Number,
ThingType::Invalid,
_number_type,
inIsIgnoredByEditor);
}
template<IsEnum T>
Error set_variable(T inValue, Sarg inName, bool inIsIgnoredByEditor = false)
{
if(auto enum_name{EnumRegistry::GetEnumName(inValue)}; not enum_name.empty())
{ return set_enum_variable(enum_name, inName, inIsIgnoredByEditor); }
return ERR_INVALID;
}
template<Resource_t T>
Error set_variable(Farg<Shared<T>> inValue, Sarg inName, bool inIsIgnoredByEditor = false)
{
if(not inValue)
{ return ERR_NULLPTR; }
return set_variable(inValue->uid(), inName, inIsIgnoredByEditor);
}
template<Thinker_t T>
Error set_variable(Farg<Shared<T>> inValue, Sarg inName, bool inIsIgnoredByEditor = false)
{
if(not inValue)
{ return ERR_NULLPTR; }
return set_variable(inValue->uid(), inName, inIsIgnoredByEditor);
}
template<Resource_t T, StringType... Names>
Error get_variable(Shared<T>& outValue, Names... inNames) const
{
ASSERT_THING_VARIABLE(thing_var, inNames, ERR_NOT_FOUND)
if(thing_var.value.empty())
{ return ERR_EMPTY; }
else if(thing_var.type != ThingVarType::ID)
{ return ERR_MISMATCHED_TYPES; }
if(auto resource{DCast<T>(_try_get_resource(thing_var.value))})
{ outValue = resource; return OK; }
return ERR_NOT_FOUND;
}
template<Thinker_t T, StringType... Names>
Error get_variable(Shared<T>& outValue, Names... inNames) const
{
ASSERT_THING_VARIABLE(thing_var, inNames, ERR_NOT_FOUND)
if(thing_var.value.empty())
{ return ERR_EMPTY; }
else if(thing_var.type != ThingVarType::ID)
{ return ERR_MISMATCHED_TYPES; }
if(auto thinker{DCast<T>(_try_get_thinker(thing_var.value))})
{ outValue = thinker; return OK; }
return ERR_NOT_FOUND;
}
template<StringType... Names>
Error get_variable(Shared<FileData>& outValue, Names... inNames) const
{
ASSERT_THING_VARIABLE(thing_var, inNames, ERR_NOT_FOUND)
if(thing_var.value.empty())
{ return ERR_EMPTY; }
else if(thing_var.type == ThingVarType::String)
{
auto output{MakeShared<FileData>()};
Error status{print_error_enum(output->ReadFile(thing_var.value))};
if(status == OK)
{ outValue = output; }
return status;
}
return ERR_MISMATCHED_TYPES;
}
template<StringContainer T, StringType... Names>
Error get_variable(T& outValue, Names... inNames) const
{
ASSERT_THING_VARIABLE(thing_var, inNames, ERR_NOT_FOUND)
if(thing_var.type != ThingVarType::String)
{ return ERR_MISMATCHED_TYPES; }
outValue = thing_var.value;
return OK;
}
template<StringType... Names>
Error get_variable(ID& outValue, Names... inNames) const
{
ASSERT_THING_VARIABLE(thing_var, inNames, ERR_NOT_FOUND)
return _get_id_variable(outValue, thing_var, ThingVarType::ID);
}
template<IsEnum T, StringType... Names>
Error get_variable(T& outValue, Names... inNames) const
{
ASSERT_THING_VARIABLE(thing_var, inNames, ERR_NOT_FOUND)
if(thing_var.type != ThingVarType::Enum)
{ return ERR_MISMATCHED_TYPES; }
else if(not EnumRegistry::GetEnum(thing_var.value, outValue))
{ return ERR_INVALID; }
return OK;
}
template<StringType... Names>
Error get_variable(bool& outValue, Names... inNames) const
{
ASSERT_THING_VARIABLE(thing_var, inNames, ERR_NOT_FOUND)
if(thing_var.type != ThingVarType::Bool)
{ return ERR_MISMATCHED_TYPES; }
else if(!thing_var.value.compare("false"))
{ outValue = false; }
else if(!thing_var.value.compare("true"))
{ outValue = true; }
else
{ return ERR_INVALID; }
return OK;
}
template<NumberOrGLM T, StringType... Names>
Error get_variable(T& outValue, Names... inNames) const
{
ASSERT_THING_VARIABLE(thing_var, inNames, ERR_NOT_FOUND)
if(thing_var.type != ThingVarType::Number)
{ return ERR_MISMATCHED_TYPES; }
else if(!StringToNum(outValue, thing_var.value))
{ return ERR_INVALID; }
return OK;
}
template<StringType... Names>
Error get_variable(BitMask& outValue, Names... inNames) const
{
ASSERT_THING_VARIABLE(thing_var, inNames, ERR_NOT_FOUND)
if(thing_var.type != ThingVarType::BitMask)
{ return ERR_MISMATCHED_TYPES; }
for(uint i{0}; i < BitMask::max and i < thing_var.value.size(); ++i)
{ outValue.set_index(i, thing_var.value[i] == '1'); }
return OK;
}
template<StringType... Names>
int erase_variables(Names... inNames)
{
int erase_value{0};
for(FAUTO name : {inNames...})
{
for(auto it{variables.begin()}; it != variables.end();)
{
if(!it->name.compare(name))
{
it = variables.erase(it);
++erase_value;
continue;
}
++it;
}
}
return erase_value;
}
};
}
#undef ASSERT_THING_VARIABLE
#endif // THING_DATA_H