StormByte-Config 2.0.0
C++26 configuration-document module of the StormByte 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 original source is dual-licensed:
7 *
8 * 1. GNU Lesser General Public License v3.0 (or later)
9 * You may redistribute and/or modify this file under the terms of the
10 * GNU Lesser General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * 2. Commercial license
15 * Alternatively, this file may be used under the terms of a commercial
16 * license agreement with the copyright holder
17 * (David C. Manuelda <StormByte@gmail.com>).
18 *
19 * Both licenses apply only to original StormByte-Config source in this
20 * repository. They do not cover other StormByte modules or any third-party
21 * material shipped with this repository. The bundled StormByte Base tree
22 * under thirdparty/ remains under its own license.
23 *
24 * Neither license grants any patent rights. Any patent licenses required
25 * to use this software or third-party components must be obtained separately
26 * from the patent holders.
27 *
28 * StormByte-Config is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU Lesser General Public License for more details.
32 *
33 * You should have received a copy of the GNU Lesser General Public License
34 * version 3 along with StormByte-Config. If not, see
35 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
36 *
37 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
38 */
39
40#pragma once
41
48#include <StormByte/platform.h>
49#include <StormByte/size.hxx>
51
52#include <istream>
53#include <ostream>
54#include <span>
55#include <string>
56#include <string_view>
57
61namespace StormByte::Config {
62 namespace Binary {
63 class Reader;
64 class Writer;
65 }
66
77 friend class Binary::Reader;
78 friend class Binary::Writer;
79 public:
88
93 Config(const Config& config);
94
99 Config(Config&& config) noexcept;
100
106 Config& operator=(const Config& config);
107
113 Config& operator=(Config&& config) noexcept;
114
118 virtual ~Config() noexcept;
130 inline Item::Base& operator[](const StormByte::Safe::String& path) {
131 return m_root.operator[](path);
132 }
133
139 inline const Item::Base& operator[](const StormByte::Safe::String& path) const {
140 return m_root.operator[](path);
141 }
142
148 inline Item::Base& operator[](std::string_view path) {
149 return m_root.operator[](path);
150 }
151
157 inline const Item::Base& operator[](std::string_view path) const {
158 return m_root.operator[](path);
159 }
160
168 return m_root[index];
169 }
170
177 const Item::Base& operator[](const StormByte::Size& index) const {
178 return m_root[index];
179 }
180
186 inline bool operator==(const Config& config) const noexcept {
187 return m_root == config.m_root;
188 }
189
195 inline bool operator!=(const Config& config) const noexcept {
196 return !operator==(config);
197 }
209 Config& operator<<(const Config& source);
210
215 void operator<<(std::istream& istream);
216
222
227 void operator<<(const std::string& str);
228
235 friend STORMBYTE_CONFIG_PUBLIC Config& operator>>(std::istream& istream, Config& file);
236
244
251 friend STORMBYTE_CONFIG_PUBLIC Config& operator>>(const std::string& str, Config& file);
263 Config& operator>>(Config& dest) const;
264
270 std::ostream& operator>>(std::ostream& ostream) const;
271
277 std::string& operator>>(std::string& str) const;
278
285 friend STORMBYTE_CONFIG_PUBLIC std::ostream& operator<<(std::ostream& ostream, const Config& file);
286
293 friend STORMBYTE_CONFIG_PUBLIC std::string& operator<<(std::string& str, const Config& file);
294
300
305 STORMBYTE_FORCE_INLINE explicit operator std::string() const {
306 return static_cast<std::string>(Text());
307 }
308
314 void Save(std::ostream& stream, Mode mode = Mode::Text) const;
315
322 static ExpectedConfig Load(std::istream& stream, Mode mode = Mode::Text);
335 inline Item::Base& Add(const Item::Base& item) {
336 return m_root.Add(item.Clone(), m_on_existing_action);
337 }
338
346 return m_root.Add(std::move(item.Move()), m_on_existing_action);
347 }
348
356 return m_root.Add(item, m_on_existing_action);
357 }
358
366 return m_root.Add(item, on_existing);
367 }
368
372 inline void Clear() noexcept {
373 m_root.Clear();
374 }
375
381 inline bool Exists(const StormByte::Safe::String& path) const {
382 return m_root.Exists(path);
383 }
384
390 inline bool Exists(std::string_view path) const {
391 return m_root.Exists(path);
392 }
393
399 inline void Remove(const StormByte::Safe::String& path) {
400 m_root.Remove(path);
401 }
402
407 inline void Remove(std::string_view path) {
408 m_root.Remove(path);
409 }
410
416 inline void Remove(const StormByte::Size& index) {
417 m_root.Remove(index);
418 }
419
424 StormByte::Size Size() const noexcept {
425 return m_root.Size();
426 }
427
432 StormByte::Size Count() const noexcept {
433 return m_root.Count();
434 }
435
440 constexpr std::span<const Item::Base::PointerType> Items() noexcept {
441 return m_root.Items();
442 }
443
448 constexpr std::span<const Item::Base::PointerType> Items() const noexcept {
449 return m_root.Items();
450 }
463
469
475
481
487
493
501 protected:
503
508 };
509
516 STORMBYTE_CONFIG_PUBLIC Config& operator>>(std::istream& istream, Config& file);
517
525
532 STORMBYTE_CONFIG_PUBLIC Config& operator>>(const std::string& str, Config& file);
533
540 STORMBYTE_CONFIG_PUBLIC std::ostream& operator<<(std::ostream& ostream, const Config& file);
541
548 STORMBYTE_CONFIG_PUBLIC std::string& operator<<(std::string& str, const Config& file);
549}
Configuration document (text or versioned binary).
Definition config.hxx:76
const Item::Base & operator[](std::string_view path) const
Gets a const reference to an item by a path view.
Definition config.hxx:157
HookFunctions m_before_read_hooks
Hooks executed before reading.
Definition config.hxx:504
friend Config & operator>>(const StormByte::Safe::String &str, Config &file)
Initializes configuration when String is on the left-hand side.
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:335
StormByte::Size Size() const noexcept
Gets the number of items in the current level.
Definition config.hxx:424
void AddHookAfterRead(HookFunction hook)
Adds a stateless hook executed after a successful read.
void OnExistingAction(const StormByte::Config::OnExistingAction &on_existing)
Sets the action to take when an item name/identity collision occurs.
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).
const Item::Base & operator[](const StormByte::Size &index) const
Gets a const reference to an item by index.
Definition config.hxx:177
Item::Base & operator[](std::string_view path)
Gets a reference to an item by a path view.
Definition config.hxx:148
Config & operator=(Config &&config) noexcept
Move assignment operator.
bool Exists(const StormByte::Safe::String &path) const
Checks if an item exists by path.
Definition config.hxx:381
Item::Base & Add(Item::Base &&item)
Adds an item to the configuration (move).
Definition config.hxx:345
const Item::Base & operator[](const StormByte::Safe::String &path) const
Gets a const reference to an item by path.
Definition config.hxx:139
void operator<<(const std::string &str)
Initialize configuration from a caller-owned std::string (text mode).
std::string & operator>>(std::string &str) const
Append serialized text to a caller-owned string.
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.
static ExpectedConfig Load(std::istream &stream, Mode mode=Mode::Text)
Read a document from an input stream.
void OnParseFailure(StormByte::Safe::Shared< FailureHook > hook)
Sets a stateful hook to execute on parse failure.
Config(const Config &config)
Copy constructor.
void Remove(const StormByte::Safe::String &path)
Removes an item by path.
Definition config.hxx:399
Config & operator>>(Config &dest) const
Output current configuration into another configuration.
bool operator==(const Config &config) const noexcept
Equality operator.
Definition config.hxx:186
void Remove(const StormByte::Size &index)
Removes an item by index.
Definition config.hxx:416
Item::Group m_root
Root group.
Definition config.hxx:502
OptionalFailureHook m_on_parse_failure_hook
Hook executed on failure.
Definition config.hxx:506
bool Exists(std::string_view path) const
Checks if an item exists by a path view.
Definition config.hxx:390
bool operator!=(const Config &config) const noexcept
Inequality operator.
Definition config.hxx:195
friend Config & operator>>(const std::string &str, Config &file)
Initializes configuration when string is on the left-hand side.
Item::Base & Add(Item::Base::PointerType item, const StormByte::Config::OnExistingAction &on_existing)
Adds an item pointer using an explicit policy.
Definition config.hxx:365
void Clear() noexcept
Clears all configuration items.
Definition config.hxx:372
void Remove(std::string_view path)
Removes an item by a path view.
Definition config.hxx:407
virtual ~Config() noexcept
Destructor.
void AddHookAfterRead(StormByte::Safe::Shared< ReadHook > hook)
Adds a stateful hook executed after a successful read.
Item::Base & operator[](const StormByte::Size &index)
Gets a reference to an item by index.
Definition config.hxx:167
Item::Base & Add(Item::Base::PointerType item)
Adds an item pointer to the configuration.
Definition config.hxx:355
constexpr std::span< const Item::Base::PointerType > Items() const noexcept
Items in the current level.
Definition config.hxx:448
friend Config & operator>>(std::istream &istream, Config &file)
Initializes configuration when istream is on the left-hand side.
constexpr std::span< const Item::Base::PointerType > Items() noexcept
Items in the current level.
Definition config.hxx:440
StormByte::Config::OnExistingAction m_on_existing_action
Collision policy.
Definition config.hxx:507
void operator<<(const StormByte::Safe::String &str)
Initialize configuration from a string (text mode).
void OnParseFailure(OnFailureHook hook)
Sets a stateless function to execute on parse failure.
Config & operator=(const Config &config)
Copy assignment operator.
Config()
Default constructor.
Config(Config &&config) noexcept
Move constructor.
friend std::string & operator<<(std::string &str, const Config &file)
Output configuration when string is on the left-hand side.
StormByte::Safe::String Text() const
Serialized document as StormByte text.
StormByte::Size Count() const noexcept
Gets the full number of items (including nested).
Definition config.hxx:432
void AddHookBeforeRead(StormByte::Safe::Shared< ReadHook > hook)
Adds a stateful hook executed before reading starts.
HookFunctions m_after_read_hooks
Hooks executed after successful reading.
Definition config.hxx:505
void AddHookBeforeRead(HookFunction hook)
Adds a stateless hook executed before reading starts.
Base class for all configuration items.
Definition base.hxx:65
Named container ({ ... }).
Definition group.hxx:56
virtual PointerType Clone() const=0
constexpr Size() noexcept
All the classes for item configuration.
Config module of the StormByte suite.
Definition alias.hxx:49
ReadHookFunction HookFunction
Stateless read hook.
Definition alias.hxx:50
std::vector< StormByte::Safe::Shared< ReadHook > > HookFunctions
Read hooks owned by Config.
Definition alias.hxx:51
OnExistingAction
Forward declaration of the configuration document.
Definition typedefs.hxx:55
FailureHookFunction OnFailureHook
Stateless failure hook.
Definition alias.hxx:52
StormByte::Expected< Config, StormByte::Exception > ExpectedConfig
Result of loading a configuration document (text or binary).
Definition typedefs.hxx:82
std::ostream & operator<<(std::ostream &ostream, const Config &file)
Output configuration when ostream is on the left-hand side.
Mode
Serialization / deserialization mode for configuration documents.
Definition typedefs.hxx:71
@ Text
Human-readable configuration syntax (UTF-8 text).
Config & operator>>(std::istream &istream, Config &file)
Initializes configuration when istream is on the left-hand side.
bool operator==(std::span< const std::byte > bytes, const BinaryData &data) noexcept
#define STORMBYTE_FORCE_INLINE
#define STORMBYTE_CONFIG_PUBLIC
Definition visibility.h:52