forked from RPCS3/rpcs3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.h
519 lines (407 loc) · 10 KB
/
Config.h
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#pragma once
#include "util/types.hpp"
#include "Utilities/StrUtil.h"
#include "util/logs.hpp"
#include "util/atomic.hpp"
#include "util/shared_ptr.hpp"
#include <utility>
#include <string>
#include <vector>
#include <set>
#include <map>
namespace cfg
{
// Format min and max values
std::vector<std::string> make_int_range(s64 min, s64 max);
// Format min and max unsigned values
std::vector<std::string> make_uint_range(u64 min, u64 max);
// Internal hack
bool try_to_enum_value(u64* out, decltype(&fmt_class_string<int>::format) func, const std::string&);
// Internal hack
std::vector<std::string> try_to_enum_list(decltype(&fmt_class_string<int>::format) func);
// Config tree entry type.
enum class type : unsigned
{
node = 0, // cfg::node type
_bool, // cfg::_bool type
_enum, // cfg::_enum type
_int, // cfg::_int type
uint, // cfg::uint type
string, // cfg::string type
set, // cfg::set_entry type
map, // cfg::map_entry type
log,
};
// Config tree entry abstract base class
class _base
{
const type m_type{};
protected:
bool m_dynamic = true;
const std::string m_name{};
// Ownerless entry constructor
_base(type _type);
// Owned entry constructor
_base(type _type, class node* owner, const std::string& name, bool dynamic);
public:
_base(const _base&) = delete;
_base& operator=(const _base&) = delete;
virtual ~_base() = default;
// Get type
type get_type() const { return m_type; }
const std::string& get_name() const { return m_name; }
// Get dynamic property for reloading configs during games
bool get_is_dynamic() const { return m_dynamic; }
// Reset defaults
virtual void from_default() = 0;
// Convert to string (optional)
virtual std::string to_string() const
{
return{};
}
// Try to convert from string (optional)
virtual bool from_string(const std::string&, bool /*dynamic*/ = false);
// Get string list (optional)
virtual std::vector<std::string> to_list() const
{
return{};
}
// Set multiple values. Implementation-specific, optional.
virtual bool from_list(std::vector<std::string>&&);
};
// Config tree node which contains another nodes
class node : public _base
{
std::vector<_base*> m_nodes{};
friend class _base;
public:
// Root node constructor
node()
: _base(type::node)
{
}
// Registered node constructor
node(node* owner, const std::string& name, bool dynamic = true)
: _base(type::node, owner, name, dynamic)
{
}
// Get child nodes
const auto& get_nodes() const
{
return m_nodes;
}
// Serialize node
std::string to_string() const override;
// Deserialize node
bool from_string(const std::string& value, bool dynamic = false) override;
// Set default values
void from_default() override;
};
class _bool final : public _base
{
atomic_t<bool> m_value;
public:
bool def;
_bool(node* owner, const std::string& name, bool def = false, bool dynamic = false)
: _base(type::_bool, owner, name, dynamic)
, m_value(def)
, def(def)
{
}
explicit operator bool() const
{
return m_value;
}
bool get() const
{
return m_value;
}
void from_default() override;
std::string to_string() const override
{
return m_value ? "true" : "false";
}
bool from_string(const std::string& value, bool /*dynamic*/ = false) override
{
if (value == "false")
m_value = false;
else if (value == "true")
m_value = true;
else
return false;
return true;
}
void set(const bool& value)
{
m_value = value;
}
};
// Value node with fixed set of possible values, each maps to an enum value of type T.
template <typename T>
class _enum final : public _base
{
atomic_t<T> m_value;
public:
const T def;
_enum(node* owner, const std::string& name, T value = {}, bool dynamic = false)
: _base(type::_enum, owner, name, dynamic)
, m_value(value)
, def(value)
{
}
operator T() const
{
return m_value;
}
T get() const
{
return m_value;
}
void set(T value)
{
m_value = value;
}
void from_default() override
{
m_value = def;
}
std::string to_string() const override
{
std::string result;
fmt_class_string<T>::format(result, fmt_unveil<T>::get(m_value.load()));
return result; // TODO: ???
}
bool from_string(const std::string& value, bool /*dynamic*/ = false) override
{
u64 result;
if (try_to_enum_value(&result, &fmt_class_string<T>::format, value))
{
// No narrowing check, it's hard to do right there
m_value = static_cast<T>(static_cast<std::underlying_type_t<T>>(result));
return true;
}
return false;
}
std::vector<std::string> to_list() const override
{
return try_to_enum_list(&fmt_class_string<T>::format);
}
};
// Signed 32/64-bit integer entry with custom Min/Max range.
template <s64 Min, s64 Max>
class _int final : public _base
{
static_assert(Min < Max, "Invalid cfg::_int range");
// Prefer 32 bit type if possible
using int_type = std::conditional_t<Min >= s32{smin} && Max <= s32{smax}, s32, s64>;
atomic_t<int_type> m_value;
public:
int_type def;
// Expose range
static const s64 max = Max;
static const s64 min = Min;
_int(node* owner, const std::string& name, int_type def = std::min<int_type>(Max, std::max<int_type>(Min, 0)), bool dynamic = false)
: _base(type::_int, owner, name, dynamic)
, m_value(def)
, def(def)
{
}
operator int_type() const
{
return m_value;
}
int_type get() const
{
return m_value;
}
void from_default() override
{
m_value = def;
}
std::string to_string() const override
{
return std::to_string(m_value);
}
bool from_string(const std::string& value, bool /*dynamic*/ = false) override
{
s64 result;
if (try_to_int64(&result, value, Min, Max))
{
m_value = static_cast<int_type>(result);
return true;
}
return false;
}
void set(const s64& value)
{
m_value = static_cast<int_type>(value);
}
std::vector<std::string> to_list() const override
{
return make_int_range(Min, Max);
}
};
// Alias for 32 bit int
using int32 = _int<s32{smin}, s32{smax}>;
// Alias for 64 bit int
using int64 = _int<s64{smin}, s64{smax}>;
// Unsigned 32/64-bit integer entry with custom Min/Max range.
template <u64 Min, u64 Max>
class uint final : public _base
{
static_assert(Min < Max, "Invalid cfg::uint range");
// Prefer 32 bit type if possible
using int_type = std::conditional_t<Max <= u32{umax}, u32, u64>;
atomic_t<int_type> m_value;
public:
int_type def;
// Expose range
static const u64 max = Max;
static const u64 min = Min;
uint(node* owner, const std::string& name, int_type def = std::max<int_type>(Min, 0), bool dynamic = false)
: _base(type::uint, owner, name, dynamic)
, m_value(def)
, def(def)
{
}
operator int_type() const
{
return m_value;
}
int_type get() const
{
return m_value;
}
void from_default() override
{
m_value = def;
}
std::string to_string() const override
{
return std::to_string(m_value);
}
bool from_string(const std::string& value, bool /*dynamic*/ = false) override
{
u64 result;
if (try_to_uint64(&result, value, Min, Max))
{
m_value = static_cast<int_type>(result);
return true;
}
return false;
}
void set(const u64& value)
{
m_value = static_cast<int_type>(value);
}
std::vector<std::string> to_list() const override
{
return make_uint_range(Min, Max);
}
};
// Alias for 32 bit uint
using uint32 = uint<0, u32{umax}>;
// Alias for 64 bit int
using uint64 = uint<0, u64{umax}>;
// Simple string entry with mutex
class string : public _base
{
atomic_ptr<std::string> m_value;
public:
std::string def;
string(node* owner, std::string name, std::string def = {}, bool dynamic = false)
: _base(type::string, owner, name, dynamic)
, m_value(def)
, def(std::move(def))
{
}
operator std::string() const
{
return *m_value.load().get();
}
std::pair<const std::string&, shared_ptr<std::string>> get() const
{
auto v = m_value.load();
if (auto s = v.get())
{
return {*s, std::move(v)};
}
else
{
static const std::string _empty;
return {_empty, {}};
}
}
void from_default() override;
std::string to_string() const override
{
return *m_value.load().get();
}
bool from_string(const std::string& value, bool /*dynamic*/ = false) override
{
m_value = value;
return true;
}
};
// Simple set entry (TODO: template for various types)
class set_entry final : public _base
{
std::set<std::string> m_set{};
public:
// Default value is empty list in current implementation
set_entry(node* owner, const std::string& name)
: _base(type::set, owner, name, false)
{
}
const std::set<std::string>& get_set() const
{
return m_set;
}
void set_set(std::set<std::string>&& set)
{
m_set = std::move(set);
}
void from_default() override;
std::vector<std::string> to_list() const override
{
return{ m_set.begin(), m_set.end() };
}
bool from_list(std::vector<std::string>&& list) override
{
m_set = { std::make_move_iterator(list.begin()), std::make_move_iterator(list.end()) };
return true;
}
};
class map_entry final : public _base
{
std::map<std::string, std::string> m_map{};
public:
map_entry(node* owner, const std::string& name)
: _base(type::map, owner, name, true)
{
}
const std::map<std::string, std::string>& get_map() const
{
return m_map;
}
std::string get_value(const std::string& key);
void set_value(const std::string& key, const std::string& value);
void set_map(std::map<std::string, std::string>&& map);
void from_default() override;
};
class log_entry final : public _base
{
std::map<std::string, logs::level> m_map{};
public:
log_entry(node* owner, const std::string& name)
: _base(type::log, owner, name, true)
{
}
const std::map<std::string, logs::level>& get_map() const
{
return m_map;
}
void set_map(std::map<std::string, logs::level>&& map);
void from_default() override;
};
}