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 (including everything under
22 * thirdparty/, and in particular the bundled StormByte-String tree and
23 * the StormByte Base tree it vendors), which remains under its own license.
24 *
25 * Neither license grants any patent rights. Any patent licenses required
26 * to use this software or third-party components must be obtained separately
27 * from the patent holders.
28 *
29 * StormByte-Config is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU Lesser General Public License for more details.
33 *
34 * You should have received a copy of the GNU Lesser General Public License
35 * version 3 along with StormByte-Config. If not, see
36 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
37 *
38 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
39 */
40
41#pragma once
42
49#include <StormByte/platform.h>
50#include <StormByte/size.hxx>
52
53#include <istream>
54#include <ostream>
55#include <span>
56#include <string>
57#include <string_view>
58
62namespace StormByte::Config {
63 namespace Binary {
64 class Reader;
65 class Writer;
66 }
67
78 friend class Binary::Reader;
79 friend class Binary::Writer;
80 public:
89
94 Config(const Config& config);
95
100 Config(Config&& config) noexcept;
101
107 Config& operator=(const Config& config);
108
114 Config& operator=(Config&& config) noexcept;
115
119 virtual ~Config() noexcept;
131 inline Item::Base& operator[](const StormByte::String::String& path) {
132 return m_root.operator[](path);
133 }
134
140 inline const Item::Base& operator[](const StormByte::String::String& path) const {
141 return m_root.operator[](path);
142 }
143
149 inline Item::Base& operator[](std::string_view path) {
150 return m_root.operator[](path);
151 }
152
158 inline const Item::Base& operator[](std::string_view path) const {
159 return m_root.operator[](path);
160 }
161
169 return m_root[index];
170 }
171
178 const Item::Base& operator[](const StormByte::Size& index) const {
179 return m_root[index];
180 }
181
187 inline bool operator==(const Config& config) const noexcept {
188 return m_root == config.m_root;
189 }
190
196 inline bool operator!=(const Config& config) const noexcept {
197 return !operator==(config);
198 }
210 Config& operator<<(const Config& source);
211
216 void operator<<(std::istream& istream);
217
223
228 void operator<<(const std::string& str);
229
236 friend STORMBYTE_CONFIG_PUBLIC Config& operator>>(std::istream& istream, Config& file);
237
245
252 friend STORMBYTE_CONFIG_PUBLIC Config& operator>>(const std::string& str, Config& file);
264 Config& operator>>(Config& dest) const;
265
271 std::ostream& operator>>(std::ostream& ostream) const;
272
278 std::string& operator>>(std::string& str) const;
279
286 friend STORMBYTE_CONFIG_PUBLIC std::ostream& operator<<(std::ostream& ostream, const Config& file);
287
294 friend STORMBYTE_CONFIG_PUBLIC std::string& operator<<(std::string& str, const Config& file);
295
301
306 STORMBYTE_FORCE_INLINE explicit operator std::string() const {
307 return static_cast<std::string>(Text());
308 }
309
315 void Save(std::ostream& stream, Mode mode = Mode::Text) const;
316
323 static ExpectedConfig Load(std::istream& stream, Mode mode = Mode::Text);
336 inline Item::Base& Add(const Item::Base& item) {
337 return m_root.Add(item.Clone(), m_on_existing_action);
338 }
339
347 return m_root.Add(std::move(item.Move()), m_on_existing_action);
348 }
349
357 return m_root.Add(item, m_on_existing_action);
358 }
359
367 return m_root.Add(item, on_existing);
368 }
369
373 inline void Clear() noexcept {
374 m_root.Clear();
375 }
376
382 inline bool Exists(const StormByte::String::String& path) const {
383 return m_root.Exists(path);
384 }
385
391 inline bool Exists(std::string_view path) const {
392 return m_root.Exists(path);
393 }
394
400 inline void Remove(const StormByte::String::String& path) {
401 m_root.Remove(path);
402 }
403
408 inline void Remove(std::string_view path) {
409 m_root.Remove(path);
410 }
411
417 inline void Remove(const StormByte::Size& index) {
418 m_root.Remove(index);
419 }
420
425 StormByte::Size Size() const noexcept {
426 return m_root.Size();
427 }
428
433 StormByte::Size Count() const noexcept {
434 return m_root.Count();
435 }
436
441 constexpr std::span<const Item::Base::PointerType> Items() noexcept {
442 return m_root.Items();
443 }
444
449 constexpr std::span<const Item::Base::PointerType> Items() const noexcept {
450 return m_root.Items();
451 }
464
470
476
482
488
494
502 protected:
504
509 };
510
517 STORMBYTE_CONFIG_PUBLIC Config& operator>>(std::istream& istream, Config& file);
518
526
533 STORMBYTE_CONFIG_PUBLIC Config& operator>>(const std::string& str, Config& file);
534
541 STORMBYTE_CONFIG_PUBLIC std::ostream& operator<<(std::ostream& ostream, const Config& file);
542
549 STORMBYTE_CONFIG_PUBLIC std::string& operator<<(std::string& str, const Config& file);
550}
virtual PointerType Clone() const=0
Configuration document (text or versioned binary).
Definition config.hxx:77
const Item::Base & operator[](std::string_view path) const
Gets a const reference to an item by a path view.
Definition config.hxx:158
HookFunctions m_before_read_hooks
Hooks executed before reading.
Definition config.hxx:505
friend std::ostream & operator<<(std::ostream &ostream, const Config &file)
Output configuration when ostream is on the left-hand side.
void operator<<(const StormByte::String::String &str)
Initialize configuration from a string (text mode).
Item::Base & Add(const Item::Base &item)
Adds an item to the configuration.
Definition config.hxx:336
StormByte::Size Size() const noexcept
Gets the number of items in the current level.
Definition config.hxx:425
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:178
Item::Base & operator[](std::string_view path)
Gets a reference to an item by a path view.
Definition config.hxx:149
void OnParseFailure(StormByte::Shared< FailureHook > hook)
Sets a stateful hook to execute on parse failure.
Config & operator=(Config &&config) noexcept
Move assignment operator.
Item::Base & Add(Item::Base &&item)
Adds an item to the configuration (move).
Definition config.hxx:346
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.
Config(const Config &config)
Copy constructor.
Config & operator>>(Config &dest) const
Output current configuration into another configuration.
bool operator==(const Config &config) const noexcept
Equality operator.
Definition config.hxx:187
void Remove(const StormByte::Size &index)
Removes an item by index.
Definition config.hxx:417
Item::Group m_root
Root group.
Definition config.hxx:503
OptionalFailureHook m_on_parse_failure_hook
Hook executed on failure.
Definition config.hxx:507
bool Exists(std::string_view path) const
Checks if an item exists by a path view.
Definition config.hxx:391
bool Exists(const StormByte::String::String &path) const
Checks if an item exists by path.
Definition config.hxx:382
bool operator!=(const Config &config) const noexcept
Inequality operator.
Definition config.hxx:196
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:366
void AddHookBeforeRead(StormByte::Shared< ReadHook > hook)
Adds a stateful hook executed before reading starts.
void Clear() noexcept
Clears all configuration items.
Definition config.hxx:373
void Remove(std::string_view path)
Removes an item by a path view.
Definition config.hxx:408
StormByte::String::String Text() const
Serialized document as StormByte text.
virtual ~Config() noexcept
Destructor.
Item::Base & operator[](const StormByte::Size &index)
Gets a reference to an item by index.
Definition config.hxx:168
const Item::Base & operator[](const StormByte::String::String &path) const
Gets a const reference to an item by path.
Definition config.hxx:140
Item::Base & Add(Item::Base::PointerType item)
Adds an item pointer to the configuration.
Definition config.hxx:356
constexpr std::span< const Item::Base::PointerType > Items() const noexcept
Items in the current level.
Definition config.hxx:449
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:441
StormByte::Config::OnExistingAction m_on_existing_action
Collision policy.
Definition config.hxx:508
void OnParseFailure(OnFailureHook hook)
Sets a stateless function to execute on parse failure.
Config & operator=(const Config &config)
Copy assignment operator.
void Remove(const StormByte::String::String &path)
Removes an item by path.
Definition config.hxx:400
Config()
Default constructor.
friend Config & operator>>(const StormByte::String::String &str, Config &file)
Initializes configuration when String is on the left-hand side.
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.
void AddHookAfterRead(StormByte::Shared< ReadHook > hook)
Adds a stateful hook executed after a successful read.
StormByte::Size Count() const noexcept
Gets the full number of items (including nested).
Definition config.hxx:433
HookFunctions m_after_read_hooks
Hooks executed after successful reading.
Definition config.hxx:506
void AddHookBeforeRead(HookFunction hook)
Adds a stateless hook executed before reading starts.
Base class for all configuration items.
Definition base.hxx:66
Named container ({ ... }).
Definition group.hxx:57
constexpr Size() noexcept
All the classes for item configuration.
Config module of the StormByte suite.
Definition alias.hxx:50
ReadHookFunction HookFunction
Stateless read hook.
Definition alias.hxx:51
std::vector< StormByte::Shared< ReadHook > > HookFunctions
Read hooks owned by Config.
Definition alias.hxx:52
OnExistingAction
Forward declaration of the configuration document.
Definition typedefs.hxx:56
FailureHookFunction OnFailureHook
Stateless failure hook.
Definition alias.hxx:53
StormByte::Expected< Config, StormByte::Exception > ExpectedConfig
Result of loading a configuration document (text or binary).
Definition typedefs.hxx:83
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:72
@ 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:53