ECSTASY
All in the name
Loading...
Searching...
No Matches
TomlNodeFactory.cpp
Go to the documentation of this file.
1
11
12#include "TomlNodeFactory.hpp"
13#include "TomlArrayNode.hpp"
14#include "TomlObjectNode.hpp"
15
16namespace util::serialization
17{
19 {
20 static TomlNodeFactory instance;
21
22 return instance;
23 }
24
26 {
27 switch (node.type()) {
28 case toml::node_type::table: return std::make_shared<TomlObjectNode>(*node.as_table());
29 case toml::node_type::array: return std::make_shared<TomlArrayNode>(*node.as_array());
30 case toml::node_type::string:
31 return std::make_shared<TomlNode<toml::value<std::string>>>(*node.as_string());
32 case toml::node_type::integer: return std::make_shared<TomlNode<toml::value<int64_t>>>(*node.as_integer());
33 case toml::node_type::floating_point:
34 return std::make_shared<TomlNode<toml::value<double>>>(*node.as_floating_point());
35 case toml::node_type::boolean: return std::make_shared<TomlNode<toml::value<bool>>>(*node.as_boolean());
36 case toml::node_type::date: return std::make_shared<TomlNode<toml::value<toml::date>>>(*node.as_date());
37 case toml::node_type::time: return std::make_shared<TomlNode<toml::value<toml::time>>>(*node.as_time());
38 case toml::node_type::date_time:
39 return std::make_shared<TomlNode<toml::value<toml::date_time>>>(*node.as_date_time());
40 default: return nullptr;
41 }
42 }
43
45 {
46 switch (type) {
47 case INode::Type::Object: return std::make_shared<TomlObjectNode>();
48 case INode::Type::Array: return std::make_shared<TomlArrayNode>();
49 case INode::Type::String: return std::make_shared<TomlNode<toml::value<std::string>>>();
50 case INode::Type::Integer: return std::make_shared<TomlNode<toml::value<int64_t>>>();
51 case INode::Type::Float: return std::make_shared<TomlNode<toml::value<double>>>();
52 case INode::Type::Boolean: return std::make_shared<TomlNode<toml::value<bool>>>();
54 case INode::Type::Date: return std::make_shared<TomlNode<toml::value<toml::date>>>(0, 0, 0);
55 case INode::Type::Time: return std::make_shared<TomlNode<toml::value<toml::time>>>(0, 0, 0, 0);
58 return std::make_shared<TomlNode<toml::value<toml::date_time>>>(
59 toml::date(1970, 1, 1), toml::time(0, 0, 0, 0));
60 default: return nullptr;
61 }
62 }
63
65 {
66 switch (node.getType()) {
67 case INode::Type::Object: return createObject(node.asObject());
68 case INode::Type::Array: return createArray(node.asArray());
69 case INode::Type::String: return create(node.asString());
70 case INode::Type::Integer: return create(node.asInteger());
71 case INode::Type::Float: return create(node.asFloat());
72 case INode::Type::Boolean: return create(node.asBoolean());
73 case INode::Type::Date: return create(node.asDate());
74 case INode::Type::Time: return create(node.asTime());
75 case INode::Type::DateTime: return create(node.asDateTime());
76 default: return nullptr;
77 }
78 }
79
81 {
82 return std::make_shared<TomlNode<toml::value<std::string>>>(string);
83 }
84
86 {
87 return std::make_shared<TomlNode<toml::value<int64_t>>>(integer);
88 }
89
90 NodePtr TomlNodeFactory::create(double floatingPoint)
91 {
92 return std::make_shared<TomlNode<toml::value<double>>>(floatingPoint);
93 }
94
96 {
97 return std::make_shared<TomlNode<toml::value<bool>>>(boolean);
98 }
99
101 {
102 return std::make_shared<TomlNode<toml::value<toml::date>>>(TomlConversion::toToml(date));
103 }
104
106 {
107 return std::make_shared<TomlNode<toml::value<toml::time>>>(TomlConversion::toToml(time));
108 }
109
111 {
112 return std::make_shared<TomlNode<toml::value<toml::date_time>>>(TomlConversion::toToml(dateTime));
113 }
114
116 {
117 NodePtr res = std::make_shared<TomlArrayNode>();
118
119 for (auto it : array)
120 res->asArray().pushBack(*it.lock());
121 return res;
122 }
123
125 {
126 NodePtr res = std::make_shared<TomlObjectNode>();
127
128 for (auto it : object)
129 res->asObject().insert(it.first, *it.second.lock());
130 return res;
131 }
132} // namespace util::serialization
Toml array node.
Toml node factory declaration.
Toml object node declaration.
virtual node_type type() const noexcept=0
virtual toml::value< time > * as_time() noexcept=0
virtual array * as_array() noexcept=0
virtual toml::value< double > * as_floating_point() noexcept=0
virtual toml::value< std::string > * as_string() noexcept=0
virtual toml::value< int64_t > * as_integer() noexcept=0
virtual table * as_table() noexcept=0
virtual toml::value< date > * as_date() noexcept=0
virtual toml::value< date_time > * as_date_time() noexcept=0
virtual toml::value< bool > * as_boolean() noexcept=0
Serialization node.
Definition INode.hpp:32
virtual const IArrayNode & asArray() const =0
Get the array node value.
virtual Time asTime() const =0
Get the time node value.
virtual const IObjectNode & asObject() const =0
Value getter ///.
virtual Type getType() const noexcept=0
Types check ///.
virtual std::string_view asString() const =0
Get the string node value.
std::chrono::high_resolution_clock::time_point DateTime
Type::DateTime underlying type.
Definition INode.hpp:64
virtual int64_t asInteger() const =0
Get the integer node value.
virtual DateTime asDateTime() const =0
Get the datetime node value.
virtual double asFloat() const =0
Get the float node 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.
virtual Date asDate() const =0
Get the date node value.
virtual bool asBoolean() const =0
Get the boolean node value.
static toml::date toToml(const INode::Date &date) noexcept
Convert a INode::Date to a toml::date.
Toml Node factory singleton.
static TomlNodeFactory & get() noexcept
Retrieve the TomlNodeFactory singleton instance.
NodePtr createFromToml(const toml::node &node)
Create a node from a toml::node object.
NodePtr createArray(const IArrayNode &array) override final
Construct a INode::Type::Array node.
NodePtr createObject(const IObjectNode &object) override final
Construct a INode::Type::Object node.
NodePtr create(INode::Type type) override final
Construct an empty node from its type.