-
Notifications
You must be signed in to change notification settings - Fork 0
/
Object.hpp
184 lines (179 loc) · 4.95 KB
/
Object.hpp
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
#ifndef OBJECT_HPP
#define OBJECT_HPP
#include "Common.hpp"
#include "StringPool.hpp"
namespace mp
{
struct Object;
struct Value
{
_MP_DISABLE_COPY(Value)
struct String // const string view
{
const static size_type P = 19260817;
const char *content;
size_type size;
size_type hash;
static size_type hash_code(const char *src, size_type size)
{
size_type hash{};
for (size_type i = 0; i < size; ++i)
hash = hash * P + *src++;
return hash;
}
static String from_cstr(const char *src)
{
String ret = {.content = src, .size = (size_type)strlen(src)};
ret.hash = hash_code(src, ret.size);
return ret;
}
static String from_cstr(const char *src, size_type size)
{
return {.content = src, .size = size, .hash = hash_code(src, size)};
}
bool operator==(String rhs) const
{
if (size != rhs.size || hash != rhs.hash)
return false;
return strcmp(content, rhs.content) == 0;
}
char operator[](size_type index) const { return content[index]; }
template <typename OS>
friend decltype(auto) operator<<(OS &&os, String str)
{
for (size_type i = 0; i < str.size; ++i)
os << str.content[i];
return std::forward<OS>(os);
}
};
struct Array
{
size_type size;
size_type capacity;
Value *content;
static size_type size_to_cap(size_type size)
{
size_type cap = 1;
while (cap < size)
cap <<= 1;
return cap;
}
Array() = default; // won't be properly constructed
explicit Array(size_type capacity)
: size(0), capacity(capacity), content((Value *)malloc(sizeof(Value) * capacity)) {}
Array &push_back(Value &&val)
{
if (size >= capacity)
content = (Value *)realloc(content, capacity <<= 1);
new (content + size++) Value(std::move(val));
return *this;
}
Value &operator[](size_type index) { return content[index]; }
const Value &operator[](size_type index) const { return content[index]; }
};
union {
bool bo;
int64_t i64;
double f64;
String str;
Array arr;
Object *obj;
} data;
enum
{
null_flag,
bool_flag,
i64_flag,
f64_flag,
str_flag,
arr_flag,
obj_flag
} type = null_flag;
decltype(auto) get_bool()
{
assert(type == bool_flag);
return (data.bo);
}
decltype(auto) get_i64()
{
assert(type == i64_flag);
return (data.i64);
}
decltype(auto) get_f64()
{
assert(type == f64_flag);
return (data.f64);
}
decltype(auto) get_str()
{
assert(type == str_flag);
return (data.str);
}
decltype(auto) get_arr()
{
assert(type == arr_flag);
return (data.arr);
}
decltype(auto) get_obj()
{
assert(type == obj_flag);
return (data.obj);
}
Value() = default;
Value(decltype(data.bo) bo) : type(bool_flag) { data.bo = bo; }
Value(decltype(data.i64) i64) : type(i64_flag) { data.i64 = i64; }
Value(decltype(data.f64) f64) : type(f64_flag) { data.f64 = f64; }
Value(decltype(data.str) str) : type(str_flag) { data.str = str; }
Value(decltype(data.arr) arr) : type(arr_flag) { data.arr = arr; }
Value(decltype(data.obj) obj) : type(obj_flag) { data.obj = obj; }
void swap(Value &rhs) noexcept
{
std::swap(data, rhs.data);
std::swap(type, rhs.type);
}
Value(Value &&rhs) noexcept
{
memcpy(this, &rhs, sizeof(Value));
memset(&rhs, 0, sizeof(Value));
}
Value &operator=(Value &&rhs) noexcept
{
Value tmp = std::move(rhs);
swap(tmp);
return *this;
}
~Value(); // impl under declaration of Object
};
using String = Value::String;
using Array = Value::Array;
struct Object
{
struct StringHash
{
size_t operator()(String str) const { return ((size_t)str.size << 32ull) | str.hash; }
};
using pool_pointer = std::shared_ptr<StringPool>;
std::unordered_map<String, Value, StringHash> dom;
pool_pointer pool;
Value &operator[](const char *name)
{
auto it = dom.find(String::from_cstr(name));
assert(it != dom.end());
return it->second;
}
bool insert(String name, Value &&val)
{
auto status = dom.insert({name, std::move(val)});
return status.second;
}
};
inline Value::~Value()
{
if (type == arr_flag)
for (Value *cur = data.arr.content, *end = cur + data.arr.size; cur != end; ++cur)
cur->~Value();
else if (type == obj_flag)
delete data.obj;
}
} // namespace mp
#endif // OBJECT_HPP