StormByte C++ Library: Config module 0.0.9999
StormByte-Config is a StormByte library module for parsing configuration files
Loading...
Searching...
No Matches
base.hxx
1#pragma once
2
3#include <StormByte/config/exception.hxx>
4#include <StormByte/config/item/type.hxx>
5#include <StormByte/clonable.hxx>
6
7#include <optional>
8#include <string>
9#include <type_traits>
10
11namespace StormByte::Config::Item {
12 // Forwards
13 class Container;
14 class Group;
15 class List;
16 template<AllowedValueType T> class Value;
17
23 bool STORMBYTE_CONFIG_PUBLIC IsNameValid(const std::string&) noexcept;
24
29 class STORMBYTE_CONFIG_PUBLIC Base: public Clonable<Base, std::shared_ptr<Base>> {
30 public:
35 Base() = default;
36
41 Base(const std::string& name);
42
47 Base(const Base& base) = default;
48
53 Base(Base&& base) noexcept = default;
54
59 Base& operator=(const Base& base) = default;
60
65 Base& operator=(Base&& base) noexcept = default;
66
70 virtual ~Base() noexcept = default;
71
77 inline bool operator==(const Base& base) const noexcept {
78 return m_name == base.m_name;
79 }
80
86 inline bool operator!=(const Base& base) const noexcept {
87 return !operator==(base);
88 }
89
94 constexpr const std::optional<std::string>& Name() const noexcept {
95 return m_name;
96 }
97
102 constexpr void Name(const std::string& name) noexcept {
103 m_name = name;
104 }
105
110 inline bool IsNameValid() const noexcept {
111 return m_name.has_value() && Item::IsNameValid(m_name.value());
112 }
113
118 constexpr virtual Type Type() const noexcept = 0;
119
124 constexpr std::string TypeToString() const noexcept {
125 return Item::TypeToString(this->Type());
126 }
127
132 virtual std::string Serialize(const int& indent_level) const noexcept;
133
138 operator std::string() const {
139 return this->Serialize(0);
140 }
141
147 template<typename T>
148 const T& Value() const {
149 if constexpr (std::is_base_of_v<std::remove_reference_t<decltype(*this)>, T>) {
150 // Direct match: T (e.g., Container, Group, etc.) matches this object type
151 return static_cast<const T&>(*this);
152 } else if constexpr (std::is_base_of_v<std::remove_reference_t<decltype(*this)>, Item::Value<T>>) {
153 // Indirect match: T is wrapped inside Item::Value<T>
154 return *static_cast<const Item::Value<T>&>(*this);
155 } else {
156 // No valid match: Throw an exception for incorrect conversion
157 throw WrongValueTypeConversion("Wrong value type {} while expecting {}", this->TypeToString(), typeid(T).name());
158 }
159 }
160
166 template<typename T>
167 T& Value() {
168 return const_cast<T&>(static_cast<const Base&>(*this).Value<T>());
169 }
170
171
172 protected:
173 std::optional<std::string> m_name;
174
175
176 };
177}
The base class for all configuration items.
Definition base.hxx:29
virtual ~Base() noexcept=default
Base(const std::string &name)
Constructs a Base item with an optional name.
bool operator!=(const Base &base) const noexcept
Definition base.hxx:86
Base()=default
Constructs a Base item with an optional name.
Base & operator=(const Base &base)=default
Base(Base &&base) noexcept=default
const T & Value() const
Definition base.hxx:148
bool IsNameValid() const noexcept
Definition base.hxx:110
constexpr void Name(const std::string &name) noexcept
Definition base.hxx:102
constexpr const std::optional< std::string > & Name() const noexcept
Gets the name of the item.
Definition base.hxx:94
virtual constexpr Type Type() const noexcept=0
virtual std::string Serialize(const int &indent_level) const noexcept
std::optional< std::string > m_name
Item name.
Definition base.hxx:173
Base & operator=(Base &&base) noexcept=default
T & Value()
Definition base.hxx:167
Base(const Base &base)=default
Represents a configuration item with a value.
Definition value.hxx:16
Thrown when a value is converted to an incorrect type.
Definition exception.hxx:27