StormByte C++ Library: Config module 1.0.1.9999
StormByte-Config is the configuration module of the StormByte C++ suite.
Loading...
Searching...
No Matches
container.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/exception.hxx>
23#include <StormByte/config/item/base.hxx>
24#include <StormByte/config/typedefs.hxx>
25
26#include <queue>
27#include <span>
28#include <vector>
29
38 class STORMBYTE_CONFIG_PUBLIC Container: public Base {
39 public:
47 Container() = default;
48
53 Container(const std::string& name);
54
59 Container(std::string&& name);
60
65 Container(const Container& base) = default;
66
71 Container(Container&& base) noexcept = default;
72
78 Container& operator=(const Container& base) = default;
79
85 Container& operator=(Container&& base) noexcept = default;
86
90 virtual ~Container() noexcept override = default;
102 bool Equals(const Base& other) const noexcept override;
103
110 Base& operator[](const size_t& index);
111
118 const Base& operator[](const size_t& index) const;
119
127 Base& operator[](const std::string& path);
128
136 inline const Base& operator[](const std::string& path) const {
137 return LookUp(path);
138 }
139
145 bool operator==(const Container& container) const noexcept;
146
152 inline bool operator!=(const Container& container) const noexcept {
153 return !operator==(container);
154 }
155
161 std::string Serialize(const int& indent_level) const noexcept override;
162
168 static constexpr std::pair<const char, const char> EnclosureCharacters(const ContainerType& type) noexcept {
169 switch (type) {
170 case ContainerType::Group: return {'{', '}'};
171 case ContainerType::List: return {'[', ']'};
172 default: return {'\0', '\0'};
173 }
174 }
175
181 static constexpr const char EndCharacter(const ContainerType& type) noexcept {
182 switch (type) {
183 case ContainerType::Group: return '}';
184 case ContainerType::List: return ']';
185 default: return '\0';
186 }
187 }
188
193 constexpr virtual Item::ContainerType ContainerType() const noexcept = 0;
194
199 constexpr std::string ContainerTypeToString() const noexcept {
200 return Item::TypeToString(this->ContainerType());
201 }
202
207 constexpr Item::Type Type() const noexcept override {
208 return Item::Type::Container;
209 }
222 inline Base& Add(const Base& item, const StormByte::Config::OnExistingAction& on_existing) {
223 return Add(std::move(*item.Clone()), on_existing);
224 }
225
232 inline Base& Add(Base&& item, const StormByte::Config::OnExistingAction& on_existing) {
233 return Add(item.Move(), on_existing);
234 }
235
241 inline Base& Add(const Base& item) {
242 return Add(std::move(*item.Clone()), m_on_existing_action);
243 }
244
250 inline Base& Add(Base&& item) {
251 return Add(item.Move(), m_on_existing_action);
252 }
253
260 Base& Add(Base::PointerType item, const StormByte::Config::OnExistingAction& on_existing);
261
265 inline void Clear() noexcept {
266 m_items.clear();
267 }
268
274 bool Exists(const std::string& path) const;
275
281 void Remove(const size_t& index);
282
289 void Remove(const std::string& path);
290
295 constexpr std::span<Base::PointerType> Items() noexcept {
296 return std::span(m_items);
297 }
298
303 constexpr std::span<const Base::PointerType> Items() const noexcept {
304 return std::span(m_items);
305 }
306
311 constexpr size_t Size() const noexcept {
312 return m_items.size();
313 }
314
319 size_t Count() const noexcept;
330 void SetOnExistingAction(StormByte::Config::OnExistingAction action) noexcept {
331 m_on_existing_action = action;
332 }
333
339 return m_on_existing_action;
340 }
343 protected:
344 std::vector<Base::PointerType> m_items;
346
353 virtual Base::PointerType BeforeAdditionActions(Base::PointerType item, const StormByte::Config::OnExistingAction onexisting) = 0;
354
355 private:
361 virtual std::string ContentsToString(const int& level) const noexcept;
362
368 static bool IsPathValid(const std::string& name) noexcept;
369
375 const Base& LookUp(const std::string& path) const;
376
382 const Base& LookUp(std::queue<std::string>& path) const;
383
388 void Remove(std::queue<std::string>& path);
389 };
390}
Configuration document (text or versioned binary).
Definition config.hxx:54
The base class for all configuration items.
Definition base.hxx:44
Holds child items. Group and List derive from this.
Definition container.hxx:38
Base & Add(Base &&item)
Adds an item using the container's current OnExistingAction policy (move).
Definition container.hxx:250
constexpr size_t Size() const noexcept
Gets the number of items in the current level.
Definition container.hxx:311
void Remove(const std::string &path)
Removes an item by path.
constexpr Item::Type Type() const noexcept override
Gets the item type.
Definition container.hxx:207
constexpr std::span< Base::PointerType > Items() noexcept
Returns a span of all items in the container.
Definition container.hxx:295
Container & operator=(Container &&base) noexcept=default
Move assignment operator.
Container()=default
Constructs an empty Container.
Container & operator=(const Container &base)=default
Copy assignment operator.
virtual Base::PointerType BeforeAdditionActions(Base::PointerType item, const StormByte::Config::OnExistingAction onexisting)=0
Actions performed before adding an item.
Base & Add(const Base &item, const StormByte::Config::OnExistingAction &on_existing)
Adds an item (const reference) using an explicit policy.
Definition container.hxx:222
Base & Add(const Base &item)
Adds an item using the container's current OnExistingAction policy.
Definition container.hxx:241
StormByte::Config::OnExistingAction GetOnExistingAction() const noexcept
Gets the current OnExistingAction policy.
Definition container.hxx:338
void Clear() noexcept
Clears all items from the container.
Definition container.hxx:265
Base & Add(Base &&item, const StormByte::Config::OnExistingAction &on_existing)
Adds an item (rvalue) using an explicit policy.
Definition container.hxx:232
static constexpr const char EndCharacter(const ContainerType &type) noexcept
Returns the closing character for a given container type.
Definition container.hxx:181
static constexpr std::pair< const char, const char > EnclosureCharacters(const ContainerType &type) noexcept
Returns the enclosure characters for a given container type.
Definition container.hxx:168
void Remove(const size_t &index)
Removes an item by index.
constexpr std::span< const Base::PointerType > Items() const noexcept
Returns a const span of all items in the container.
Definition container.hxx:303
std::string Serialize(const int &indent_level) const noexcept override
Serializes the container to a string.
Container(Container &&base) noexcept=default
Move constructor.
std::vector< Base::PointerType > m_items
Items stored in the container.
Definition container.hxx:344
virtual constexpr Item::ContainerType ContainerType() const noexcept=0
Gets the container type.
bool operator==(const Container &container) const noexcept
Equality operator.
Container(const std::string &name)
Constructs a Container with the given name.
size_t Count() const noexcept
Gets the total number of items including nested ones.
bool Exists(const std::string &path) const
Checks if an item exists by path.
virtual ~Container() noexcept override=default
Destructor.
Container(std::string &&name)
Constructs a Container with the given name (move).
Base & Add(Base::PointerType item, const StormByte::Config::OnExistingAction &on_existing)
Adds an item to the container.
Container(const Container &base)=default
Copy constructor.
bool operator!=(const Container &container) const noexcept
Inequality operator.
Definition container.hxx:152
Configuration items (values, comments, groups, lists).
Definition alias.hxx:30
enum STORMBYTE_CONFIG_PUBLIC ContainerType
Group of named items.
Definition type.hxx:95
Type
Represents the type of a configuration item.
Definition type.hxx:39
OnExistingAction
Forward declaration of the configuration document.
Definition typedefs.hxx:35
@ ThrowException
Throw an exception to indicate the conflict.