StormByte C++ Library: Config module 1.0.1.9999
StormByte-Config is the configuration module of the StormByte C++ suite.
Loading...
Searching...
No Matches
config.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/alias.hxx>
23#include <StormByte/config/item/comment.hxx>
24#include <StormByte/config/item/group.hxx>
25#include <StormByte/config/item/list.hxx>
26#include <StormByte/config/item/value.hxx>
27#include <StormByte/config/typedefs.hxx>
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
54 class STORMBYTE_CONFIG_PUBLIC Config {
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
290 inline void Clear() noexcept {
291 m_root.Clear();
292 }
293
299 inline bool Exists(const std::string& path) const {
300 return m_root.Exists(path);
301 }
302
308 inline void Remove(const std::string& path) {
309 m_root.Remove(path);
310 }
311
317 inline void Remove(const size_t& index) {
318 m_root.Remove(index);
319 }
320
325 constexpr virtual size_t Size() const noexcept {
326 return m_root.Size();
327 }
328
333 inline virtual size_t Count() const noexcept {
334 return m_root.Count();
335 }
336
341 constexpr std::span<Item::Base::PointerType> Items() noexcept {
342 return m_root.Items();
343 }
344
349 constexpr std::span<const Item::Base::PointerType> Items() const noexcept {
350 return m_root.Items();
351 }
363 void OnExistingAction(const OnExistingAction& on_existing) {
364 m_on_existing_action = on_existing;
365 m_root.SetOnExistingAction(on_existing);
366 }
367
372 constexpr void OnParseFailure(OnFailureHook hook) {
373 m_on_parse_failure_hook = hook;
374 }
375
381 constexpr void AddHookBeforeRead(HookFunction hook) {
382 m_before_read_hooks.push_back(hook);
383 }
384
390 constexpr void AddHookAfterRead(HookFunction hook) {
391 m_after_read_hooks.push_back(hook);
392 }
395 protected:
397
402 };
403
410 STORMBYTE_CONFIG_PUBLIC Config& operator>>(std::istream& istream, Config& file);
411
418 STORMBYTE_CONFIG_PUBLIC Config& operator>>(const std::string& str, Config& file);
419
426 STORMBYTE_CONFIG_PUBLIC std::ostream& operator<<(std::ostream& ostream, const Config& file);
427
434 STORMBYTE_CONFIG_PUBLIC std::string& operator<<(std::string& str, const Config& file);
435}
Configuration document (text or versioned binary).
Definition config.hxx:54
HookFunctions m_before_read_hooks
Hooks executed before reading.
Definition config.hxx:398
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:325
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. The policy is applied to the roo...
Definition config.hxx:363
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:317
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:372
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:396
OptionalFailureHook m_on_parse_failure_hook
Hook executed on failure.
Definition config.hxx:400
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
friend STORMBYTE_CONFIG_PUBLIC Config & operator>>(std::istream &istream, Config &file)
Initializes configuration when istream is on the left-hand side.
bool operator!=(const Config &config) const noexcept
Inequality operator.
Definition config.hxx:155
constexpr void AddHookAfterRead(HookFunction hook)
Adds a hook executed after a successful read. Hooks run in order.
Definition config.hxx:390
friend STORMBYTE_CONFIG_PUBLIC std::string & operator<<(std::string &, const Config &)
Output configuration when string is on the left-hand side.
friend STORMBYTE_CONFIG_PUBLIC 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:341
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:290
Config & operator=(Config &&config) noexcept=default
Move assignment operator.
friend STORMBYTE_CONFIG_PUBLIC std::ostream & operator<<(std::ostream &ostream, const Config &file)
Output configuration when ostream is on the left-hand side.
void Remove(const std::string &path)
Removes an item by path.
Definition config.hxx:308
constexpr void AddHookBeforeRead(HookFunction hook)
Adds a hook executed before reading starts. Hooks run in order.
Definition config.hxx:381
constexpr std::span< const Item::Base::PointerType > Items() const noexcept
Gets the items in the current level (const).
Definition config.hxx:349
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:401
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:333
HookFunctions m_after_read_hooks
Hooks executed after successful reading.
Definition config.hxx:399
bool Exists(const std::string &path) const
Checks if an item exists by path.
Definition config.hxx:299
The base class for all configuration items.
Definition base.hxx:44
Named container { … } that can hold items, subgroups and lists.
Definition group.hxx:38
@ Binary
Binary data (std::vector<std::byte>; text form is Base64 b"...")
Config module of the StormByte suite.
Definition alias.hxx:29
STORMBYTE_CONFIG_PUBLIC Config & operator>>(std::istream &istream, Config &file)
Initializes configuration when istream is on the left-hand side.
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
STORMBYTE_CONFIG_PUBLIC 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