NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
yaml_value.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_FILE_YAML_YAML_VALUE_HPP__
2#define NEFORCE_CORE_FILE_YAML_YAML_VALUE_HPP__
3
11
17NEFORCE_BEGIN_NAMESPACE__
18
24
29struct yaml_exception final : value_exception {
30 explicit yaml_exception(const char* info = "YAML Operation Failed.", const char* type = static_type,
31 const int code = 0) noexcept :
32 value_exception(info, type, code) {}
33
34 explicit yaml_exception(const exception& e) :
35 value_exception(e) {}
36
37 ~yaml_exception() override = default;
38 static constexpr auto static_type = "yaml_exception";
39};
40 // Exceptions
42
47
191
192class yaml_value;
193class yaml_null;
194class yaml_boolean;
195class yaml_integer;
196class yaml_float;
197class yaml_string;
198class yaml_timestamp;
199class yaml_sequence;
200class yaml_mapping;
201
202
216class NEFORCE_API yaml_value : public istringify<yaml_value> {
217public:
218 string anchor;
219 string tag;
220
235
236 yaml_value() = default;
237 virtual ~yaml_value() = default;
238
239 yaml_value(const yaml_value&) = default;
240 yaml_value& operator=(const yaml_value&) = default;
241
242 yaml_value(yaml_value&&) noexcept = default;
243 yaml_value& operator=(yaml_value&&) noexcept = default;
244
249 NEFORCE_NODISCARD virtual types type() const noexcept = 0;
250
254 NEFORCE_NODISCARD virtual const yaml_null* as_null() const noexcept { return nullptr; }
255 NEFORCE_NODISCARD virtual const yaml_boolean* as_boolean() const noexcept { return nullptr; }
256 NEFORCE_NODISCARD virtual const yaml_integer* as_integer() const noexcept { return nullptr; }
257 NEFORCE_NODISCARD virtual const yaml_float* as_float() const noexcept { return nullptr; }
258 NEFORCE_NODISCARD virtual const yaml_string* as_string() const noexcept { return nullptr; }
259 NEFORCE_NODISCARD virtual const yaml_timestamp* as_timestamp() const noexcept { return nullptr; }
260 NEFORCE_NODISCARD virtual const yaml_sequence* as_sequence() const noexcept { return nullptr; }
261 NEFORCE_NODISCARD virtual const yaml_mapping* as_mapping() const noexcept { return nullptr; }
263
267 NEFORCE_NODISCARD bool is_null() const noexcept { return type() == Null; }
268 NEFORCE_NODISCARD bool is_boolean() const noexcept { return type() == Boolean; }
269 NEFORCE_NODISCARD bool is_integer() const noexcept { return type() == Integer; }
270 NEFORCE_NODISCARD bool is_float() const noexcept { return type() == Float; }
271 NEFORCE_NODISCARD bool is_string() const noexcept { return type() == String; }
272 NEFORCE_NODISCARD bool is_timestamp() const noexcept { return type() == Timestamp; }
273 NEFORCE_NODISCARD bool is_sequence() const noexcept { return type() == Sequence; }
274 NEFORCE_NODISCARD bool is_mapping() const noexcept { return type() == Mapping; }
276
281 void set_anchor(const string& a) { this->anchor = a; }
282
287 void set_tag(const string& t) { this->tag = t; }
288
293 NEFORCE_NODISCARD string to_string() const;
294
299 NEFORCE_NODISCARD string to_document() const;
300};
301
309class NEFORCE_API yaml_null final : public yaml_value {
310public:
311 yaml_null() = default;
312 ~yaml_null() override = default;
313
314 yaml_null(const yaml_null&) = default;
315 yaml_null& operator=(const yaml_null&) = default;
316 yaml_null(yaml_null&&) noexcept = default;
317 yaml_null& operator=(yaml_null&&) noexcept = default;
318
319 NEFORCE_NODISCARD types type() const noexcept override { return Null; }
320 NEFORCE_NODISCARD const yaml_null* as_null() const noexcept override { return this; }
321};
322
331class NEFORCE_API yaml_boolean final : public yaml_value {
332private:
333 bool value;
334
335public:
336 ~yaml_boolean() override = default;
337
338 yaml_boolean(const yaml_boolean&) = default;
339 yaml_boolean& operator=(const yaml_boolean&) = default;
340 yaml_boolean(yaml_boolean&&) noexcept = default;
341 yaml_boolean& operator=(yaml_boolean&&) noexcept = default;
342
347 explicit yaml_boolean(const bool v) :
348 value(v) {}
349
350 NEFORCE_NODISCARD types type() const noexcept override { return Boolean; }
351 NEFORCE_NODISCARD const yaml_boolean* as_boolean() const noexcept override { return this; }
352
357 NEFORCE_NODISCARD bool get_value() const noexcept { return value; }
358};
359
372class NEFORCE_API yaml_integer final : public yaml_value {
373private:
374 int64_t value;
375
376public:
377 ~yaml_integer() override = default;
378
379 yaml_integer(const yaml_integer&) = default;
380 yaml_integer& operator=(const yaml_integer&) = default;
381 yaml_integer(yaml_integer&&) noexcept = default;
382 yaml_integer& operator=(yaml_integer&&) noexcept = default;
383
388 explicit yaml_integer(const int64_t v) :
389 value(v) {}
390
391 NEFORCE_NODISCARD types type() const noexcept override { return Integer; }
392 NEFORCE_NODISCARD const yaml_integer* as_integer() const noexcept override { return this; }
393
398 NEFORCE_NODISCARD int64_t get_value() const noexcept { return value; }
399};
400
412class NEFORCE_API yaml_float final : public yaml_value {
413private:
414 double value;
415
416public:
417 ~yaml_float() override = default;
418
419 yaml_float(const yaml_float&) = default;
420 yaml_float& operator=(const yaml_float&) = default;
421 yaml_float(yaml_float&&) noexcept = default;
422 yaml_float& operator=(yaml_float&&) noexcept = default;
423
428 explicit yaml_float(const double v) :
429 value(v) {}
430
431 NEFORCE_NODISCARD types type() const noexcept override { return Float; }
432 NEFORCE_NODISCARD const yaml_float* as_float() const noexcept override { return this; }
433
438 NEFORCE_NODISCARD double get_value() const noexcept { return value; }
439};
440
455class NEFORCE_API yaml_string final : public yaml_value {
456public:
468
469private:
470 string value;
471 string_style style;
472
473public:
474 ~yaml_string() override = default;
475
476 yaml_string(const yaml_string&) = default;
477 yaml_string& operator=(const yaml_string&) = default;
478 yaml_string(yaml_string&&) noexcept = default;
479 yaml_string& operator=(yaml_string&&) noexcept = default;
480
486 explicit yaml_string(string v, const string_style s = Plain) :
487 value(_NEFORCE move(v)),
488 style(s) {}
489
490 NEFORCE_NODISCARD types type() const noexcept override { return String; }
491 NEFORCE_NODISCARD const yaml_string* as_string() const noexcept override { return this; }
492
497 NEFORCE_NODISCARD const string& get_value() const noexcept { return value; }
498
503 NEFORCE_NODISCARD string_style get_style() const noexcept { return style; }
504};
505
520class NEFORCE_API yaml_timestamp final : public yaml_value {
521private:
522 datetime value;
523
524public:
525 ~yaml_timestamp() override = default;
526
527 yaml_timestamp(const yaml_timestamp&) = default;
528 yaml_timestamp& operator=(const yaml_timestamp&) = default;
529 yaml_timestamp(yaml_timestamp&&) noexcept = default;
530 yaml_timestamp& operator=(yaml_timestamp&&) noexcept = default;
531
537 explicit yaml_timestamp(const string_view v) {
538 datetime dt;
539 if (dt.try_parse_RFC3339(v) || dt.try_parse_ISO8601(v)) {
540 value = dt;
541 } else {
542 NEFORCE_THROW_EXCEPTION(yaml_exception(("Invalid timestamp format: " + string(v)).data()));
543 }
544 }
545
550 explicit yaml_timestamp(const datetime& dt) :
551 value(dt) {}
552
553 NEFORCE_NODISCARD types type() const noexcept override { return Timestamp; }
554 NEFORCE_NODISCARD const yaml_timestamp* as_timestamp() const noexcept override { return this; }
555
560 NEFORCE_NODISCARD const datetime& get_value() const noexcept { return value; }
561
566 NEFORCE_NODISCARD string get_string_value() const { return value.to_RFC3339(); }
567};
568
581class NEFORCE_API yaml_sequence final : public yaml_value {
582public:
591
592private:
594 sequence_style style;
595
596public:
597 ~yaml_sequence() override = default;
598
603 explicit yaml_sequence(const sequence_style s = Block) :
604 style(s) {}
605
606 yaml_sequence(const yaml_sequence&) = delete;
607 yaml_sequence& operator=(const yaml_sequence&) = delete;
608 yaml_sequence(yaml_sequence&&) noexcept = default;
609 yaml_sequence& operator=(yaml_sequence&&) noexcept = default;
610
611 NEFORCE_NODISCARD types type() const noexcept override { return Sequence; }
612 NEFORCE_NODISCARD const yaml_sequence* as_sequence() const noexcept override { return this; }
613
618 void add_element(shared_ptr<yaml_value> value) { elements.emplace_back(_NEFORCE move(value)); }
619
625 NEFORCE_NODISCARD const yaml_value* get_element(const size_t index) const noexcept {
626 if (index < elements.size()) {
627 return elements[index].get();
628 }
629 return nullptr;
630 }
631
637 NEFORCE_NODISCARD yaml_value* get_element(const size_t index) noexcept {
638 if (index < elements.size()) {
639 return elements[index].get();
640 }
641 return nullptr;
642 }
643
648 NEFORCE_NODISCARD size_t size() const noexcept { return elements.size(); }
649
654 NEFORCE_NODISCARD const vector<shared_ptr<yaml_value>>& get_elements() const noexcept { return elements; }
655
660 NEFORCE_NODISCARD sequence_style get_style() const noexcept { return style; }
661
666 void set_style(const sequence_style s) noexcept { style = s; }
667};
668
683class NEFORCE_API yaml_mapping final : public yaml_value {
684public:
693
694private:
696 mapping_style style;
697
698public:
699 ~yaml_mapping() override = default;
700
705 explicit yaml_mapping(const mapping_style s = Block) :
706 style(s) {}
707
708 yaml_mapping(const yaml_mapping&) = delete;
709 yaml_mapping& operator=(const yaml_mapping&) = delete;
710 yaml_mapping(yaml_mapping&&) noexcept = default;
711 yaml_mapping& operator=(yaml_mapping&&) noexcept = default;
712
713 NEFORCE_NODISCARD types type() const noexcept override { return Mapping; }
714 NEFORCE_NODISCARD const yaml_mapping* as_mapping() const noexcept override { return this; }
715
721 void add_member(const string& key, shared_ptr<yaml_value> value) { members[key] = _NEFORCE move(value); }
722
728 NEFORCE_NODISCARD const yaml_value* get_member(const string& key) const {
729 const auto it = members.find(key);
730 if (it != members.end()) {
731 return it->second.get();
732 }
733 return nullptr;
734 }
735
741 NEFORCE_NODISCARD yaml_value* get_member(const string& key) {
742 const auto it = members.find(key);
743 if (it != members.end()) {
744 return it->second.get();
745 }
746 return nullptr;
747 }
748
754 NEFORCE_NODISCARD bool has_member(const string& key) const { return members.find(key) != members.end(); }
755
760 NEFORCE_NODISCARD const unordered_map<string, shared_ptr<yaml_value>>& get_members() const noexcept {
761 return members;
762 }
763
768 NEFORCE_NODISCARD mapping_style get_style() const noexcept { return style; }
769
774 void set_style(const mapping_style s) noexcept { style = s; }
775
783 void merge_from(const yaml_mapping* other) {
784 if (other == nullptr) {
785 return;
786 }
787 for (const auto& pair: other->get_members()) {
788 if (members.find(pair.first) == members.end()) {
789 members[pair.first] = pair.second;
790 }
791 }
792 }
793};
794 // YamlConfig
796 // ConfigFormat
798
799NEFORCE_END_NAMESPACE__
800#endif // NEFORCE_CORE_FILE_YAML_YAML_VALUE_HPP__
日期时间类
constexpr bool try_parse_ISO8601(const string_view view) noexcept
尝试解析 ISO 8601
constexpr bool try_parse_RFC3339(const string_view view) noexcept
尝试解析 RFC 3339
共享智能指针类模板
无序映射容器
动态大小数组容器
YAML布尔值类型
bool get_value() const noexcept
获取布尔值
yaml_boolean(const bool v)
构造布尔值
types type() const noexcept override
获取值的具体类型
YAML浮点数值类型
yaml_float(const double v)
构造浮点数值
types type() const noexcept override
获取值的具体类型
double get_value() const noexcept
获取浮点数值
YAML整数值类型
types type() const noexcept override
获取值的具体类型
int64_t get_value() const noexcept
获取整数值
yaml_integer(const int64_t v)
构造整数值
YAML映射值类型(字典/对象)
yaml_value * get_member(const string &key)
获取指定键名的成员(可变版本)
const yaml_value * get_member(const string &key) const
获取指定键名的成员(常量版本)
mapping_style
映射集合样式枚举
@ Flow
流样式({key: value})
@ Block
块样式(key: value)
mapping_style get_style() const noexcept
获取集合样式
void merge_from(const yaml_mapping *other)
合并另一个映射的成员
void set_style(const mapping_style s) noexcept
设置集合样式
bool has_member(const string &key) const
检查键名是否存在
const unordered_map< string, shared_ptr< yaml_value > > & get_members() const noexcept
获取所有成员的常量引用
types type() const noexcept override
获取值的具体类型
void add_member(const string &key, shared_ptr< yaml_value > value)
添加或覆盖键值对
yaml_mapping(const mapping_style s=Block)
构造映射
YAML空值类型
types type() const noexcept override
获取值的具体类型
YAML序列值类型(数组)
types type() const noexcept override
获取值的具体类型
const vector< shared_ptr< yaml_value > > & get_elements() const noexcept
获取所有元素的常量引用
const yaml_value * get_element(const size_t index) const noexcept
获取指定索引的元素(常量版本)
void add_element(shared_ptr< yaml_value > value)
添加元素到序列末尾
yaml_value * get_element(const size_t index) noexcept
获取指定索引的元素(可变版本)
sequence_style get_style() const noexcept
获取集合样式
void set_style(const sequence_style s) noexcept
设置集合样式
sequence_style
序列集合样式枚举
@ Block
块样式(- item)
@ Flow
流样式([item, ...])
size_t size() const noexcept
获取序列大小
yaml_sequence(const sequence_style s=Block)
构造序列
YAML字符串值类型
string_style
字符串标量样式枚举
@ Folded
块折叠样式(>,换行转空格)
@ SingleQuoted
单引号样式('string')
@ Plain
纯文本样式(无引号)
@ DoubleQuoted
双引号样式("string",支持转义)
@ Literal
块字面量样式(|,保留换行)
types type() const noexcept override
获取值的具体类型
const string & get_value() const noexcept
获取字符串内容
string_style get_style() const noexcept
获取标量样式
YAML时间戳值类型
string get_string_value() const
获取 RFC 3339 格式的字符串表示
const datetime & get_value() const noexcept
获取日期时间值
types type() const noexcept override
获取值的具体类型
yaml_timestamp(const string_view v)
从字符串构造时间戳(自动检测格式)
yaml_timestamp(const datetime &dt)
从 datetime 对象构造时间戳
YAML值的抽象基类
string tag
类型标签(YAML !tag 语法),空字符串表示无标签
types
YAML值类型枚举
@ String
Unicode字符串类型
@ Integer
64位有符号整数类型
@ Sequence
有序值列表类型
@ Float
双精度浮点数类型
@ Boolean
布尔值类型
@ Mapping
键值对集合类型
@ Timestamp
ISO 8601 / RFC 3339 时间戳类型
@ Null
空值类型
void set_anchor(const string &a)
设置锚点名
string to_string() const
紧凑单行序列化
void set_tag(const string &t)
设置类型标签
virtual types type() const noexcept=0
获取值的具体类型
string to_document() const
格式化文档序列化
string anchor
锚点名(YAML &anchor 语法),空字符串表示无锚点
日期时间处理库
long long int64_t
64位有符号整数类型
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
basic_string_view< char > string_view
字符字符串视图
constexpr decltype(auto) data(Container &cont) noexcept(noexcept(cont.data()))
获取容器的底层数据指针
可字符串化接口
共享智能指针实现
exception(const char *info=static_type, const char *type=static_type, const int code=0)
构造函数
const char * type() const noexcept
获取异常类型
int code() const noexcept
获取异常码
可字符串化接口
存储两个值的元组对
T2 second
第二个元素
T1 first
第一个元素
YAML格式操作失败
无序映射容器
动态大小数组容器