StormByte C++ Library: Config module 1.0.1.9999
StormByte-Config is the configuration module of the StormByte C++ suite.
Loading...
Searching...
No Matches
base.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/exception.hxx>
23#include <StormByte/config/item/type.hxx>
24#include <StormByte/clonable.hxx>
25
26#include <optional>
27#include <string>
28#include <type_traits>
29
34 class Container;
35 class Group;
36 class List;
37 template<AllowedValueType T> class Value;
38 bool STORMBYTE_CONFIG_PUBLIC IsNameValid(const std::string&) noexcept;
39
44 class STORMBYTE_CONFIG_PUBLIC Base: public Clonable<Base, std::shared_ptr<Base>> {
45 public:
49 Base() = default;
50
55 Base(const std::string& name);
56
61 Base(const Base& base) = default;
62
67 Base(Base&& base) noexcept = default;
68
74 Base& operator=(const Base& base) = default;
75
81 Base& operator=(Base&& base) noexcept = default;
82
86 virtual ~Base() noexcept = default;
87
93 bool operator==(const Base& other) const noexcept {
94 return this->Equals(other);
95 }
96
102 bool operator!=(const Base& other) const noexcept {
103 return !(*this == other);
104 }
105
111 virtual bool Equals(const Base& other) const noexcept = 0;
112
117 constexpr const std::optional<std::string>& Name() const noexcept {
118 return m_name;
119 }
120
125 constexpr void Name(const std::string& name) noexcept {
126 m_name = name;
127 }
128
133 inline bool IsNameValid() const noexcept {
134 return m_name.has_value() && Item::IsNameValid(m_name.value());
135 }
136
141 constexpr virtual Type Type() const noexcept = 0;
142
151 virtual std::optional<CommentType> GetCommentType() const noexcept {
152 return std::nullopt;
153 }
154
159 constexpr std::string TypeToString() const noexcept {
160 return Item::TypeToString(this->Type());
161 }
162
168 virtual std::string Serialize(const int& indent_level) const noexcept;
169
174 operator std::string() const {
175 return this->Serialize(0);
176 }
177
184 template<typename T>
185 const T& Value() const {
186 if constexpr (std::is_base_of_v<std::remove_reference_t<decltype(*this)>, T>) {
187 return static_cast<const T&>(*this);
188 } else if constexpr (std::is_base_of_v<std::remove_reference_t<decltype(*this)>, Item::Value<T>>) {
189 return *static_cast<const Item::Value<T>&>(*this);
190 } else {
191 throw WrongValueTypeConversion("Wrong value type {} while expecting {}", this->TypeToString(), typeid(T).name());
192 }
193 }
194
201 template<typename T>
202 T& Value() {
203 return const_cast<T&>(static_cast<const Base&>(*this).Value<T>());
204 }
205
206 protected:
207 std::optional<std::string> m_name;
208 };
209}
The base class for all configuration items.
Definition base.hxx:44
virtual ~Base() noexcept=default
Destructor.
virtual bool Equals(const Base &other) const noexcept=0
Polymorphic equality comparison.
Base(const std::string &name)
Constructs a Base item with a name.
Base()=default
Default constructor.
Base & operator=(const Base &base)=default
Copy assignment operator.
Base(Base &&base) noexcept=default
Move constructor.
const T & Value() const
Gets the item value (const).
Definition base.hxx:185
bool IsNameValid() const noexcept
Checks if the current name is valid.
Definition base.hxx:133
constexpr std::string TypeToString() const noexcept
Gets the item type as a string.
Definition base.hxx:159
constexpr void Name(const std::string &name) noexcept
Sets the item name.
Definition base.hxx:125
constexpr const std::optional< std::string > & Name() const noexcept
Gets the name of the item.
Definition base.hxx:117
virtual constexpr Type Type() const noexcept=0
Gets the item type.
virtual std::string Serialize(const int &indent_level) const noexcept
Serializes the item.
std::optional< std::string > m_name
Item name.
Definition base.hxx:207
bool operator!=(const Base &other) const noexcept
Inequality operator.
Definition base.hxx:102
Base & operator=(Base &&base) noexcept=default
Move assignment operator.
T & Value()
Gets the item value (mutable).
Definition base.hxx:202
Base(const Base &base)=default
Copy constructor.
Named or unnamed typed value.
Definition value.hxx:37
Thrown when a value is converted to an incorrect type.
Definition exception.hxx:45
Configuration items (values, comments, groups, lists).
Definition alias.hxx:30
enum STORMBYTE_CONFIG_PUBLIC List
< List of unnamed items
Definition type.hxx:98
Type
Represents the type of a configuration item.
Definition type.hxx:39
constexpr STORMBYTE_CONFIG_PUBLIC std::string TypeToString(const CommentType &t) noexcept
Converts a CommentType to a human-readable string.
Definition type.hxx:82