ECSTASY
All in the name
Loading...
Searching...
No Matches
ANode.cpp
Go to the documentation of this file.
1
11
12#include "ANode.hpp"
13#include "IArrayNode.hpp"
14#include "IObjectNode.hpp"
15
17{
18 bool ANode::isType(Type type) const noexcept
19 {
20 return getType() == type;
21 }
22
23 bool ANode::isNull() const noexcept
24 {
25 return isType(Type::Null);
26 }
27
28 bool ANode::isObject() const noexcept
29 {
30 return isType(Type::Object);
31 }
32
33 bool ANode::isArray() const noexcept
34 {
35 return isType(Type::Array);
36 }
37
38 bool ANode::isString() const noexcept
39 {
40 return isType(Type::String);
41 }
42
43 bool ANode::isInteger() const noexcept
44 {
45 return isType(Type::Integer);
46 }
47
48 bool ANode::isFloat() const noexcept
49 {
50 return isType(Type::Float);
51 }
52
53 bool ANode::isBoolean() const noexcept
54 {
55 return isType(Type::Boolean);
56 }
57
58 bool ANode::isDate() const noexcept
59 {
60 return isType(Type::Date);
61 }
62
63 bool ANode::isTime() const noexcept
64 {
65 return isType(Type::Time);
66 }
67
68 bool ANode::isDateTime() const noexcept
69 {
70 return isType(Type::DateTime);
71 }
72
74 {
75 return const_cast<ANode &>(*this).asObject();
76 }
77
79 {
80 if (!isObject()) [[unlikely]]
81 throw std::runtime_error("Node is not an Object Node.");
82 return dynamic_cast<IObjectNode &>(*this);
83 }
84
86 {
87 if (isObject())
88 return dynamic_cast<const IObjectNode &>(*this);
90 }
91
93 {
94 if (isObject())
95 return dynamic_cast<IObjectNode &>(*this);
97 }
98
100 {
101 return const_cast<ANode &>(*this).asArray();
102 }
103
105 {
106 if (!isArray()) [[unlikely]]
107 throw std::runtime_error("Node is not an Array Node.");
108 return dynamic_cast<IArrayNode &>(*this);
109 }
110
112 {
113 if (isArray())
114 return dynamic_cast<const IArrayNode &>(*this);
116 }
117
119 {
120 if (isArray())
121 return dynamic_cast<IArrayNode &>(*this);
123 }
124
126 {
127 return tryAsString().value();
128 }
129
130 int64_t ANode::asInteger() const
131 {
132 return tryAsInteger().value();
133 }
134
135 double ANode::asFloat() const
136 {
137 return tryAsFloat().value();
138 }
139
140 bool ANode::asBoolean() const
141 {
142 return tryAsBoolean().value();
143 }
144
146 {
147 return tryAsDate().value();
148 }
149
151 {
152 return tryAsTime().value();
153 }
154
156 {
157 return tryAsDateTime().value();
158 }
159
160} // namespace util::serialization
Abstract node partial implementation.
Array node interface.
Object node interface.
Serialization node.
Definition ANode.hpp:25
int64_t asInteger() const override final
Get the integer node value.
Definition ANode.cpp:130
bool isNull() const noexcept override final
Check if the node type is Type::Null.
Definition ANode.cpp:23
Date asDate() const override final
Get the date node value.
Definition ANode.cpp:145
const IObjectNode & asObject() const override final
Get the object node value.
Definition ANode.cpp:73
bool isTime() const noexcept override final
Check if the node type is Type::Time.
Definition ANode.cpp:63
bool isBoolean() const noexcept override final
Check if the node type is Type::Boolean.
Definition ANode.cpp:53
bool isFloat() const noexcept override final
Check if the node type is Type::Float.
Definition ANode.cpp:48
bool asBoolean() const override final
Get the boolean node value.
Definition ANode.cpp:140
bool isDateTime() const noexcept override final
Check if the node type is Type::DateTime.
Definition ANode.cpp:68
std::optional< std::reference_wrapper< const IObjectNode > > tryAsObject() const noexcept override final
Try to get the node object value.
Definition ANode.cpp:85
bool isInteger() const noexcept override final
Check if the node type is Type::Integer.
Definition ANode.cpp:43
std::string_view asString() const override final
Get the string node value.
Definition ANode.cpp:125
const IArrayNode & asArray() const override final
Get the array node value.
Definition ANode.cpp:99
Time asTime() const override final
Get the time node value.
Definition ANode.cpp:150
DateTime asDateTime() const override final
Get the datetime node value.
Definition ANode.cpp:155
bool isObject() const noexcept override final
Check if the node type is Type::Object.
Definition ANode.cpp:28
bool isArray() const noexcept override final
Check if the node type is Type::Array.
Definition ANode.cpp:33
std::optional< std::reference_wrapper< const IArrayNode > > tryAsArray() const noexcept override final
Try to get the node array value.
Definition ANode.cpp:111
double asFloat() const override final
Get the float node value.
Definition ANode.cpp:135
bool isType(Type type) const noexcept override final
Check if the node type is type.
Definition ANode.cpp:18
bool isDate() const noexcept override final
Check if the node type is Type::Date.
Definition ANode.cpp:58
bool isString() const noexcept override final
Check if the node type is Type::String.
Definition ANode.cpp:38
virtual std::optional< Time > tryAsTime() const noexcept=0
Try to get the node time value.
virtual std::optional< double > tryAsFloat() const noexcept=0
Try to get the node float value.
std::chrono::high_resolution_clock::time_point DateTime
Type::DateTime underlying type.
Definition INode.hpp:64
virtual std::optional< std::string_view > tryAsString() const noexcept=0
Try to get the node string value.
virtual std::optional< DateTime > tryAsDateTime() const noexcept=0
Try to get the node datetime value.
virtual std::optional< int64_t > tryAsInteger() const noexcept=0
Try to get the node integer value.
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.
@ Null
Null node (none/null).
virtual std::optional< bool > tryAsBoolean() const noexcept=0
Try to get the node boolean value.
virtual std::optional< Date > tryAsDate() const noexcept=0
Try to get the node date value.