-
Notifications
You must be signed in to change notification settings - Fork 15
/
AnyMap.hpp
204 lines (189 loc) · 6.49 KB
/
AnyMap.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//
// Created by Marc Rousavy on 30.07.24.
//
#pragma once
#include <map>
#include <memory>
#include <string>
#include <unordered_map>
#include <variant>
#include <vector>
namespace margelo::nitro {
struct AnyValue;
using AnyArray = std::vector<AnyValue>;
using AnyObject = std::unordered_map<std::string, AnyValue>;
using VariantType = std::variant<std::monostate, bool, double, int64_t, std::string, AnyArray, AnyObject>;
struct AnyValue : VariantType {
using VariantType::variant;
AnyValue(const VariantType& variant) : VariantType(variant) {}
AnyValue(VariantType&& variant) : VariantType(std::move(variant)) {}
};
/**
* Represents a JS map-like object (`Record<K, V>`).
* This is essentially a wrapper around `std::unordered_map<string, variant<...>>`.
*
* Supported values are:
* 1. Primitives
* 2. Arrays of primitives
* 3. Objects of primitives
*/
class AnyMap final {
public:
/**
* Create a new instance of AnyMap.
*/
explicit AnyMap() {}
/**
* Create a new instance of AnyMap with the given amount of spaces pre-allocated.
*/
explicit AnyMap(size_t size) {
_map.reserve(size);
}
public:
/**
* Create a new `shared_ptr` instance of AnyMap.
*/
static std::shared_ptr<AnyMap> make() {
return std::make_shared<AnyMap>();
}
/**
* Create a new `shared_ptr` instance of AnyMap with the given amount of spaces pre-allocated.
*/
static std::shared_ptr<AnyMap> make(size_t size) {
return std::make_shared<AnyMap>(size);
}
public:
/**
* Returns whether the map contains the given key, or not.
*/
bool contains(const std::string& key) const;
/**
* Removes the given key from the map, leaving no value.
*/
void remove(const std::string& key);
/**
* Deletes all keys and values inside the map.
*/
void clear() noexcept;
public:
/**
* Returns whether the value under the given key is a `null`.
* If the value is not a `null` (or there is no value at the given `key`), this returns `false`.
*/
bool isNull(const std::string& key) const;
/**
* Returns whether the value under the given key is a `double`.
* If the value is not a `double` (or there is no value at the given `key`), this returns `false`.
*/
bool isDouble(const std::string& key) const;
/**
* Returns whether the value under the given key is a `boolean`.
* If the value is not a `boolean` (or there is no value at the given `key`), this returns `false`.
*/
bool isBoolean(const std::string& key) const;
/**
* Returns whether the value under the given key is a `bigint`.
* If the value is not a `bigint` (or there is no value at the given `key`), this returns `false`.
*/
bool isBigInt(const std::string& key) const;
/**
* Returns whether the value under the given key is a `string`.
* If the value is not a `string` (or there is no value at the given `key`), this returns `false`.
*/
bool isString(const std::string& key) const;
/**
* Returns whether the value under the given key is an array.
* If the value is not an array (or there is no value at the given `key`), this returns `false`.
*/
bool isArray(const std::string& key) const;
/**
* Returns whether the value under the given key is an object.
* If the value is not an object (or there is no value at the given `key`), this returns `false`.
*/
bool isObject(const std::string& key) const;
public:
/**
* Returns the null value at the given `key`.
* If no `null` value exists at the given `key`, this method will throw.
*/
std::monostate getNull(const std::string& key) const;
/**
* Returns the double value at the given `key`.
* If no `double` value exists at the given `key`, this method will throw.
*/
double getDouble(const std::string& key) const;
/**
* Returns the boolean value at the given `key`.
* If no `boolean` value exists at the given `key`, this method will throw.
*/
bool getBoolean(const std::string& key) const;
/**
* Returns the bigint value at the given `key`.
* If no `bigint` value exists at the given `key`, this method will throw.
*/
int64_t getBigInt(const std::string& key) const;
/**
* Returns the string value at the given `key`.
* If no `string` value exists at the given `key`, this method will throw.
*/
std::string getString(const std::string& key) const;
/**
* Returns the array value at the given `key`.
* If no array value exists at the given `key`, this method will throw.
*/
AnyArray getArray(const std::string& key) const;
/**
* Returns the object value at the given `key`.
* If no object value exists at the given `key`, this method will throw.
*/
AnyObject getObject(const std::string& key) const;
public:
/**
* Set the value at the given key to `null`.
* If the key already exists, this will overwrite the value at that `key`.
*/
void setNull(const std::string& key);
/**
* Set the value at the given key to the given `double`.
* If the key already exists, this will overwrite the value at that `key`.
*/
void setDouble(const std::string& key, double value);
/**
* Set the value at the given key to the given `boolean`.
* If the key already exists, this will overwrite the value at that `key`.
*/
void setBoolean(const std::string& key, bool value);
/**
* Set the value at the given key to the given `bigint`.
* If the key already exists, this will overwrite the value at that `key`.
*/
void setBigInt(const std::string& key, int64_t value);
/**
* Set the value at the given key to the given `string`.
* If the key already exists, this will overwrite the value at that `key`.
*/
void setString(const std::string& key, const std::string& value);
/**
* Set the value at the given key to the given array.
* If the key already exists, this will overwrite the value at that `key`.
*/
void setArray(const std::string& key, const AnyArray& value);
/**
* Set the value at the given key to the given object.
* If the key already exists, this will overwrite the value at that `key`.
*/
void setObject(const std::string& key, const AnyObject& value);
/**
* Set the value at the given key to the given `AnyValue`.
* If the key already exists, this will overwrite the value at that `key`.
*/
void setAny(const std::string& key, const AnyValue& value);
public:
/**
* Get the actual C++ map that holds all keys and variant values.
*/
const std::unordered_map<std::string, AnyValue>& getMap() const;
private:
std::unordered_map<std::string, AnyValue> _map;
};
} // namespace margelo::nitro