MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
toml_value.hpp
1#ifndef MSTL_CORE_FILE_TOML_TOML_VALUE_HPP__
2#define MSTL_CORE_FILE_TOML_TOML_VALUE_HPP__
3#include "MSTL/core/container/unordered_map.hpp"
4#include "MSTL/core/container/vector.hpp"
6#include "MSTL/core/interface/istringify.hpp"
7#include "MSTL/core/time/datetime.hpp"
9
10MSTL_ERROR_BUILD_FINAL_CLASS(toml_exception, value_exception, "TOML Operation Failed.")
11
12
13class toml_value;
14class toml_boolean;
15class toml_integer;
16class toml_float;
17class toml_string;
18class toml_datetime;
19class toml_array;
20class toml_table;
21
22class MSTL_API toml_value : public istringify<toml_value> {
23public:
24 enum types {
25 Boolean,
26 Integer,
27 Float,
28 String,
29 DateTime,
30 Array,
31 Table
32 };
33
34 virtual ~toml_value() = default;
35 MSTL_NODISCARD virtual types type() const noexcept = 0;
36
37 MSTL_NODISCARD virtual const toml_boolean* as_boolean() const noexcept { return nullptr; }
38 MSTL_NODISCARD virtual const toml_integer* as_integer() const noexcept { return nullptr; }
39 MSTL_NODISCARD virtual const toml_float* as_float() const noexcept { return nullptr; }
40 MSTL_NODISCARD virtual const toml_string* as_string() const noexcept { return nullptr; }
41 MSTL_NODISCARD virtual const toml_datetime* as_datetime() const noexcept { return nullptr; }
42 MSTL_NODISCARD virtual const toml_array* as_array() const noexcept { return nullptr; }
43 MSTL_NODISCARD virtual const toml_table* as_table() const noexcept { return nullptr; }
44
45 MSTL_NODISCARD bool is_boolean() const noexcept { return type() == Boolean; }
46 MSTL_NODISCARD bool is_integer() const noexcept { return type() == Integer; }
47 MSTL_NODISCARD bool is_float() const noexcept { return type() == Float; }
48 MSTL_NODISCARD bool is_string() const noexcept { return type() == String; }
49 MSTL_NODISCARD bool is_datetime() const noexcept { return type() == DateTime; }
50 MSTL_NODISCARD bool is_array() const noexcept { return type() == Array; }
51 MSTL_NODISCARD bool is_table() const noexcept { return type() == Table; }
52
53 MSTL_NODISCARD string to_string() const;
54 MSTL_NODISCARD string to_document() const;
55};
56
57using toml_ptr = unique_ptr<toml_value>;
58
59
60class MSTL_API toml_boolean final : public toml_value {
61private:
62 bool value;
63
64public:
65 explicit toml_boolean(const bool v) noexcept : value(v) {}
66 MSTL_NODISCARD types type() const noexcept override { return Boolean; }
67 MSTL_NODISCARD const toml_boolean* as_boolean() const noexcept override { return this; }
68 MSTL_NODISCARD bool get_value() const noexcept { return value; }
69};
70
71class MSTL_API toml_integer final : public toml_value {
72private:
73 int64_t value;
74
75public:
76 explicit toml_integer(const int64_t v) noexcept : value(v) {}
77 MSTL_NODISCARD types type() const noexcept override { return Integer; }
78 MSTL_NODISCARD const toml_integer* as_integer() const noexcept override { return this; }
79 MSTL_NODISCARD int64_t get_value() const noexcept { return value; }
80};
81
82class MSTL_API toml_float final : public toml_value {
83private:
84 double value;
85
86public:
87 explicit toml_float(const double v) noexcept : value(v) {}
88 MSTL_NODISCARD types type() const noexcept override { return Float; }
89 MSTL_NODISCARD const toml_float* as_float() const noexcept override { return this; }
90 MSTL_NODISCARD double get_value() const noexcept { return value; }
91};
92
93class MSTL_API toml_string final : public toml_value {
94public:
95 enum string_type {
96 Basic, // "string"
97 Literal, // 'string'
98 MultiBasic, // """string"""
99 MultiLiteral // '''string'''
100 };
101
102private:
103 string value;
104 string_type str_type;
105
106public:
107 explicit toml_string(string v, const string_type t = Basic) noexcept
108 : value(_MSTL move(v)), str_type(t) {}
109
110 MSTL_NODISCARD types type() const noexcept override { return String; }
111 MSTL_NODISCARD const toml_string* as_string() const noexcept override { return this; }
112 MSTL_NODISCARD const string& get_value() const noexcept { return value; }
113 MSTL_NODISCARD string_type get_string_type() const noexcept { return str_type; }
114};
115
116class MSTL_API toml_datetime final : public toml_value {
117public:
118 enum datetime_type {
119 OffsetDateTime, // 1979-05-27T07:32:00Z
120 LocalDateTime, // 1979-05-27T07:32:00
121 LocalDate, // 1979-05-27
122 LocalTime // 07:32:00
123 };
124
125private:
126 datetime value;
127 datetime_type dt_type;
128
129public:
130 explicit toml_datetime(const string_view v, const datetime_type type) noexcept
131 : dt_type(type) {
132 switch (dt_type) {
133 case datetime_type::OffsetDateTime: {
134 datetime dt;
135 dt.try_parse_ISO_UTC(v);
136 value = dt;
137 break;
138 }
139 case datetime_type::LocalDateTime: {
140 datetime dt;
141 dt.try_parse_ISO(v);
142 value = dt;
143 break;
144 }
145 case datetime_type::LocalDate: {
146 date d{};
147 d.try_parse(v);
148 value = d;
149 break;
150 }
151 case datetime_type::LocalTime: default: {
152 time t{};
153 t.try_parse(v);
154 value = t;
155 break;
156 }
157 }
158 }
159
160 MSTL_NODISCARD types type() const noexcept override { return DateTime; }
161 MSTL_NODISCARD const toml_datetime* as_datetime() const noexcept override { return this; }
162 MSTL_NODISCARD const datetime& get_value() const noexcept { return value; }
163
164 MSTL_NODISCARD string get_string_value() const noexcept {
165 switch (dt_type) {
166 case datetime_type::OffsetDateTime: {
167 return value.to_string_ISO_UTC();
168 }
169 case datetime_type::LocalDateTime: {
170 return value.to_string_ISO();
171 }
172 case datetime_type::LocalDate: {
173 return value.date().to_string();
174 }
175 case datetime_type::LocalTime: default: {
176 return value.time().to_string();
177 }
178 }
179 }
180
181 MSTL_NODISCARD datetime_type get_datetime_type() const noexcept { return dt_type; }
182};
183
184class MSTL_API toml_array final : public toml_value {
185private:
186 vector<toml_ptr> elements;
187
188public:
189 toml_array() = default;
190 toml_array(const toml_array&) = delete;
191 toml_array& operator =(const toml_array&) = delete;
192 toml_array(toml_array&&) = default;
193 toml_array& operator =(toml_array&&) = default;
194
195 MSTL_NODISCARD types type() const noexcept override { return Array; }
196 MSTL_NODISCARD const toml_array* as_array() const noexcept override { return this; }
197
198 void add_element(unique_ptr<toml_value> value) {
199 elements.emplace_back(_MSTL move(value));
200 }
201
202 MSTL_NODISCARD const toml_value* get_element(const size_t index) const noexcept {
203 if (index < elements.size()) return elements[index].get();
204 return nullptr;
205 }
206
207 MSTL_NODISCARD size_t size() const noexcept { return elements.size(); }
208 MSTL_NODISCARD const vector<toml_ptr>& get_elements() const noexcept { return elements; }
209};
210
211class MSTL_API toml_table final : public toml_value {
212private:
213 unordered_map<string, toml_ptr> members{};
214 bool is_inline_table = false;
215
216public:
217 toml_table() = default;
218 explicit toml_table(const bool is_inline) : is_inline_table(is_inline) {}
219
220 toml_table(const toml_table&) = delete;
221 toml_table& operator =(const toml_table&) = delete;
222 toml_table(toml_table&&) = default;
223 toml_table& operator =(toml_table&&) = default;
224
225 MSTL_NODISCARD types type() const noexcept override { return Table; }
226 MSTL_NODISCARD const toml_table* as_table() const noexcept override { return this; }
227
228 void add_member(const string& key, unique_ptr<toml_value> value) {
229 members[key] = _MSTL move(value);
230 }
231
232 MSTL_NODISCARD const toml_value* get_member(const string& key) const {
233 const auto it = members.find(key);
234 if (it != members.end()) return it->second.get();
235 return nullptr;
236 }
237
238 MSTL_NODISCARD toml_value* get_member(const string& key) {
239 const auto it = members.find(key);
240 if (it != members.end()) return it->second.get();
241 return nullptr;
242 }
243
244 MSTL_NODISCARD bool has_member(const string& key) const {
245 return members.find(key) != members.end();
246 }
247
248 MSTL_NODISCARD const unordered_map<string, toml_ptr>& get_members() const noexcept {
249 return members;
250 }
251
252 MSTL_NODISCARD bool is_inline() const noexcept { return is_inline_table; }
253 void set_inline(const bool is_inline) noexcept { is_inline_table = is_inline; }
254};
255
257string MSTL_API toml_value_to_string(const toml_value* value);
258string MSTL_API toml_value_document(const toml_value* root);
260
261MSTL_ALWAYS_INLINE_INLINE string to_string(const toml_value* value) {
262 return _INNER toml_value_to_string(value);
263}
264MSTL_ALWAYS_INLINE_INLINE string to_string(const toml_value& value) {
265 return _INNER toml_value_to_string(&value);
266}
267MSTL_ALWAYS_INLINE_INLINE string to_string(const toml_ptr& value) {
268 return _INNER toml_value_to_string(value.get());
269}
270
271MSTL_NODISCARD MSTL_ALWAYS_INLINE_INLINE string toml_value::to_string() const {
272 return _INNER toml_value_to_string(this);
273}
274
275
276MSTL_ALWAYS_INLINE_INLINE string toml_document(const toml_value* value) {
277 return _INNER toml_value_document(value);
278}
279MSTL_ALWAYS_INLINE_INLINE string toml_document(const toml_value& value) {
280 return _INNER toml_value_document(&value);
281}
282MSTL_ALWAYS_INLINE_INLINE string toml_document(const toml_ptr& value) {
283 return _INNER toml_value_document(value.get());
284}
285
286MSTL_NODISCARD MSTL_ALWAYS_INLINE_INLINE string toml_value::to_document() const {
287 return _INNER toml_value_document(this);
288}
289
291#endif // MSTL_CORE_FILE_TOML_TOML_VALUE_HPP__
独占智能指针
MSTL_CONSTEXPR20 pointer get() const noexcept
获取原始指针
long long int64_t
64位有符号整数类型
#define MSTL_ERROR_BUILD_FINAL_CLASS(THIS, BASE, INFO)
构建最终异常类宏
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_INNER__
结束inner命名空间
#define _INNER
inner命名空间前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
#define MSTL_BEGIN_INNER__
开始inner命名空间
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result)
移动范围元素
MSTL_NODISCARD MSTL_ALWAYS_INLINE constexpr decltype(auto) size(const Container &cont) noexcept(noexcept(cont.size()))
获取容器的大小
判断类型是否为数组类型
判断类型是否为布尔类型
变量处理异常
MSTL独占智能指针