49#include <StormByte/platform.h>
50#include <StormByte/safe/binary.hxx>
51#include <StormByte/safe/string.hxx>
52#include <StormByte/size.hxx>
80 friend class Binary::Reader;
81 friend class Binary::Writer;
133 inline
Item::Base& operator[](
std::string_view path) {
134 return m_root.operator[](path);
143 return m_root.operator[](path);
172 return m_root == config.m_root;
203 while (istream.read(chunk,
sizeof(chunk)) || istream.gcount() > 0)
204 text.append(chunk,
static_cast<std::size_t
>(istream.gcount()));
258 str +=
static_cast<std::string
>(
Text());
289 return static_cast<std::string
>(
Text());
305 stream << static_cast<std::string>(
Text());
309 const auto data =
Binary();
310 const auto view = data.span();
312 stream.write(
reinterpret_cast<const char*
>(view.data()),
static_cast<std::streamsize
>(view.size()));
339 while (stream.read(chunk,
sizeof(chunk)) || stream.gcount() > 0)
340 text.append(chunk,
static_cast<std::size_t
>(stream.gcount()));
341 return Load(std::string_view(text));
347 stream.seekg(0, std::ios::end);
348 const std::streamsize size = stream.tellg();
349 stream.seekg(0, std::ios::beg);
350 std::vector<std::byte> bytes;
354 while (stream.read(chunk,
sizeof(chunk)) || stream.gcount() > 0) {
355 const auto count =
static_cast<std::size_t
>(stream.gcount());
356 const auto* begin =
reinterpret_cast<const std::byte*
>(chunk);
357 bytes.insert(bytes.end(), begin, begin + count);
360 bytes.resize(
static_cast<std::size_t
>(size));
361 if (size > 0 && !stream.read(
reinterpret_cast<char*
>(bytes.data()), size))
409 return m_root.Add(item, on_existing);
424 inline bool Exists(std::string_view path)
const {
425 return m_root.Exists(path);
433 inline void Remove(std::string_view path) {
558 return file.operator>>(str);
Configuration document (text or versioned binary).
Definition config.hxx:79
void operator<<(std::string_view str)
Initialize configuration from text.
const Item::Base & operator[](std::string_view path) const
Gets a const reference to an item by path.
Definition config.hxx:142
HookFunctions m_before_read_hooks
Hooks executed before reading.
Definition config.hxx:512
void OnParseFailure(FailureHook hook)
Sets a safe function to execute on parse failure.
Item::Base & Add(const Item::Base &item)
Adds an item to the configuration.
Definition config.hxx:378
StormByte::Size Size() const noexcept
Gets the number of items in the current level.
Definition config.hxx:450
void OnExistingAction(const StormByte::Config::OnExistingAction &on_existing)
Sets the action to take when an item name/identity collision occurs.
void AddHookAfterRead(ReadHook hook)
Adds a safe callback executed after a successful read.
std::ostream & operator>>(std::ostream &ostream) const
Output configuration serialized to an output stream (text).
Definition config.hxx:247
void operator<<(std::istream &istream)
Initialize configuration from an input stream (text mode).
Definition config.hxx:200
const Item::Base & operator[](const StormByte::Size &index) const
Gets a const reference to an item by index.
Definition config.hxx:162
Config & operator=(Config &&config) noexcept
Move assignment operator.
Item::Base & Add(Item::Base &&item)
Adds an item to the configuration (move).
Definition config.hxx:388
std::string & operator>>(std::string &str) const
Append serialized text to a caller-owned string.
Definition config.hxx:257
void Save(std::ostream &stream, Mode mode=Mode::Text) const
Write this document to an output stream.
Definition config.hxx:303
Config & operator<<(const Config &source)
Import data from another configuration.
static ExpectedConfig Load(std::istream &stream, Mode mode=Mode::Text)
Read a document from a caller-owned stream.
Definition config.hxx:335
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:171
void Remove(const StormByte::Size &index)
Removes an item by index.
Definition config.hxx:442
Item::Group m_root
Root group.
Definition config.hxx:510
OptionalFailureHook m_on_parse_failure_hook
Callback executed on failure, empty if unset.
Definition config.hxx:514
bool Exists(std::string_view path) const
Checks if an item exists by path.
Definition config.hxx:424
bool operator!=(const Config &config) const noexcept
Inequality operator.
Definition config.hxx:180
Item::Base & Add(Item::Base::PointerType item, const StormByte::Config::OnExistingAction &on_existing)
Adds an item pointer using an explicit policy.
Definition config.hxx:408
StormByte::Safe::Binary Binary() const
Serialize the document to Base-owned binary bytes.
void Clear() noexcept
Clears all configuration items.
Definition config.hxx:415
void Remove(std::string_view path)
Removes an item by path.
Definition config.hxx:433
virtual ~Config() noexcept
Destructor.
void AddHookBeforeRead(ReadHook hook)
Adds a safe callback executed before reading starts.
Item::Base & operator[](const StormByte::Size &index)
Gets a reference to an item by index.
Definition config.hxx:152
Item::Base & Add(Item::Base::PointerType item)
Adds an item pointer to the configuration.
Definition config.hxx:398
friend Config & operator>>(std::istream &istream, Config &file)
Initializes configuration when istream is on the left-hand side.
Definition config.hxx:524
StormByte::Config::OnExistingAction m_on_existing_action
Collision policy.
Definition config.hxx:515
const StormByte::Safe::Vector< Item::Base::PointerType > & Items() noexcept
Items in the current level.
Definition config.hxx:466
Config & operator=(const Config &config)
Copy assignment operator.
Config()
Default constructor.
static ExpectedConfig Load(const StormByte::Safe::Binary &data)
Load a document from Base-owned binary bytes.
Config(Config &&config) noexcept
Move constructor.
const StormByte::Safe::Vector< Item::Base::PointerType > & Items() const noexcept
Items in the current level.
Definition config.hxx:474
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:458
static ExpectedConfig Load(std::string_view text)
Load a document from text.
HookFunctions m_after_read_hooks
Hooks executed after successful reading.
Definition config.hxx:513
Base class for all configuration items.
Definition base.hxx:66
Named container ({ ... }).
Definition group.hxx:57
virtual PointerType Clone() const=0
STORMBYTE_DECLARE_MAYBE_SAFE(StormByte::Config::Config)
All the classes for item configuration.
Config module of the StormByte suite.
Definition alias.hxx:51
StormByte::Safe::Function< void(StormByte::Safe::Shared< Item::Group >)> ReadHook
Safe read callback receiving a Base-owned mutable group handle.
Definition hook.hxx:62
StormByte::Expected< Config, StormByte::Exception > ExpectedConfig
Result of loading a configuration document (text or binary).
Definition typedefs.hxx:83
StormByte::Safe::Shared< FailureHook > OptionalFailureHook
Optional parse-failure callback.
Definition alias.hxx:53
OnExistingAction
Forward declaration of the configuration document.
Definition typedefs.hxx:56
StormByte::Safe::Vector< StormByte::Safe::Shared< ReadHook > > HookFunctions
Read callbacks owned by Config.
Definition alias.hxx:52
std::ostream & operator<<(std::ostream &ostream, const Config &file)
Output configuration when ostream is on the left-hand side.
Definition config.hxx:546
StormByte::Safe::Function< bool(StormByte::Safe::Shared< const Item::Group >)> FailureHook
Safe parse-failure callback receiving a Base-owned read-only group handle.
Definition hook.hxx:67
Mode
Serialization / deserialization mode for configuration documents.
Definition typedefs.hxx:72
@ Text
Human-readable configuration syntax (UTF-8 text).
Definition typedefs.hxx:73
Config & operator>>(std::istream &istream, Config &file)
Initializes configuration when istream is on the left-hand side.
Definition config.hxx:524
auto Unexpected(Safe::Shared< E > error)
#define STORMBYTE_CONFIG_PUBLIC
Definition visibility.h:53