1#ifndef MSTL_CORE_FILE_TOML_BUILDER_HPP__
2#define MSTL_CORE_FILE_TOML_BUILDER_HPP__
3#include "MSTL/core/container/stack.hpp"
5#include "toml_value.hpp"
8class MSTL_API toml_builder {
10 enum RANGE_TYPE { TABLE, INLINE_TABLE, ARRAY };
13 RANGE_TYPE type = TABLE;
15 toml_table* table_ptr =
nullptr;
16 toml_array* array_ptr;
20 frame(
const RANGE_TYPE t, toml_table* tbl) : type(t), table_ptr(tbl) {}
21 frame(
const RANGE_TYPE t, toml_array* arr) : type(t), array_ptr(arr) {}
23 frame(
const frame&) =
default;
24 frame& operator =(
const frame&) =
default;
25 frame(frame&&) =
default;
26 frame& operator =(frame&&) =
default;
30 _MSTL stack<frame> contexts_;
31 unique_ptr<toml_table> root_;
36 toml_builder& value_impl(unique_ptr<T> v) {
37 if (contexts_.empty()) {
38 throw_exception(toml_exception(
"Cannot add value to root (root must be a table)"));
41 const auto& top = contexts_.top();
42 if (top.type == ARRAY) {
43 top.array_ptr->add_element(
_MSTL move(v));
44 }
else if (top.type == TABLE || top.type == INLINE_TABLE) {
45 if (current_key_.empty()) {
46 throw_exception(toml_exception(
"No key set for value in table"));
48 if (top.table_ptr->has_member(current_key_)) {
49 throw_exception(toml_exception((
"Duplicate key: " + current_key_).
data()));
51 top.table_ptr->add_member(current_key_,
_MSTL move(v));
57 template <
typename Iterable, enable_if_t<is_iterable_v<Iterable>,
int> = 0>
58 toml_builder& value_iterable_dispatch(
const Iterable& t) {
59 return this->value_iterable_impl(t);
62 template <
typename Map, enable_if_t<is_maplike_v<Map>,
int> = 0>
63 toml_builder& value_iterable_impl(
const Map& map) {
65 for (
const auto& pair : map) {
66 this->key(pair.first).value(pair.second);
72 template <
typename Iterable, enable_if_t<!is_maplike_v<Iterable>,
int> = 0>
73 toml_builder& value_iterable_impl(
const Iterable& t) {
75 for (
const auto& element : t) {
82 toml_table* get_or_create_table_path(
const vector<string>& path)
const;
83 toml_array* get_or_create_array_for_array_table(
const vector<string>& path)
const;
87 toml_builder(
const toml_builder&) =
delete;
88 toml_builder& operator =(
const toml_builder&) =
delete;
89 toml_builder(toml_builder&&) =
default;
90 toml_builder& operator =(toml_builder&&) =
default;
92 toml_builder& key(
const string& k);
94 toml_builder& begin_table(
const string& table_name);
95 toml_builder& begin_table(
const vector<string>& table_path);
96 toml_builder& end_table();
98 toml_builder& begin_inline_table();
99 toml_builder& end_inline_table();
101 toml_builder& begin_array();
102 toml_builder& end_array();
104 toml_builder& begin_array_table(
const string& array_table_name);
105 toml_builder& begin_array_table(
const vector<string>& array_table_path);
106 toml_builder& end_array_table();
111 toml_builder& value(
const int v) {
return value(
static_cast<int64_t>(v)); }
113 toml_builder& value(
const string& v) {
116 toml_builder& value(
const char* v) {
return value(
string(v)); }
117 toml_builder& value(
const string_view v) {
return value(
string(v)); }
118 toml_builder& value(unique_ptr<toml_value>&& v) {
return value_impl(
_MSTL move(v)); }
120 toml_builder& value_string(
const string& v, toml_string::string_type type) {
124 toml_builder& value_datetime(
const string_view v, toml_datetime::datetime_type type) {
128 template <
typename Iterable>
129 toml_builder& value(
const Iterable& t) {
130 return this->value_iterable_dispatch(t);
133 toml_builder& value_table(
_MSTL function<
void(toml_builder&)>&& build_func);
134 toml_builder& value_inline_table(
_MSTL function<
void(toml_builder&)>&& build_func);
135 toml_builder& value_array(
_MSTL function<
void(toml_builder&)>&& build_func);
137 unique_ptr<toml_table> build();
long long int64_t
64位有符号整数类型
decltype(nullptr) nullptr_t
空指针类型
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result)
移动范围元素
MSTL_NODISCARD MSTL_ALWAYS_INLINE constexpr decltype(auto) data(Container &cont) noexcept(noexcept(cont.data()))
获取容器的底层数据指针
MSTL_CONSTEXPR20 unique_ptr< T > make_unique(Args &&... args)
创建unique_ptr