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"
22class MSTL_API toml_value : public istringify<toml_value> {
34 virtual ~toml_value() =
default;
35 MSTL_NODISCARD
virtual types type() const noexcept = 0;
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; }
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; }
53 MSTL_NODISCARD
string to_string()
const;
54 MSTL_NODISCARD
string to_document()
const;
60class MSTL_API toml_boolean final :
public toml_value {
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; }
71class MSTL_API toml_integer final :
public toml_value {
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; }
82class MSTL_API toml_float final :
public toml_value {
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; }
93class MSTL_API toml_string final :
public toml_value {
104 string_type str_type;
107 explicit toml_string(
string v,
const string_type t = Basic) noexcept
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; }
116class MSTL_API toml_datetime final :
public toml_value {
127 datetime_type dt_type;
130 explicit toml_datetime(
const string_view v,
const datetime_type type) noexcept
133 case datetime_type::OffsetDateTime: {
135 dt.try_parse_ISO_UTC(v);
139 case datetime_type::LocalDateTime: {
145 case datetime_type::LocalDate: {
151 case datetime_type::LocalTime:
default: {
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; }
164 MSTL_NODISCARD
string get_string_value() const noexcept {
166 case datetime_type::OffsetDateTime: {
167 return value.to_string_ISO_UTC();
169 case datetime_type::LocalDateTime: {
170 return value.to_string_ISO();
172 case datetime_type::LocalDate: {
173 return value.date().to_string();
175 case datetime_type::LocalTime:
default: {
176 return value.time().to_string();
181 MSTL_NODISCARD datetime_type get_datetime_type() const noexcept {
return dt_type; }
184class MSTL_API toml_array final :
public toml_value {
186 vector<toml_ptr> elements;
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;
195 MSTL_NODISCARD types type() const noexcept
override {
return Array; }
196 MSTL_NODISCARD
const toml_array* as_array() const noexcept
override {
return this; }
198 void add_element(unique_ptr<toml_value> value) {
199 elements.emplace_back(
_MSTL move(value));
202 MSTL_NODISCARD
const toml_value* get_element(
const size_t index)
const noexcept {
203 if (index < elements.size())
return elements[index].get();
207 MSTL_NODISCARD
size_t size() const noexcept {
return elements.size(); }
208 MSTL_NODISCARD
const vector<toml_ptr>& get_elements() const noexcept {
return elements; }
211class MSTL_API toml_table final :
public toml_value {
213 unordered_map<string, toml_ptr> members{};
214 bool is_inline_table =
false;
217 toml_table() =
default;
218 explicit toml_table(
const bool is_inline) : is_inline_table(is_inline) {}
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;
225 MSTL_NODISCARD types type() const noexcept
override {
return Table; }
226 MSTL_NODISCARD
const toml_table* as_table() const noexcept
override {
return this; }
228 void add_member(
const string& key, unique_ptr<toml_value> value) {
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();
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();
244 MSTL_NODISCARD
bool has_member(
const string& key)
const {
245 return members.find(key) != members.end();
248 MSTL_NODISCARD
const unordered_map<string, toml_ptr>& get_members() const noexcept {
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; }
257string MSTL_API toml_value_to_string(
const toml_value* value);
258string MSTL_API toml_value_document(
const toml_value* root);
261MSTL_ALWAYS_INLINE_INLINE
string to_string(
const toml_value* value) {
262 return _INNER toml_value_to_string(value);
264MSTL_ALWAYS_INLINE_INLINE
string to_string(
const toml_value& value) {
265 return _INNER toml_value_to_string(&value);
267MSTL_ALWAYS_INLINE_INLINE
string to_string(
const toml_ptr& value) {
268 return _INNER toml_value_to_string(value.
get());
271MSTL_NODISCARD MSTL_ALWAYS_INLINE_INLINE
string toml_value::to_string()
const {
272 return _INNER toml_value_to_string(
this);
276MSTL_ALWAYS_INLINE_INLINE
string toml_document(
const toml_value* value) {
277 return _INNER toml_value_document(value);
279MSTL_ALWAYS_INLINE_INLINE
string toml_document(
const toml_value& value) {
280 return _INNER toml_value_document(&value);
282MSTL_ALWAYS_INLINE_INLINE
string toml_document(
const toml_ptr& value) {
283 return _INNER toml_value_document(value.
get());
286MSTL_NODISCARD MSTL_ALWAYS_INLINE_INLINE
string toml_value::to_document()
const {
287 return _INNER toml_value_document(
this);
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()))
获取容器的大小