MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
ini_value.hpp
1#ifndef MSTL_CORE_FILE_INI_INI_VALUE_HPP__
2#define MSTL_CORE_FILE_INI_INI_VALUE_HPP__
3#include "MSTL/core/container/unordered_map.hpp"
5#include "MSTL/core/interface/istringify.hpp"
7
8MSTL_ERROR_BUILD_FINAL_CLASS(ini_exception, value_exception, "INI Operation Failed.")
9
10
11class ini_value;
12class ini_section;
13class ini_property;
14
15class MSTL_API ini_value : public istringify<ini_value> {
16public:
17 enum types {
18 Section,
19 Property
20 };
21
22 virtual ~ini_value() = default;
23 MSTL_NODISCARD virtual types type() const noexcept = 0;
24
25 MSTL_NODISCARD virtual const ini_section* as_section() const noexcept { return nullptr; }
26 MSTL_NODISCARD virtual const ini_property* as_property() const noexcept { return nullptr; }
27
28 MSTL_NODISCARD bool is_section() const noexcept { return type() == Section; }
29 MSTL_NODISCARD bool is_property() const noexcept { return type() == Property; }
30
31 MSTL_NODISCARD string to_string() const;
32 MSTL_NODISCARD string to_document() const;
33};
34
35using ini_ptr = unique_ptr<ini_value>;
36
37
38class MSTL_API ini_property final : public ini_value {
39private:
40 string value_;
41
42public:
43 explicit ini_property(string v) noexcept : value_(_MSTL move(v)) {}
44
45 MSTL_NODISCARD types type() const noexcept override { return Property; }
46 MSTL_NODISCARD const ini_property* as_property() const noexcept override { return this; }
47
48 MSTL_NODISCARD const string& get_value() const noexcept { return value_; }
49 void set_value(string v) noexcept { value_ = _MSTL move(v); }
50
51 MSTL_NODISCARD int get_int(int default_value = 0) const noexcept;
52 MSTL_NODISCARD double get_double(double default_value = 0.0) const noexcept;
53 MSTL_NODISCARD bool get_bool(bool default_value = false) const noexcept;
54};
55
56class MSTL_API ini_section final : public ini_value {
57private:
58 unordered_map<string, unique_ptr<ini_property>> properties_;
59 string name_;
60
61public:
62 explicit ini_section(string name = "") noexcept : name_(_MSTL move(name)) {}
63
64 ini_section(const ini_section&) = delete;
65 ini_section& operator =(const ini_section&) = delete;
66 ini_section(ini_section&&) = default;
67 ini_section& operator =(ini_section&&) = default;
68
69 MSTL_NODISCARD types type() const noexcept override { return Section; }
70 MSTL_NODISCARD const ini_section* as_section() const noexcept override { return this; }
71
72 MSTL_NODISCARD const string& get_name() const noexcept { return name_; }
73 void set_name(string name) noexcept { name_ = _MSTL move(name); }
74
75 void add_property(const string& key, unique_ptr<ini_property> property) {
76 properties_[key] = _MSTL move(property);
77 }
78
79 void set_property(const string& key, const string& value) {
80 properties_[key] = make_unique<ini_property>(value);
81 }
82
83 MSTL_NODISCARD const ini_property* get_property(const string& key) const {
84 const auto it = properties_.find(key);
85 if (it != properties_.end()) return it->second.get();
86 return nullptr;
87 }
88
89 MSTL_NODISCARD ini_property* get_property(const string& key) {
90 const auto it = properties_.find(key);
91 if (it != properties_.end()) return it->second.get();
92 return nullptr;
93 }
94
95 MSTL_NODISCARD bool has_property(const string& key) const {
96 return properties_.find(key) != properties_.end();
97 }
98
99 MSTL_NODISCARD const unordered_map<string, unique_ptr<ini_property>>& get_properties() const noexcept {
100 return properties_;
101 }
102
103 MSTL_NODISCARD string get_string(const string& key, const string& default_value = "") const {
104 const auto* prop = get_property(key);
105 return prop ? prop->get_value() : default_value;
106 }
107
108 MSTL_NODISCARD int get_int(const string& key, int default_value = 0) const {
109 const auto* prop = get_property(key);
110 return prop ? prop->get_int(default_value) : default_value;
111 }
112
113 MSTL_NODISCARD double get_double(const string& key, double default_value = 0.0) const {
114 const auto* prop = get_property(key);
115 return prop ? prop->get_double(default_value) : default_value;
116 }
117
118 MSTL_NODISCARD bool get_bool(const string& key, bool default_value = false) const {
119 const auto* prop = get_property(key);
120 return prop ? prop->get_bool(default_value) : default_value;
121 }
122};
123
124class MSTL_API ini_document final {
125private:
126 unordered_map<string, unique_ptr<ini_section>> sections_;
127 unique_ptr<ini_section> global_section_;
128
129public:
130 ini_document() : global_section_(make_unique<ini_section>("")) {}
131
132 ini_document(const ini_document&) = delete;
133 ini_document& operator =(const ini_document&) = delete;
134 ini_document(ini_document&&) = default;
135 ini_document& operator =(ini_document&&) = default;
136
137 void add_section(const string& name, unique_ptr<ini_section> section) {
138 if (name.empty()) {
139 global_section_ = _MSTL move(section);
140 } else {
141 sections_[name] = _MSTL move(section);
142 }
143 }
144
145 MSTL_NODISCARD const ini_section* get_section(const string& name) const {
146 if (name.empty()) return global_section_.get();
147 const auto it = sections_.find(name);
148 if (it != sections_.end()) return it->second.get();
149 return nullptr;
150 }
151
152 MSTL_NODISCARD ini_section* get_section(const string& name) {
153 if (name.empty()) return global_section_.get();
154 const auto it = sections_.find(name);
155 if (it != sections_.end()) return it->second.get();
156 return nullptr;
157 }
158
159 MSTL_NODISCARD bool has_section(const string& name) const {
160 if (name.empty()) return global_section_ != nullptr;
161 return sections_.find(name) != sections_.end();
162 }
163
164 MSTL_NODISCARD const unordered_map<string, unique_ptr<ini_section>>& get_sections() const noexcept {
165 return sections_;
166 }
167
168 MSTL_NODISCARD const ini_section* get_global_section() const noexcept {
169 return global_section_.get();
170 }
171
172 MSTL_NODISCARD ini_section* get_global_section() noexcept {
173 return global_section_.get();
174 }
175
176 MSTL_NODISCARD string get_string(const string& section, const string& key, const string& default_value = "") const {
177 const auto* sec = get_section(section);
178 return sec ? sec->get_string(key, default_value) : default_value;
179 }
180
181 MSTL_NODISCARD int get_int(const string& section, const string& key, int default_value = 0) const {
182 const auto* sec = get_section(section);
183 return sec ? sec->get_int(key, default_value) : default_value;
184 }
185
186 MSTL_NODISCARD double get_double(const string& section, const string& key, double default_value = 0.0) const {
187 const auto* sec = get_section(section);
188 return sec ? sec->get_double(key, default_value) : default_value;
189 }
190
191 MSTL_NODISCARD bool get_bool(const string& section, const string& key, bool default_value = false) const {
192 const auto* sec = get_section(section);
193 return sec ? sec->get_bool(key, default_value) : default_value;
194 }
195
196 MSTL_NODISCARD string to_string() const;
197};
198
200string MSTL_API ini_value_to_string(const ini_value* value);
201string MSTL_API ini_document_to_string(const ini_document* doc);
203
204MSTL_ALWAYS_INLINE_INLINE string to_string(const ini_value* value) {
205 return _INNER ini_value_to_string(value);
206}
207MSTL_ALWAYS_INLINE_INLINE string to_string(const ini_value& value) {
208 return _INNER ini_value_to_string(&value);
209}
210MSTL_ALWAYS_INLINE_INLINE string to_string(const ini_ptr& value) {
211 return _INNER ini_value_to_string(value.get());
212}
213MSTL_ALWAYS_INLINE_INLINE string to_string(const ini_document& doc) {
214 return _INNER ini_document_to_string(&doc);
215}
216
217MSTL_NODISCARD MSTL_ALWAYS_INLINE_INLINE string ini_value::to_string() const {
218 return _INNER ini_value_to_string(this);
219}
220
221MSTL_NODISCARD MSTL_ALWAYS_INLINE_INLINE string ini_value::to_document() const {
222 return _INNER ini_value_to_string(this);
223}
224
225MSTL_NODISCARD MSTL_ALWAYS_INLINE_INLINE string ini_document::to_string() const {
226 return _INNER ini_document_to_string(this);
227}
228
230#endif // MSTL_CORE_FILE_INI_INI_VALUE_HPP__
独占智能指针
MSTL_CONSTEXPR20 pointer get() const noexcept
获取原始指针
#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_CONSTEXPR20 unique_ptr< T > make_unique(Args &&... args)
创建unique_ptr
变量处理异常
MSTL独占智能指针