xtd 0.2.0
Loading...
Searching...
No Matches
yaml.hpp
Go to the documentation of this file.
1
4#pragma once
8#include "../any_object.hpp"
10#include "../string.hpp"
11#include <type_traits>
12#include <concepts>
13
15namespace xtd {
17 namespace serialization {
43 class yaml : public object {
44 public:
46
48 using boolean_type = xtd::boolean;
49 using floating_point_type = xtd::decimal;
50 using integer_type = xtd::int64;
51 using mapping_type = yaml;
53 using null_type = xtd::null_ptr;
54 using sequence_type = xtd::collections::array_list;
55 using string_type = xtd::string;
57
59
61 yaml() = default;
62 yaml(const nodes_collection& nodes);
63 yaml(nodes_collection&& nodes);
64 yaml(const yaml&) = default;
65 yaml(yaml&&) = default;
66 yaml& operator =(const yaml&) = default;
67 yaml& operator =(yaml&&) = default;
69
71
73 auto nodes() const noexcept -> const nodes_collection&;
74 auto nodes() noexcept -> nodes_collection&;
75 auto nodes(const nodes_collection& nodes) noexcept -> void;
77
79
81 template<class type_t>
82 auto as(const xtd::string& key) const -> type_t {
83 if constexpr (std::floating_point<type_t>) return xtd::as<type_t>(as_floating_point(key));
84 else if constexpr (std::integral<type_t> && !std::same_as<type_t, boolean_type>) return xtd::as<type_t>(xtd::is<integer_type>(nodes_[key]) ? xtd::as<int64>(nodes_[key]) : xtd::as<uint64>(nodes_[key]));
85 else if constexpr (std::same_as<type_t, null_type>) return nullptr;
86 else if constexpr (std::same_as<type_t, boolean_type>) return xtd::as<boolean_type>(nodes_[key]);
87 else if constexpr (std::same_as<type_t, string_type> || xtd::is_string_literal<type_t>) return xtd::as<string_type>(nodes_[key]);
88 else if constexpr (std::same_as<type_t, mapping_type>) return xtd::as<mapping_type>(nodes_[key]);
89 else if constexpr (std::same_as<type_t, sequence_type>) return xtd::as<sequence_type>(nodes_[key]);
91 }
92
93 auto as_boolean(const xtd::string& key) const -> boolean_type;
94 auto as_integer(const xtd::string& key) const {return xtd::is<integer_type>(nodes_[key]) ? xtd::as<integer_type>(nodes_[key]) : xtd::as<uint64>(nodes_[key]);}
95 auto as_floating_point(const xtd::string& key) const -> floating_point_type;
96 auto as_mapping(const xtd::string& key) const -> mapping_type;
97 auto as_null(const xtd::string& key) const -> null_type;
98 auto as_sequence(const xtd::string& key) const -> sequence_type;
99 auto as_string(const xtd::string& key) const -> string_type;
100
101 auto contains_key(const xtd::string& key) const noexcept -> bool;
102
103 template<class type_t>
104 auto is(const xtd::string& key) const -> bool {
105 if (!contains_key(key)) return false;
106 if constexpr (std::floating_point<type_t>) return is_floating_point(key) && xtd::box_floating_point<type_t>::is_valid(as_floating_point(key));
107 else if constexpr (std::integral<type_t> && !std::same_as<type_t, boolean_type>) return is_integer(key) && xtd::box_integer<type_t>::is_valid(xtd::is<integer_type>(nodes_[key]) ? xtd::as<int64>(nodes_[key]) : xtd::as<uint64>(nodes_[key]));
108 else if constexpr (std::same_as<type_t, null_type>) return contains_key(key) && xtd::is<xtd::null_ptr>(nodes_[key]);
109 else if constexpr (std::same_as<type_t, boolean_type>) return contains_key(key) && xtd::is<boolean_type>(nodes_[key]);
110 else if constexpr (std::same_as<type_t, string_type> || xtd::is_string_literal<type_t>) return contains_key(key) && xtd::is<string_type>(nodes_[key]);
111 else if constexpr (std::same_as<type_t, mapping_type>) return contains_key(key) && xtd::is<mapping_type>(nodes_[key]);
112 else if constexpr (std::same_as<type_t, sequence_type>) return contains_key(key) && xtd::is<sequence_type>(nodes_[key]);
113 else return false;
114 }
115
116 auto is_boolean(const xtd::string& key) const noexcept -> bool;
117 auto is_integer(const xtd::string& key) const noexcept -> bool;
118 auto is_floating_point(const xtd::string& key) const noexcept -> bool;
119 auto is_mapping(const xtd::string& key) const -> bool;
120 auto is_null(const xtd::string& key) const noexcept -> bool;
121 auto is_sequence(const xtd::string& key) const -> bool;
122 auto is_string(const xtd::string& key) const noexcept -> bool;
124
126
128 auto operator[](const xtd::string& key) const -> const xtd::any_object&;
129 auto operator[](const xtd::string& key) -> xtd::any_object&;
131
132 private:
133 nodes_collection nodes_;
134 };
135 }
136}
Contains xtd::any_object class.
Contains xtd::collections::array_list alias.
Represent a polymorphic wrapper capable of holding any type.
Definition any_object.hpp:29
static bool is_valid(std::floating_point auto value) noexcept
Determines whether the specified floating point value is within the range of type_t.
Definition box_floating_point.hpp:100
static bool is_valid(std::signed_integral auto value) noexcept
Determines whether the specified signed integral value is within the range of type_t.
Definition box_integer.hpp:89
Represents a collection of keys and values.
Definition dictionary.hpp:67
static auto throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current()) -> void
Throws an exption with specified exception case.
object()=default
Create a new instance of the ultimate base class object.
Represent a string literal concept.
Definition is_string_literal.hpp:26
Contains xtd::collections::generic::dictionary <key_t, value_t> class.
generic::list< xtd::any_object > array_list
Represents a collection of xtd::any_object.
Definition array_list.hpp:31
@ invalid_cast
The cast is not valid.
Definition exception_case.hpp:63
xtd::basic_string< char > string
Represents text as a sequence of UTF-8 code units.
Definition __string_definitions.hpp:43
std::int64_t int64
Represents a 64-bit signed integer.
Definition int64.hpp:23
std::nullptr_t null_ptr
Represents the null_opt alias on std::nullptr_t.
Definition null_ptr.hpp:19
bool boolean
Represents a boolean.
Definition boolean.hpp:23
long double decimal
Represents a decimal-precision floating-point number.
Definition decimal.hpp:23
auto as(any_object &o) -> type_t
Casts a type into another type.
Definition __as_any_object.hpp:60
bool is(xtd::any value)
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:484
Contains xtd::is_string_literal struct and is_string_literal_v struct.
Contains classes that can be used for serializing and deserializing objects. Serialization is the pro...
Definition yaml.hpp:17
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
Contains xtd::string alias.
Contains xtd::helpers::throw_helper class.