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>
54 class STORMBYTE_CONFIG_PUBLIC
Config {
55 friend class Binary::Reader;
56 friend class Binary::Writer;
109 return m_root.operator[](path);
118 return m_root.operator[](path);
128 return m_root[index];
138 return m_root[index];
147 return m_root == config.m_root;
156 return !operator==(config);
231 friend STORMBYTE_CONFIG_PUBLIC std::ostream&
operator<<(std::ostream& ostream,
const Config& file);
245 operator std::string()
const;
252 void Save(std::ostream& stream,
Mode mode = Mode::Text)
const;
274 return m_root.Add(item.Clone(), m_on_existing_action);
284 return m_root.Add(std::move(item.Move()), m_on_existing_action);
299 inline bool Exists(
const std::string& path)
const {
300 return m_root.Exists(path);
308 inline void Remove(
const std::string& path) {
317 inline void Remove(
const size_t& index) {
318 m_root.Remove(index);
325 constexpr virtual size_t Size() const noexcept {
326 return m_root.Size();
333 inline virtual size_t Count() const noexcept {
334 return m_root.Count();
341 constexpr std::span<Item::Base::PointerType>
Items() noexcept {
342 return m_root.Items();
349 constexpr std::span<const Item::Base::PointerType>
Items() const noexcept {
350 return m_root.Items();
364 m_on_existing_action = on_existing;
365 m_root.SetOnExistingAction(on_existing);
373 m_on_parse_failure_hook = hook;
382 m_before_read_hooks.push_back(hook);
391 m_after_read_hooks.push_back(hook);
426 STORMBYTE_CONFIG_PUBLIC std::ostream&
operator<<(std::ostream& ostream,
const Config& file);
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