StormByte C++ Library: Config module 0.0.9999
StormByte-Config is a StormByte library module for parsing configuration files
Loading...
Searching...
No Matches
config.hxx
1#pragma once
2
3#include <StormByte/config/alias.hxx>
4#include <StormByte/config/item/value.hxx>
5#include <StormByte/config/item/comment.hxx>
6#include <StormByte/config/item/group.hxx>
7#include <StormByte/config/item/list.hxx>
8#include <StormByte/config/type.hxx>
9#include <StormByte/serializable.hxx>
10
15namespace StormByte::Config {
29 class STORMBYTE_CONFIG_PUBLIC Config {
30 friend class StormByte::Serializable<Config>;
31 public:
36
41 Config(const Config& config) = default;
42
47 Config(Config&& config) noexcept = default;
48
53 Config& operator=(const Config& config) = default;
54
59 Config& operator=(Config&& config) noexcept = default;
60
64 virtual ~Config() = default;
65
71 inline Item::Base& operator[](const std::string& path) {
72 return m_root.operator[](path);
73 }
74
80 inline const Item::Base& operator[](const std::string& path) const {
81 return m_root.operator[](path);
82 }
83
90 Item::Base& operator[](const size_t& index) {
91 return m_root[index];
92 }
93
100 const Item::Base& operator[](const size_t& index) const {
101 return m_root[index];
102 }
103
109 inline bool operator==(const Config& config) const noexcept {
110 return m_root == config.m_root;
111 }
112
118 inline bool operator!=(const Config& config) const noexcept {
119 return !operator==(config);
120 }
121
122 /* INPUT */
128 Config& operator<<(const Config& source);
129
134 void operator<<(std::istream& istream); // 1
135
140 void operator<<(const std::string& str); // 2
141
147 friend STORMBYTE_CONFIG_PUBLIC Config& operator>>(std::istream& istream, Config& file); // 3
148
154 friend STORMBYTE_CONFIG_PUBLIC Config& operator>>(const std::string& str, Config& file); // 4
155
156 /* OUTPUT */
162 Config& operator>>(Config& dest) const;
163
168 std::ostream& operator>>(std::ostream& ostream) const; // 5
169
174 std::string& operator>>(std::string& str) const; // 6
175
181 friend STORMBYTE_CONFIG_PUBLIC std::ostream& operator<<(std::ostream& ostream, const Config& file); // 7
182
188 friend STORMBYTE_CONFIG_PUBLIC std::string& operator<<(std::string&, const Config&); // 8
189
193 operator std::string() const;
194
201 inline Item::Base& Add(const Item::Base& item) {
202 return m_root.Add(item.Clone(), m_on_existing_action);
203 }
204
212 return m_root.Add(std::move(item.Move()), m_on_existing_action);
213 }
214
218 inline void Clear() noexcept {
219 m_root.Clear();
220 }
221
227 inline bool Exists(const std::string& path) const {
228 return m_root.Exists(path);
229 }
230
236 inline void Remove(const std::string& path) {
237 m_root.Remove(path);
238 }
239
245 inline void Remove(const size_t& path) {
246 m_root.Remove(path);
247 }
248
254 constexpr void OnExistingAction(const OnExistingAction& on_existing) {
255 m_on_existing_action = on_existing;
256 }
257
262 constexpr void OnParseFailure(OnFailureHook hook) {
263 m_on_parse_failure_hook = hook;
264 }
265
270 constexpr void AddHookBeforeRead(HookFunction hook) {
271 m_before_read_hooks.push_back(hook);
272 }
273
278 constexpr void AddHookAfterRead(HookFunction hook) {
279 m_after_read_hooks.push_back(hook);
280 }
281
286 constexpr virtual size_t Size() const noexcept {
287 return m_root.Size();
288 }
289
294 inline virtual size_t Count() const noexcept {
295 return m_root.Count();
296 }
297
302 constexpr std::span<Item::Base::PointerType> Items() noexcept {
303 return m_root.Items();
304 }
305
310 constexpr std::span<const Item::Base::PointerType> Items() const noexcept {
311 return m_root.Items();
312 }
313
314 protected:
316
321 HookFunctions m_before_read_hooks;
322 HookFunctions m_after_read_hooks;
323 OptionalFailureHook m_on_parse_failure_hook;
324
330 StormByte::Config::OnExistingAction m_on_existing_action;
331 };
337 STORMBYTE_CONFIG_PUBLIC Config& operator>>(std::istream& istream, Config& file);
338
344 STORMBYTE_CONFIG_PUBLIC Config& operator>>(const std::string& str, Config& file);
345
351 STORMBYTE_CONFIG_PUBLIC std::ostream& operator<<(std::ostream& ostream, const Config& file);
352
358 STORMBYTE_CONFIG_PUBLIC std::string& operator<<(std::string& str, const Config& file);
359}
HookFunctions m_before_read_hooks
Hooks executed before reading.
Definition config.hxx:321
Item::Base & Add(const Item::Base &item)
Adds an item to the configuration.
Definition config.hxx:201
void Remove(const size_t &path)
Definition config.hxx:245
std::ostream & operator>>(std::ostream &ostream) const
void operator<<(std::istream &istream)
virtual constexpr size_t Size() const noexcept
Definition config.hxx:286
Item::Base & Add(Item::Base &&item)
Definition config.hxx:211
void operator<<(const std::string &str)
std::string & operator>>(std::string &str) const
Config & operator<<(const Config &source)
constexpr void OnParseFailure(OnFailureHook hook)
Definition config.hxx:262
Config & operator>>(Config &dest) const
bool operator==(const Config &config) const noexcept
Definition config.hxx:109
Item::Group m_root
Root group.
Definition config.hxx:315
OptionalFailureHook m_on_parse_failure_hook
Hook executed on failure.
Definition config.hxx:323
Item::Base & operator[](const size_t &index)
Definition config.hxx:90
Config(const Config &config)=default
const Item::Base & operator[](const size_t &index) const
Definition config.hxx:100
friend STORMBYTE_CONFIG_PUBLIC Config & operator>>(std::istream &istream, Config &file)
bool operator!=(const Config &config) const noexcept
Definition config.hxx:118
constexpr void AddHookAfterRead(HookFunction hook)
Definition config.hxx:278
friend STORMBYTE_CONFIG_PUBLIC std::string & operator<<(std::string &, const Config &)
friend STORMBYTE_CONFIG_PUBLIC Config & operator>>(const std::string &str, Config &file)
constexpr std::span< Item::Base::PointerType > Items() noexcept
Definition config.hxx:302
const Item::Base & operator[](const std::string &path) const
Definition config.hxx:80
void Clear() noexcept
Definition config.hxx:218
Config & operator=(Config &&config) noexcept=default
friend STORMBYTE_CONFIG_PUBLIC std::ostream & operator<<(std::ostream &ostream, const Config &file)
void Remove(const std::string &path)
Definition config.hxx:236
constexpr void AddHookBeforeRead(HookFunction hook)
Definition config.hxx:270
constexpr std::span< const Item::Base::PointerType > Items() const noexcept
Definition config.hxx:310
Item::Base & operator[](const std::string &path)
Definition config.hxx:71
StormByte::Config::OnExistingAction m_on_existing_action
Action to take when item name already exists.
Definition config.hxx:330
Config(Config &&config) noexcept=default
virtual ~Config()=default
Config & operator=(const Config &config)=default
virtual size_t Count() const noexcept
Definition config.hxx:294
HookFunctions m_after_read_hooks
Hooks executed after successful reading.
Definition config.hxx:322
constexpr void OnExistingAction(const OnExistingAction &on_existing)
Definition config.hxx:254
bool Exists(const std::string &path) const
Definition config.hxx:227
The base class for all configuration items.
Definition base.hxx:29
Represents a group in a configuration file that can hold other items, subgroups, and sublists recursi...
Definition group.hxx:20
Contains type aliases and utilities for configuration handling.