StormByte C++ Library: Config module 1.1.0
StormByte-Config is the configuration module of the StormByte C++ suite.
Loading...
Searching...
No Matches
config.hxx
Go to the documentation of this file.
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
28
29#include <cstddef>
30#include <istream>
31#include <ostream>
32#include <span>
33#include <string>
34#include <vector>
35
39namespace StormByte::Config {
40 namespace Binary {
41 class Reader;
42 class Writer;
43 }
44
55 friend class Binary::Reader;
56 friend class Binary::Writer;
57 public:
66
71 Config(const Config& config) = default;
72
77 Config(Config&& config) noexcept = default;
78
84 Config& operator=(const Config& config) = default;
85
91 Config& operator=(Config&& config) noexcept = default;
92
96 virtual ~Config() = default;
108 inline Item::Base& operator[](const std::string& path) {
109 return m_root.operator[](path);
110 }
111
117 inline const Item::Base& operator[](const std::string& path) const {
118 return m_root.operator[](path);
119 }
120
127 Item::Base& operator[](const size_t& index) {
128 return m_root[index];
129 }
130
137 const Item::Base& operator[](const size_t& index) const {
138 return m_root[index];
139 }
140
146 inline bool operator==(const Config& config) const noexcept {
147 return m_root == config.m_root;
148 }
149
155 inline bool operator!=(const Config& config) const noexcept {
156 return !operator==(config);
157 }
169 Config& operator<<(const Config& source);
170
175 void operator<<(std::istream& istream);
176
181 void operator<<(const std::string& str);
182
189 friend STORMBYTE_CONFIG_PUBLIC Config& operator>>(std::istream& istream, Config& file);
190
197 friend STORMBYTE_CONFIG_PUBLIC Config& operator>>(const std::string& str, Config& file);
209 Config& operator>>(Config& dest) const;
210
216 std::ostream& operator>>(std::ostream& ostream) const;
217
223 std::string& operator>>(std::string& str) const;
224
231 friend STORMBYTE_CONFIG_PUBLIC std::ostream& operator<<(std::ostream& ostream, const Config& file);
232
239 friend STORMBYTE_CONFIG_PUBLIC std::string& operator<<(std::string&, const Config&);
240
245 operator std::string() const;
246
252 void Save(std::ostream& stream, Mode mode = Mode::Text) const;
253
260 static ExpectedConfig Load(std::istream& stream, Mode mode = Mode::Text);
273 inline Item::Base& Add(const Item::Base& item) {
274 return m_root.Add(item.Clone(), m_on_existing_action);
275 }
276
284 return m_root.Add(std::move(item.Move()), m_on_existing_action);
285 }
286
293 inline Item::Base& Add(Item::Base::PointerType item) {
294 return m_root.Add(item, m_on_existing_action);
295 }
296
303 inline Item::Base& Add(Item::Base::PointerType item, const StormByte::Config::OnExistingAction& on_existing) {
304 return m_root.Add(item, on_existing);
305 }
306
310 inline void Clear() noexcept {
311 m_root.Clear();
312 }
313
319 inline bool Exists(const std::string& path) const {
320 return m_root.Exists(path);
321 }
322
328 inline void Remove(const std::string& path) {
329 m_root.Remove(path);
330 }
331
337 inline void Remove(const size_t& index) {
338 m_root.Remove(index);
339 }
340
345 constexpr virtual size_t Size() const noexcept {
346 return m_root.Size();
347 }
348
353 inline virtual size_t Count() const noexcept {
354 return m_root.Count();
355 }
356
361 constexpr std::span<Item::Base::PointerType> Items() noexcept {
362 return m_root.Items();
363 }
364
369 constexpr std::span<const Item::Base::PointerType> Items() const noexcept {
370 return m_root.Items();
371 }
383 void OnExistingAction(const OnExistingAction& on_existing) {
384 m_on_existing_action = on_existing;
385 m_root.SetOnExistingAction(on_existing);
386 }
387
392 constexpr void OnParseFailure(OnFailureHook hook) {
393 m_on_parse_failure_hook = hook;
394 }
395
401 constexpr void AddHookBeforeRead(HookFunction hook) {
402 m_before_read_hooks.push_back(hook);
403 }
404
410 constexpr void AddHookAfterRead(HookFunction hook) {
411 m_after_read_hooks.push_back(hook);
412 }
415 protected:
417
422 };
423
430 STORMBYTE_CONFIG_PUBLIC Config& operator>>(std::istream& istream, Config& file);
431
438 STORMBYTE_CONFIG_PUBLIC Config& operator>>(const std::string& str, Config& file);
439
446 STORMBYTE_CONFIG_PUBLIC std::ostream& operator<<(std::ostream& ostream, const Config& file);
447
454 STORMBYTE_CONFIG_PUBLIC std::string& operator<<(std::string& str, const Config& file);
455}
Configuration document (text or versioned binary).
Definition config.hxx:54
HookFunctions m_before_read_hooks
Hooks executed before reading.
Definition config.hxx:418
friend std::ostream & operator<<(std::ostream &ostream, const Config &file)
Output configuration when ostream is on the left-hand side.
Item::Base & Add(const Item::Base &item)
Adds an item to the configuration.
Definition config.hxx:273
std::ostream & operator>>(std::ostream &ostream) const
Output configuration serialized to an output stream (text).
void operator<<(std::istream &istream)
Initialize configuration from an input stream (text mode).
virtual constexpr size_t Size() const noexcept
Gets the number of items in the current level.
Definition config.hxx:345
Item::Base & Add(Item::Base &&item)
Adds an item to the configuration (move).
Definition config.hxx:283
void OnExistingAction(const OnExistingAction &on_existing)
Sets the action to take when an item name/identity collision occurs.
Definition config.hxx:383
void operator<<(const std::string &str)
Initialize configuration from a string (text mode).
void Remove(const size_t &index)
Removes an item by index.
Definition config.hxx:337
std::string & operator>>(std::string &str) const
Output configuration serialized to a string (text).
void Save(std::ostream &stream, Mode mode=Mode::Text) const
Write this document to an output stream.
Config & operator<<(const Config &source)
Import data from another configuration.
constexpr void OnParseFailure(OnFailureHook hook)
Sets a function to execute on parse failure.
Definition config.hxx:392
static ExpectedConfig Load(std::istream &stream, Mode mode=Mode::Text)
Read a document from an input stream.
Config & operator>>(Config &dest) const
Output current configuration into another configuration.
bool operator==(const Config &config) const noexcept
Equality operator.
Definition config.hxx:146
Item::Group m_root
Root group.
Definition config.hxx:416
OptionalFailureHook m_on_parse_failure_hook
Hook executed on failure.
Definition config.hxx:420
Item::Base & operator[](const size_t &index)
Gets a reference to an item by index.
Definition config.hxx:127
Config(const Config &config)=default
Copy constructor.
const Item::Base & operator[](const size_t &index) const
Gets a const reference to an item by index.
Definition config.hxx:137
bool operator!=(const Config &config) const noexcept
Inequality operator.
Definition config.hxx:155
friend std::string & operator<<(std::string &, const Config &)
Output configuration when string is on the left-hand side.
constexpr void AddHookAfterRead(HookFunction hook)
Adds a hook executed after a successful read.
Definition config.hxx:410
friend Config & operator>>(const std::string &str, Config &file)
Initializes configuration when string is on the left-hand side.
constexpr std::span< Item::Base::PointerType > Items() noexcept
Gets the items in the current level.
Definition config.hxx:361
Item::Base & Add(Item::Base::PointerType item, const StormByte::Config::OnExistingAction &on_existing)
Adds an item pointer using an explicit policy.
Definition config.hxx:303
const Item::Base & operator[](const std::string &path) const
Gets a const reference to an item by path.
Definition config.hxx:117
void Clear() noexcept
Clears all configuration items.
Definition config.hxx:310
Config & operator=(Config &&config) noexcept=default
Move assignment operator.
void Remove(const std::string &path)
Removes an item by path.
Definition config.hxx:328
constexpr void AddHookBeforeRead(HookFunction hook)
Adds a hook executed before reading starts.
Definition config.hxx:401
Item::Base & Add(Item::Base::PointerType item)
Adds an item pointer to the configuration.
Definition config.hxx:293
constexpr std::span< const Item::Base::PointerType > Items() const noexcept
Gets the items in the current level (const).
Definition config.hxx:369
friend Config & operator>>(std::istream &istream, Config &file)
Initializes configuration when istream is on the left-hand side.
Item::Base & operator[](const std::string &path)
Gets a reference to an item by path.
Definition config.hxx:108
StormByte::Config::OnExistingAction m_on_existing_action
Collision policy.
Definition config.hxx:421
Config()
Default constructor.
Config(Config &&config) noexcept=default
Move constructor.
virtual ~Config()=default
Destructor.
Config & operator=(const Config &config)=default
Copy assignment operator.
virtual size_t Count() const noexcept
Gets the full number of items (including nested).
Definition config.hxx:353
HookFunctions m_after_read_hooks
Hooks executed after successful reading.
Definition config.hxx:419
bool Exists(const std::string &path) const
Checks if an item exists by path.
Definition config.hxx:319
The base class for all configuration items.
Definition base.hxx:45
Named container { … } that can hold items, subgroups and lists.
Definition group.hxx:38
Config module of the StormByte suite.
Definition alias.hxx:29
OnExistingAction
Forward declaration of the configuration document.
Definition typedefs.hxx:35
std::function< void(Item::Group &)> HookFunction
Function executed as a hook.
Definition alias.hxx:34
std::vector< HookFunction > HookFunctions
List of hook functions.
Definition alias.hxx:35
StormByte::Expected< Config, StormByte::Exception > ExpectedConfig
Result of loading a configuration document (text or binary).
Definition typedefs.hxx:62
std::ostream & operator<<(std::ostream &ostream, const Config &file)
Output configuration when ostream is on the left-hand side.
std::function< bool(const Item::Group &)> OnFailureHook
Function executed on failure.
Definition alias.hxx:36
std::optional< OnFailureHook > OptionalFailureHook
Optional failure hook.
Definition alias.hxx:37
Mode
Serialization / deserialization mode for configuration documents.
Definition typedefs.hxx:51
Config & operator>>(std::istream &istream, Config &file)
Initializes configuration when istream is on the left-hand side.
#define STORMBYTE_CONFIG_PUBLIC
Definition visibility.h:26