ECSTASY
All in the name
Loading...
Searching...
No Matches
TomlNode.hpp
Go to the documentation of this file.
1
11
12#ifndef UTIL_SERIALIZATION_TOML_TOMLNODE_HPP_
13#define UTIL_SERIALIZATION_TOML_TOMLNODE_HPP_
14
15#include "TomlConversion.hpp"
17#include <toml++/toml.h>
18
19namespace util::serialization
20{
27 template <typename N>
28 requires std::derived_from<N, toml::node> && std::copy_constructible<N>
29 class TomlNode : public ANode {
30 public:
37 TomlNode() noexcept(std::is_nothrow_default_constructible_v<N>) = default;
38
47 TomlNode(const N &node) noexcept(std::is_nothrow_copy_constructible_v<N>) : _node(node)
48 {
49 }
50
61 template <typename... Args>
62 TomlNode(Args &&...args) noexcept(std::is_nothrow_constructible_v<N, Args...>)
63 : _node(std::forward<Args &&>(args)...)
64 {
65 }
66
68 [[nodiscard]] constexpr INode::Type getType() const noexcept override final
69 {
70 if constexpr (std::same_as<toml::table, N>)
72 else if constexpr (std::same_as<toml::array, N>)
73 return INode::Type::Array;
74 else if constexpr (std::same_as<toml::value<std::string>, N>)
76 else if constexpr (std::same_as<toml::value<int64_t>, N>)
78 else if constexpr (std::same_as<toml::value<double>, N>)
79 return INode::Type::Float;
80 else if constexpr (std::same_as<toml::value<bool>, N>)
82 else if constexpr (std::same_as<toml::value<toml::date>, N>)
83 return INode::Type::Date;
84 else if constexpr (std::same_as<toml::value<toml::time>, N>)
85 return INode::Type::Time;
86 else if constexpr (std::same_as<toml::value<toml::date_time>, N>)
88 else
90 }
91
93 [[nodiscard]] std::optional<std::string_view> tryAsString() const noexcept override final
94 {
95 if constexpr (std::same_as<toml::value<std::string>, N>)
96 return _node.get();
97 else
99 }
100
102 [[nodiscard]] std::optional<int64_t> tryAsInteger() const noexcept override final
103 {
104 if constexpr (std::same_as<toml::value<int64_t>, N>)
105 return _node.get();
106 else
107 return std::optional<int64_t>();
108 }
109
111 [[nodiscard]] std::optional<double> tryAsFloat() const noexcept override final
112 {
113 if constexpr (std::same_as<toml::value<double>, N>)
114 return _node.get();
115 else
116 return std::optional<double>();
117 }
118
120 [[nodiscard]] std::optional<bool> tryAsBoolean() const noexcept override final
121 {
122 if constexpr (std::same_as<toml::value<bool>, N>)
123 return _node.get();
124 else
125 return std::optional<bool>();
126 }
127
129 [[nodiscard]] std::optional<Date> tryAsDate() const noexcept override final
130 {
131 if constexpr (std::same_as<toml::value<toml::date>, N>)
132 return TomlConversion::fromToml(_node.get());
133 else
134 return std::optional<Date>();
135 }
136
138 [[nodiscard]] std::optional<Time> tryAsTime() const noexcept override final
139 {
140 if constexpr (std::same_as<toml::value<toml::time>, N>)
141 return TomlConversion::fromToml(_node.get());
142 else
143 return std::optional<Time>();
144 }
145
147 [[nodiscard]] std::optional<DateTime> tryAsDateTime() const noexcept override final
148 {
149 if constexpr (std::same_as<toml::value<toml::date_time>, N>)
150 return TomlConversion::fromToml(_node.get());
151 else
153 }
154
155 protected:
158 };
159
160} // namespace util::serialization
161
162#endif /* !UTIL_SERIALIZATION_TOML_TOMLNODE_HPP_ */
Abstract node partial implementation.
Toml conversion interface.
Serialization node.
Definition ANode.hpp:25
Type
Available node types.
Definition INode.hpp:42
@ Float
Floating point number node.
@ Boolean
Boolean value node.
@ Array
Node containing multiple unnamed nodes. (similar to a std::vector<string>)
@ Object
Node containing multiple named nodes. (similar to a std::map<string, INode>)
@ DateTime
DateTime node. (Timestamp)
@ Integer
Integer number node.
static INode::Date fromToml(const toml::date &date) noexcept
Convert a toml::date to a INode::Date.
Serialization node.
Definition TomlNode.hpp:29
std::optional< DateTime > tryAsDateTime() const noexcept override final
Try to get the node datetime value.
Definition TomlNode.hpp:147
TomlNode() noexcept(std::is_nothrow_default_constructible_v< N >)=default
Default constructor.
std::optional< Date > tryAsDate() const noexcept override final
Try to get the node date value.
Definition TomlNode.hpp:129
TomlNode(Args &&...args) noexcept(std::is_nothrow_constructible_v< N, Args... >)
Construct a new Toml Node from a node data.
Definition TomlNode.hpp:62
std::optional< int64_t > tryAsInteger() const noexcept override final
Try to get the node integer value.
Definition TomlNode.hpp:102
constexpr INode::Type getType() const noexcept override final
Types check ///.
Definition TomlNode.hpp:68
std::optional< bool > tryAsBoolean() const noexcept override final
Try to get the node boolean value.
Definition TomlNode.hpp:120
std::optional< Time > tryAsTime() const noexcept override final
Try to get the node time value.
Definition TomlNode.hpp:138
std::optional< std::string_view > tryAsString() const noexcept override final
Try to get the node string value.
Definition TomlNode.hpp:93
std::optional< double > tryAsFloat() const noexcept override final
Try to get the node float value.
Definition TomlNode.hpp:111
T is_nothrow_constructible_v