-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtableplusplus.h
476 lines (390 loc) · 12 KB
/
tableplusplus.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
/*----------------------------------------------------------------------------------------
table++
by Josh Klint
https://www.ultraengine.com
LICENSE
This code may be used freely for any purpose.
If you use this code you must include this unmodified README file somewhere in your application directory or subdirectory where it is accessible to the end user.
This unmodified readme file must be included in any source code distribution, or distribution of code derived from this code.
You may not misrepresent the origin of this code.
You may not use this code in AI training models.
----------------------------------------------------------------------------------------*/
#pragma once
#ifndef __TABLE_PLUS_PLUS
#define __TABLE_PLUS_PLUS
// Options
#define TABLEPLUSPLUS_INCLUDE_SOL 1
#define TABLEPLUSPLUS_INCLUDE_JSON 1
#define TABLEPLUSPLUS_LUATABLEFUNCTION 0
#include <string>
#include <map>
#include <sstream>
#include <stdexcept>
#if TABLEPLUSPLUS_INCLUDE_SOL
#define SOL_NO_CHECK_NUMBER_PRECISION 1
#define SOL_ALL_SAFETIES_ON 1
#include <sol/sol.hpp>
#endif
#if TABLEPLUSPLUS_INCLUDE_JSON
//#include <json.hpp>
#include "../nlohmann_json/single_include/nlohmann/json.hpp"
#endif
namespace tableplusplus
{
class table;
class tablekey;
#ifdef SOL_VERSION
struct IDKWTFLOL;
struct tablewrapper;
struct tablekeywrapper;
extern void bind_table_plus_plus(sol::state* L);
#endif
class tablekey
{
public:
enum type
{
index,
name
};
private:
friend table;
type t;
std::string s;
int i;
tablekey() {};
public:
operator std::string() const
{
if (t == type::name) return s;
std::stringstream out;
out << i;
return out.str();
}
operator int() const
{
if (t == type::name) return 0;
return i;
}
bool operator<(const tablekey k) const
{
if (t == type::index && k.t == type::name) return true;
if (t == type::name && k.t == type::index) return false;
if (t == type::index) return i < k.i;
return s < k.s;
}
bool operator==(const tablekey k) const
{
if (t != k.t) return false;
if (t == type::index) return i == k.i;
return s == k.s;
}
tablekey(const int index)
{
t = type::index;
i = index;
}
tablekey(const std::string& s)
{
t = type::name;
this->s = s;
i = 0;
}
type get_type() const { return t; };
#ifdef SOL_VERSION
friend IDKWTFLOL;
friend tablekeywrapper;
friend tablewrapper;
friend void bind_table_plus_plus(sol::state*);
#endif
};
class table
{
static std::string replace(const std::string& s, const std::string& from, const std::string& to);
public:
enum type
{
object,
null,
number_float,
number_integer,
boolean,
string
};
private:
double f;
int64_t i;
bool b;
std::string s;
type t;
std::shared_ptr<std::map<tablekey, table> > _m;
std::shared_ptr<std::map<tablekey, table> > m();
#ifdef SOL_VERSION
static std::map<int, table> copiedluatables;
#endif
public:
type get_type() { return type(t); };
table();
~table();
bool operator==(const table& k) const;
bool operator!=(const table& k) const;
table& operator[](const char* c);
table& operator[](const std::string& key);
table& operator[](const int key);
table& operator[](const size_t key);
bool is_string();
bool is_object();
bool is_number();
bool is_array();
bool is_integer();
bool is_float();
bool is_boolean();
bool is_null();
std::map<tablekey, table>::iterator erase(const std::map<tablekey, table>::iterator& it);
std::map<tablekey, table>::iterator find(const int key);
std::map<tablekey, table>::iterator find(const std::string& key);
bool empty();
void clear();
size_t size();
void push_back(const table& j3);
void resize(const size_t sz);
std::string to_json(const std::string indent = "");
table copy(const bool recursive = true);
bool operator==(const bool b) const;
std::map<tablekey, table>::iterator begin()
{
return m()->begin();
}
std::map<tablekey, table>::iterator end()
{
return m()->end();
}
operator bool() const
{
if (t == type::boolean) return b;
return false;
}
operator short int() const
{
return int64_t(*this);
}
operator unsigned short int() const
{
return int64_t(*this);
}
operator unsigned int() const
{
return int64_t(*this);
}
operator int() const
{
return int64_t(*this);
}
operator int64_t() const
{
if (t == type::number_integer) return i;
if (t == type::number_float) return f;
return 0;
}
operator float() const
{
return double(*this);
}
operator double() const
{
if (t == type::number_float) return f;
if (t == type::number_integer) return i;
return 0.0f;
}
operator std::string() const
{
if (t == type::string) return s;
if (t == type::number_integer)
{
std::stringstream out;
out << i;
return out.str();
}
if (t == type::number_float)
{
std::stringstream out;
out << f;
return out.str();
}
if (t == type::boolean)
{
if (b) return "true";
return "false";
}
return "null";
}
table(const int i_)
{
clear();
i = i_;
t = type::number_integer;
}
table(const int64_t i_)
{
clear();
i = i_;
t = type::number_integer;
}
table(const bool b_)
{
clear();
b = b_;
t = type::boolean;
}
table(const float f_)
{
clear();
f = f_;
t = type::number_float;
}
table(const double f_)
{
clear();
f = f_;
t = type::number_float;
}
table(const std::string& s_)
{
clear();
s = s_;
t = type::string;
}
table(const char* c)
{
clear();
s = std::string(c);
t = type::string;
}
table(const std::nullptr_t)
{
clear();
t = type::null;
}
#ifdef SOL_VERSION
friend IDKWTFLOL;
table(const sol::table& tbl);
private:
table(const sol::table& tbl, const bool handleduplicates);
public:
#endif
#ifdef NLOHMANN_JSON_VERSION_MAJOR
table(const nlohmann::json& j3);
operator nlohmann::json();
#endif
#ifdef SOL_VERSION
private:
void dynamic_set(const tablekey& key, const sol::object& value);
void dynamic_sets(const std::string& key, const sol::object& value);
void dynamic_seti(const int key, const sol::object& value);
sol::object dynamic_get(sol::this_state L, const tablekey& key);
sol::object dynamic_gets(sol::this_state L, const std::string& key);
sol::object dynamic_geti(sol::this_state L, const int key);
friend IDKWTFLOL;
friend void bind_table_plus_plus(sol::state*);
friend tablewrapper;
#endif
};
#ifdef SOL_VERSION
struct tablekeywrapper : private tablekey
{
friend tablekeywrapper;
friend void bind_table_plus_plus(sol::state* L);
tablekey tokey()
{
return *this;
}
tablekeywrapper(const tablekey& t)
{
this->t = t.t;
this->i = t.i;
this->s = t.s;
}
};
struct tablewrapper : private table
{
friend table;
friend tablewrapper;
friend void bind_table_plus_plus(sol::state* L);
auto begin() { return table::begin(); }
auto end() { return table::end(); }
size_t size() { return table::size(); }
table totable()
{
return *this;
}
tablewrapper(const table& t)
{
this->t = t.t;
this->b = t.b;
this->i = t.i;
this->f = t.f;
this->s = t.s;
this->_m = t._m;
}
};
struct IDKWTFLOL
{
struct lua_iterator_state {
typedef std::map<tablekey, table>::iterator it_t;
it_t it;
it_t last;
lua_iterator_state(table& mt)
: it(mt.m()->begin()), last(mt.m()->end()) {
}
};
static std::tuple<sol::object, sol::object> my_next(
sol::user<lua_iterator_state&> user_it_state,
sol::this_state l) {
// this gets called
// to start the first iteration, and every
// iteration there after
// the state you passed in my_pairs is argument 1
// the key value is argument 2, but we do not
// care about the key value here
lua_iterator_state& it_state = user_it_state;
auto& it = it_state.it;
if (it == it_state.last) {
// return nil to signify that
// there's nothing more to work with.
return std::make_tuple(sol::object(sol::lua_nil),
sol::object(sol::lua_nil));
}
auto itderef = *it;
// 2 values are returned (pushed onto the stack):
// the key and the value
// the state is left alone
auto r = std::make_tuple(
sol::object(l, sol::in_place, tablekeywrapper(it->first)),
sol::object(l, sol::in_place, tablewrapper(it->second)));
// the iterator must be moved forward one before we return
std::advance(it, 1);
return r;
}
static auto my_pairs(table& mt) {
// pairs expects 3 returns:
// the "next" function on how to advance,
// the "table" itself or some state,
// and an initial key value (can be nil)
// prepare our state
lua_iterator_state it_state(mt);
// sol::user is a space/time optimization over regular
// usertypes, it's incompatible with regular usertypes and
// stores the type T directly in lua without any pretty
// setup saves space allocation and a single dereference
return std::make_tuple(&my_next,
sol::user<lua_iterator_state>(std::move(it_state)),
sol::lua_nil);
}
};
#endif
}
//Cleanup
#undef TABLEPLUSPLUS_INCLUDE_SOL
#undef TABLEPLUSPLUS_INCLUDE_JSON
#undef TABLEPLUSPLUS_LUATABLEFUNCTION
#endif