StormByte C++ Library: Config module 1.0.1.9999
StormByte-Config is the configuration module of the StormByte C++ suite.
Loading...
Searching...
No Matches
value.hxx
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This file is part of StormByte-Config.
5 *
6 * StormByte-Config is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License version 3
8 * or later, as published by the Free Software Foundation.
9 *
10 * StormByte-Config is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with StormByte-Config. If not, see
17 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
18 */
19
20#pragma once
21
22#include <StormByte/config/item/base.hxx>
23
24#include <cstddef>
25#include <vector>
26
36 template<AllowedValueType T>
37 class STORMBYTE_CONFIG_PUBLIC Value: public Base {
38 public:
47 Value(const T& value): Base(), m_value(value) {}
48
53 template <typename U = T>
54 Value(const char* value) requires std::is_same_v<U, std::string>
55 : Base(), m_value(std::string(value)) {}
56
62 template <typename U = T>
63 Value(const char* name, const char* value) requires std::is_same_v<U, std::string>
64 : Base(std::string(name)), m_value(std::string(value)) {}
65
70 Value(T&& value): Base(), m_value(std::move(value)) {}
71
77 Value(const std::string& name, const T& value): Base(name), m_value(value) {}
78
84 Value(std::string&& name, T&& value): Base(std::move(name)), m_value(std::move(value)) {}
85
91 Value(const std::string& name, const char* value) requires std::is_same_v<T, std::string>
92 : Base(name), m_value(std::string(value)) {}
93
99 Value(std::string&& name, const char* value) requires std::is_same_v<T, std::string>
100 : Base(std::move(name)), m_value(std::string(value)) {}
101
106 Value(const Value& single) = default;
107
112 Value(Value&& single) noexcept = default;
113
119 Value& operator=(const Value& single) = default;
120
126 Value& operator=(Value&& single) noexcept = default;
127
131 virtual ~Value() noexcept override = default;
143 bool Equals(const Base& other) const noexcept override {
144 if (this->Type() != other.Type())
145 return false;
146 if (this->Name() != other.Name())
147 return false;
148 const Value<T>& other_value = static_cast<const Value<T>&>(other);
149 return m_value == other_value.m_value;
150 }
151
156 constexpr virtual Item::Type Type() const noexcept override {
157 if constexpr (std::is_same_v<T, std::string>) {
158 return Item::Type::String;
159 } else if constexpr (std::is_same_v<T, int>) {
160 return Item::Type::Integer;
161 } else if constexpr (std::is_same_v<T, double>) {
162 return Item::Type::Double;
163 } else if constexpr (std::is_same_v<T, bool>) {
164 return Item::Type::Bool;
165 } else if constexpr (std::is_same_v<T, std::vector<std::byte>>) {
166 return Item::Type::Binary;
167 }
168 }
169
175 bool operator==(const Value<T>& single) const noexcept {
176 return Equals(single);
177 }
178
184 bool operator!=(const Value<T>& single) const noexcept {
185 return !operator==(single);
186 }
187
192 T& operator*() noexcept {
193 return m_value;
194 }
195
200 const T& operator*() const noexcept {
201 return m_value;
202 }
203
209 std::string Serialize(const int& indent_level) const noexcept override;
210
215 virtual PointerType Clone() const override {
216 return MakePointer<Value<T>>(*this);
217 }
218
223 virtual PointerType Move() override {
224 return MakePointer<Value<T>>(std::move(*this));
225 }
228 protected:
230 };
231
232 Value(const char*) -> Value<std::string>;
233 Value(const char*, const char*) -> Value<std::string>;
234}
The base class for all configuration items.
Definition base.hxx:44
Named or unnamed typed value.
Definition value.hxx:37
Value(const char *value)
Constructs a string Value from a C string.
Definition value.hxx:54
virtual constexpr Item::Type Type() const noexcept override
Gets the type of the item.
Definition value.hxx:156
const T & operator*() const noexcept
Gets the item value (const).
Definition value.hxx:200
virtual PointerType Move() override
Moves the item.
Definition value.hxx:223
Value(const std::string &name, const T &value)
Constructs a named Value.
Definition value.hxx:77
T & operator*() noexcept
Gets the item value (mutable).
Definition value.hxx:192
virtual ~Value() noexcept override=default
Destructor.
Value(const std::string &name, const char *value)
Named string constructor from C string value.
Definition value.hxx:91
Value(std::string &&name, const char *value)
Named string constructor (move name) from C string value.
Definition value.hxx:99
Value(T &&value)
Move constructor from value.
Definition value.hxx:70
Value(const Value &single)=default
Copy constructor.
T m_value
The value of the item.
Definition value.hxx:229
Value(Value &&single) noexcept=default
Move constructor.
Value & operator=(Value &&single) noexcept=default
Move assignment operator.
Value(const T &value)
Constructs a Value with the given value.
Definition value.hxx:47
bool operator==(const Value< T > &single) const noexcept
Checks if two Value objects are equal.
Definition value.hxx:175
virtual PointerType Clone() const override
Clones the item.
Definition value.hxx:215
Value(const char *name, const char *value)
Constructs a named string Value from C strings.
Definition value.hxx:63
Value(std::string &&name, T &&value)
Constructs a named Value (move).
Definition value.hxx:84
Value & operator=(const Value &single)=default
Copy assignment operator.
std::string Serialize(const int &indent_level) const noexcept override
Serializes the item to a string.
bool operator!=(const Value< T > &single) const noexcept
Inequality operator.
Definition value.hxx:184
Configuration items (values, comments, groups, lists).
Definition alias.hxx:30
Type
Represents the type of a configuration item.
Definition type.hxx:39