Sese Framework  2.3.0
A cross-platform framework
Loading...
Searching...
No Matches
Value.h
Go to the documentation of this file.
1// Copyright 2024 libsese
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
19
20#pragma once
21
22#include <map>
23#include <memory>
24#include <variant>
25#include <vector>
26#include <optional>
27#include <stack>
28#include <tuple>
29
30#include <sese/Config.h>
32#include <sese/text/Format.h>
33
34namespace sese {
35
37class Value {
38public:
39 enum class Type {
40 NONE = 0,
41 BOOL,
42 INT,
43 DOUBLE,
44 STRING,
45 BLOB,
46 LIST,
47 DICT
48 };
49
50 using Integer = int64_t;
51 using String = std::string;
52 using Blob = std::vector<uint8_t>;
53
54 using StreamifyStack = std::stack<std::tuple<const Value *, unsigned int, unsigned int>>;
55 using StreamifyIterStack = std::stack<std::map<std::string, std::shared_ptr<Value>>::const_iterator>;
56
57 class List;
58 class Dict;
59
61 class Null {};
62
64 class List {
65 public:
66 using Raw = std::vector<std::shared_ptr<Value>>;
67 using Iterator = Raw::iterator;
68 using ConstIterator = Raw::const_iterator;
69 using ReverseIterator = Raw::reverse_iterator;
70 using ConstReverseIterator = Raw::const_reverse_iterator;
71
72 [[nodiscard]] size_t empty() const;
73 [[nodiscard]] size_t size() const;
74 void reserve(size_t size);
75 void resize(size_t size);
76 void clear();
77
78 const std::shared_ptr<Value> operator[](size_t index) const;
79 std::shared_ptr<Value> operator[](size_t index);
80
81 size_t erase(const std::shared_ptr<Value> &value);
84 Iterator erase(const Iterator &first, const Iterator &last);
85 ConstIterator erase(const ConstIterator &first, const ConstIterator &last);
86
88 [[nodiscard]] ConstIterator begin() const;
89 Iterator end();
90 [[nodiscard]] ConstIterator end() const;
91
92 [[nodiscard]] ConstIterator cbegin() const;
93 [[nodiscard]] ConstIterator cend() const;
94
96 [[nodiscard]] ConstReverseIterator rbegin() const;
98 [[nodiscard]] ConstReverseIterator rend() const;
99
100 [[nodiscard]] const std::shared_ptr<Value> front() const;
101 std::shared_ptr<Value> front();
102 [[nodiscard]] const std::shared_ptr<Value> back() const;
103 std::shared_ptr<Value> back();
104
108 std::shared_ptr<Value> appendRef(Value &&value);
109 void append(Value &&value) &;
110 void append(bool value) &;
111 void append(Integer value) &;
112 void append(double value) &;
113 void append(const char *value) &;
114 void append(String &&value) &;
115 void append(Blob &&value) &;
116 void append(const char *bytes, size_t length) &;
117 void append(List &&value) &;
118 void append(Dict &&value) &;
119
120 List &&append(Value &&value) &&;
121 List &&append(bool value) &&;
122 List &&append(Integer value) &&;
123 List &&append(double value) &&;
124 List &&append(const char *value) &&;
125 List &&append(String &&value) &&;
126 List &&append(const char *bytes, size_t length) &&;
127 List &&append(Blob &&value) &&;
128 List &&append(List &&value) &&;
129 List &&append(Dict &&value) &&;
130
131 Iterator insert(Iterator it, Value &&value);
132
133 private:
135 };
136
138 class Dict {
139 public:
140 using Raw = std::map<String, std::shared_ptr<Value>>;
141 using Iterator = Raw::iterator;
142 using ConstIterator = Raw::const_iterator;
143 using ReverseIterator = Raw::reverse_iterator;
144 using ConstReverseIterator = Raw::const_reverse_iterator;
145
146 Dict() = default;
147 ~Dict();
148 // move constructor
149 Dict(Dict &&other) noexcept;
150 Dict &operator=(Dict &&other) noexcept;
151 // copy constructor
152 Dict(const Dict &other) = delete;
153 Dict &operator=(const Dict &other) = delete;
154
155 [[nodiscard]] bool empty() const;
156 [[nodiscard]] size_t size() const;
157 void clear();
158 [[nodiscard]] bool contains(const String &key) const;
159
160 Iterator begin();
161 [[nodiscard]] ConstIterator begin() const;
162 Iterator end();
163 [[nodiscard]] ConstIterator end() const;
164
165 [[nodiscard]] ConstIterator cbegin() const;
166 [[nodiscard]] ConstIterator cend() const;
167
169 [[nodiscard]] ConstReverseIterator rbegin() const;
171 [[nodiscard]] ConstReverseIterator rend() const;
172
173 Iterator erase(const Iterator &it);
174 Iterator erase(const ConstIterator &it);
175
176 [[nodiscard]] const std::shared_ptr<Value> find(const String &key) const;
177 std::shared_ptr<Value> find(const String &key);
178
183 std::shared_ptr<Value> setRef(const String &key, Value &&value);
184 void set(const String &key, Value &&value) &;
185 void set(const String &key, bool value) &;
186 void set(const String &key, Integer value) &;
187 void set(const String &key, double value) &;
188 void set(const String &key, const char *value) &;
189 void set(const String &key, const char *value, size_t length) &;
190 void set(const String &key, String &&value) &;
191 void set(const String &key, Blob &&value) &;
192 void set(const String &key, List &&value) &;
193 void set(const String &key, Dict &&value) &;
194
195 Dict &&set(const String &key, Value &&value) &&;
196 Dict &&set(const String &key, bool value) &&;
197 Dict &&set(const String &key, Integer value) &&;
198 Dict &&set(const String &key, double value) &&;
199 Dict &&set(const String &key, const char *value) &&;
200 Dict &&set(const String &key, const char *value, size_t length) &&;
201 Dict &&set(const String &key, String &&value) &&;
202 Dict &&set(const String &key, Blob &&value) &&;
203 Dict &&set(const String &key, List &&value) &&;
204 Dict &&set(const String &key, Dict &&value) &&;
205
206 bool remove(const String &key);
207
208 private:
210 };
211
212 Value() = default;
213 explicit Value(Type type);
215 explicit Value(bool value);
217 explicit Value(Integer value);
219 explicit Value(double value);
221 explicit Value(const char *value);
223 explicit Value(const char *bytes, size_t length);
225 explicit Value(String &&value) noexcept;
227 explicit Value(Blob &&value);
229 explicit Value(List &&value);
231 explicit Value(Dict &&value);
232
233 static Value list();
234 static Value dict();
235
236 [[nodiscard]] Type getType() const;
237
238 [[nodiscard]] bool isNull() const;
239 [[nodiscard]] bool isBool() const;
240 [[nodiscard]] bool isInt() const;
241 [[nodiscard]] bool isDouble() const;
242 [[nodiscard]] bool isString() const;
243 [[nodiscard]] bool isBlob() const;
244 [[nodiscard]] bool isList() const;
245 [[nodiscard]] bool isDict() const;
246
249 [[nodiscard]] std::optional<bool> getIfBool() const;
252 [[nodiscard]] std::optional<Integer> getIfInt() const;
255 [[nodiscard]] std::optional<double> getIfDouble() const;
256 [[nodiscard]] String *getIfString();
257 [[nodiscard]] Blob *getIfBlob();
258 [[nodiscard]] Dict *getIfDict();
259 [[nodiscard]] List *getIfList();
260
261 [[nodiscard]] bool getBool() const;
262 [[nodiscard]] Integer getInt() const;
263 [[nodiscard]] double getDouble() const;
264 [[nodiscard]] const String &getString() const;
265 [[nodiscard]] String &getString();
266 [[nodiscard]] const Blob &getBlob() const;
267 [[nodiscard]] Blob &getBlob();
268 [[nodiscard]] const List &getList() const;
269 [[nodiscard]] List &getList();
270 [[nodiscard]] const Dict &getDict() const;
271 [[nodiscard]] Dict &getDict();
272
273 [[nodiscard]] std::string toStringBasic() const noexcept;
274 [[nodiscard]] std::string toString() const noexcept;
275 void toString(text::StringBuilder &string_builder) const noexcept;
276
277 bool operator==(const Value &rhs) const;
278 bool operator!=(const Value &rhs) const;
279
280private:
281 static void writeSpace(text::StringBuilder &string_builder, size_t count);
282 static void toStringDict(
283 text::StringBuilder &string_builder,
284 StreamifyStack &stack,
285 StreamifyIterStack &map_iter_stack
286 );
287 static void toStringList(
288 text::StringBuilder &string_builder,
289 StreamifyStack &stack,
290 StreamifyIterStack &map_iter_stack
291 );
292
293 std::variant<Null, bool, Integer, double, String, Blob, List, Dict> data;
294};
295
296template<>
297struct text::overload::Formatter<Value> {
298
299 bool parse(const std::string &) {
300 return true;
301 }
302
303 void format(FmtCtx &ctx, Value &value) const {
304 value.toString(ctx.builder);
305 }
306};
307
308} // namespace sese