StormByte-Config 2.0.0
C++26 configuration-document module of the StormByte suite
 
Loading...
Searching...
No Matches
container.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
46#include <StormByte/size.hxx>
48
49#include <queue>
50#include <span>
51#include <string_view>
52#include <vector>
53
63 public:
72
78
84
89 Container(const Container& base);
90
95 Container(Container&& base) noexcept;
96
103
109 Container& operator=(Container&& base) noexcept;
110
114 virtual ~Container() noexcept override;
126 bool Equals(const Base& other) const noexcept override;
127
134 Base& operator[](const StormByte::Size& index);
135
142 const Base& operator[](const StormByte::Size& index) const;
143
151 Base& operator[](const StormByte::String::String& path);
152
160 inline const Base& operator[](const StormByte::String::String& path) const {
161 return LookUp(path);
162 }
163
169 inline Base& operator[](std::string_view path) {
170 return operator[](StormByte::String::String(path));
171 }
172
178 inline const Base& operator[](std::string_view path) const {
179 return operator[](StormByte::String::String(path));
180 }
181
187 bool operator==(const Container& container) const noexcept;
188
194 inline bool operator!=(const Container& container) const noexcept {
195 return !operator==(container);
196 }
197
203 StormByte::String::String Serialize(const int& indent_level) const noexcept override;
204
210 static constexpr std::pair<const char, const char> EnclosureCharacters(const ContainerType& type) noexcept {
211 switch (type) {
212 case ContainerType::Group: return {'{', '}'};
213 case ContainerType::List: return {'[', ']'};
214 default: return {'\0', '\0'};
215 }
216 }
217
223 static constexpr const char EndCharacter(const ContainerType& type) noexcept {
224 switch (type) {
225 case ContainerType::Group: return '}';
226 case ContainerType::List: return ']';
227 default: return '\0';
228 }
229 }
230
235 constexpr virtual Item::ContainerType ContainerType() const noexcept = 0;
236
241 constexpr std::string_view ContainerTypeToString() const noexcept {
242 return Item::TypeToString(this->ContainerType());
243 }
244
249 constexpr Item::Type Type() const noexcept override {
250 return Item::Type::Container;
251 }
264 inline Base& Add(const Base& item, const StormByte::Config::OnExistingAction& on_existing) {
265 return Add(std::move(*item.Clone()), on_existing);
266 }
267
274 inline Base& Add(Base&& item, const StormByte::Config::OnExistingAction& on_existing) {
275 return Add(item.Move(), on_existing);
276 }
277
283 inline Base& Add(const Base& item) {
284 return Add(std::move(*item.Clone()), m_on_existing_action);
285 }
286
292 inline Base& Add(Base&& item) {
293 return Add(item.Move(), m_on_existing_action);
294 }
295
301 inline Base& Add(Base::PointerType item) {
302 return Add(item, m_on_existing_action);
303 }
304
312
316 void Clear() noexcept;
317
323 bool Exists(const StormByte::String::String& path) const;
324
330 inline bool Exists(std::string_view path) const {
331 return Exists(StormByte::String::String(path));
332 }
333
339 void Remove(const StormByte::Size& index);
340
348
353 inline void Remove(std::string_view path) {
354 Remove(StormByte::String::String(path));
355 }
356
361 constexpr std::span<const Base::PointerType> Items() noexcept {
362 return std::span<const Base::PointerType>(m_items);
363 }
364
369 constexpr std::span<const Base::PointerType> Items() const noexcept {
370 return std::span<const Base::PointerType>(m_items);
371 }
372
377 StormByte::Size Size() const noexcept;
378
383 StormByte::Size Count() const noexcept;
394 void SetOnExistingAction(StormByte::Config::OnExistingAction action) noexcept {
395 m_on_existing_action = action;
396 }
397
403 return m_on_existing_action;
404 }
407 protected:
408 std::vector<Base::PointerType> m_items;
410
418
419 private:
425 virtual StormByte::String::String ContentsToString(const int& level) const noexcept;
426
432 static bool IsPathValid(const StormByte::String::String& name) noexcept;
433
439 const Base& LookUp(const StormByte::String::String& path) const;
440
446 const Base& LookUp(std::queue<StormByte::String::String>& path) const;
447
452 void Remove(std::queue<StormByte::String::String>& path);
453 };
454}
virtual PointerType Clone() const=0
Configuration document (text or versioned binary).
Definition config.hxx:77
Base class for all configuration items.
Definition base.hxx:66
Holds child items.
Definition container.hxx:62
Base & Add(Base &&item)
Adds an item using the container's current OnExistingAction policy (move).
Definition container.hxx:292
Container()
Constructs an empty Container.
constexpr Item::Type Type() const noexcept override
Gets the item type.
Definition container.hxx:249
constexpr std::span< const Base::PointerType > Items() noexcept
Items in this level.
Definition container.hxx:361
Base & operator[](std::string_view path)
Gets a reference to an item by a path view.
Definition container.hxx:169
Container(const Container &base)
Copy constructor.
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:264
Base & Add(const Base &item)
Adds an item using the container's current OnExistingAction policy.
Definition container.hxx:283
Container(StormByte::String::String &&name)
Constructs a Container with the given name (move).
Container(const StormByte::String::String &name)
Constructs a Container with the given name.
StormByte::Config::OnExistingAction GetOnExistingAction() const noexcept
Gets the current OnExistingAction policy.
Definition container.hxx:402
StormByte::Size Size() const noexcept
Gets the number of items in the current level.
void Clear() noexcept
Clears all items from the container.
Base & Add(Base &&item, const StormByte::Config::OnExistingAction &on_existing)
Adds an item (rvalue) using an explicit policy.
Definition container.hxx:274
static constexpr const char EndCharacter(const ContainerType &type) noexcept
Returns the closing character for a given container type.
Definition container.hxx:223
const Base & operator[](std::string_view path) const
Gets a const reference to an item by a path view.
Definition container.hxx:178
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:210
Base & Add(Base::PointerType item)
Adds an item to the container using current policy.
Definition container.hxx:301
constexpr std::span< const Base::PointerType > Items() const noexcept
Items in this level.
Definition container.hxx:369
Container(Container &&base) noexcept
Move constructor.
Container & operator=(const Container &base)
Copy assignment operator.
std::vector< Base::PointerType > m_items
Items stored in the container.
Definition container.hxx:408
virtual constexpr Item::ContainerType ContainerType() const noexcept=0
Gets the container type.
void Remove(const StormByte::Size &index)
Removes an item by index.
Container & operator=(Container &&base) noexcept
Move assignment operator.
bool operator==(const Container &container) const noexcept
Equality operator.
virtual ~Container() noexcept override
Destructor.
Base & Add(Base::PointerType item, const StormByte::Config::OnExistingAction &on_existing)
Adds an item to the container.
StormByte::String::String Serialize(const int &indent_level) const noexcept override
Serializes the container to a string.
void Remove(std::string_view path)
Removes an item by a path view.
Definition container.hxx:353
void Remove(const StormByte::String::String &path)
Removes an item by path.
bool operator!=(const Container &container) const noexcept
Inequality operator.
Definition container.hxx:194
Configuration items (values, comments, groups, lists).
Definition hook.hxx:50
Type
Node kind in the document tree.
Definition type.hxx:57
ContainerType
Group ({}) or list ([]).
Definition type.hxx:115
OnExistingAction
Forward declaration of the configuration document.
Definition typedefs.hxx:56
@ ThrowException
Throw an exception to indicate the conflict.
bool operator==(std::span< const std::byte > bytes, const BinaryData &data) noexcept
STL namespace.
#define STORMBYTE_CONFIG_PUBLIC
Definition visibility.h:53