StormByte C++ Library: Config module 0.0.9999
StormByte-Config is a StormByte library module for parsing configuration files
Loading...
Searching...
No Matches
value.hxx
1#pragma once
2
3#include <StormByte/config/item/base.hxx>
4
9namespace StormByte::Config::Item {
15 template<AllowedValueType T>
16 class STORMBYTE_CONFIG_PUBLIC Value: public Base {
17 public:
22 Value(const T& value):Base(), m_value(value) {}
23
24
25 template <typename U = T>
26 Value(const char* value) requires std::is_same_v<U, std::string>
27 : Base(), m_value(std::string(value)) {}
28
29
30 template <typename U = T>
31 Value(const char* name, const char* value) requires std::is_same_v<U, std::string>
32 : Base(std::string(name)), m_value(std::string(value)) {}
33
38 Value(T&& value):Base(), m_value(std::move(value)) {}
39
45 Value(const std::string& name, const T& value):Base(name), m_value(value) {}
46
52 Value(std::string&& name, T&& value):Base(std::move(name)), m_value(std::move(value)) {}
53
59 Value(const std::string& name, const char* value) requires std::is_same_v<T, std::string>
60 : Base(name), m_value(std::string(value)) {}
61
67 Value(std::string&& name, const char* value) requires std::is_same_v<T, std::string>
68 : Base(std::move(name)), m_value(std::string(value)) {}
69
74 Value(const Value& single) = default;
75
80 Value(Value&& single) noexcept = default;
81
86 Value& operator=(const Value& single) = default;
87
92 Value& operator=(Value&& single) noexcept = default;
93
97 virtual ~Value() noexcept override = default;
98
103 constexpr virtual Item::Type Type() const noexcept override {
104 if constexpr (std::is_same_v<T, std::string>) {
105 return Item::Type::String;
106 } else if constexpr (std::is_same_v<T, int>) {
107 return Item::Type::Integer;
108 } else if constexpr (std::is_same_v<T, double>) {
109 return Item::Type::Double;
110 } else if constexpr (std::is_same_v<T, bool>)
111 return Item::Type::Bool;
112 }
113
119 bool operator==(const Value<T>& single) const noexcept {
120 // Compare the Base class
121 if (Base::operator!=(single)) return false;
122
123 // Compare the Type
124 if constexpr (!std::is_same_v<T, decltype(m_value)>) {
125 return false;
126 }
127
128 // Compare the m_value
129 return m_value == single.m_value;
130 }
131
137 bool operator!=(const Value<T>& single) const noexcept {
138 return !operator==(single);
139 }
140
145 T& operator*() noexcept {
146 return m_value;
147 }
148
153 const T& operator*() const noexcept {
154 return m_value;
155 }
156
162 std::string Serialize(const int& indent_level) const noexcept override;
163
168 virtual PointerType Clone() const override {
169 return MakePointer<Value<T>>(*this);
170 }
171
176 virtual PointerType Move() override {
177 return MakePointer<Value<T>>(std::move(*this));
178 }
179
180 protected:
182 };
183
184 // Deduction guides
185 Value(const char*) -> Value<std::string>;
186 Value(const char*, const char*) -> Value<std::string>;
187}
The base class for all configuration items.
Definition base.hxx:29
Represents a configuration item with a value.
Definition value.hxx:16
const T & operator*() const noexcept
Definition value.hxx:153
virtual PointerType Move() override
Definition value.hxx:176
Value(const std::string &name, const T &value)
Definition value.hxx:45
T & operator*() noexcept
Definition value.hxx:145
virtual ~Value() noexcept override=default
Value(const std::string &name, const char *value)
Definition value.hxx:59
Value(std::string &&name, const char *value)
Definition value.hxx:67
Value(T &&value)
Definition value.hxx:38
Value(const Value &single)=default
T m_value
The value of the item.
Definition value.hxx:181
Value(Value &&single) noexcept=default
Value & operator=(Value &&single) noexcept=default
Value(const T &value)
Constructs a Value with the given value.
Definition value.hxx:22
bool operator==(const Value< T > &single) const noexcept
Checks if two Value objects are equal.
Definition value.hxx:119
virtual PointerType Clone() const override
Definition value.hxx:168
Value(std::string &&name, T &&value)
Definition value.hxx:52
Value & operator=(const Value &single)=default
std::string Serialize(const int &indent_level) const noexcept override
Serializes the item to a string.
bool operator!=(const Value< T > &single) const noexcept
Definition value.hxx:137
All the configuration item classes namespace.