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. The bundled StormByte Base tree
22 * under thirdparty/ remains under its own license.
23 *
24 * Neither license grants any patent rights. Any patent licenses required
25 * to use this software or third-party components must be obtained separately
26 * from the patent holders.
27 *
28 * StormByte-Config is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU Lesser General Public License for more details.
32 *
33 * You should have received a copy of the GNU Lesser General Public License
34 * version 3 along with StormByte-Config. If not, see
35 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
36 *
37 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
38 */
39
40#pragma once
41
45#include <StormByte/size.hxx>
47
48#include <queue>
49#include <span>
50#include <string_view>
51#include <vector>
52
62 public:
71
77
83
88 Container(const Container& base);
89
94 Container(Container&& base) noexcept;
95
102
108 Container& operator=(Container&& base) noexcept;
109
113 virtual ~Container() noexcept override;
125 bool Equals(const Base& other) const noexcept override;
126
133 Base& operator[](const StormByte::Size& index);
134
141 const Base& operator[](const StormByte::Size& index) const;
142
150 Base& operator[](const StormByte::Safe::String& path);
151
159 inline const Base& operator[](const StormByte::Safe::String& path) const {
160 return LookUp(path);
161 }
162
168 inline Base& operator[](std::string_view path) {
169 return operator[](StormByte::Safe::String(path));
170 }
171
177 inline const Base& operator[](std::string_view path) const {
178 return operator[](StormByte::Safe::String(path));
179 }
180
186 bool operator==(const Container& container) const noexcept;
187
193 inline bool operator!=(const Container& container) const noexcept {
194 return !operator==(container);
195 }
196
202 StormByte::Safe::String Serialize(const int& indent_level) const noexcept override;
203
209 static constexpr std::pair<const char, const char> EnclosureCharacters(const ContainerType& type) noexcept {
210 switch (type) {
211 case ContainerType::Group: return {'{', '}'};
212 case ContainerType::List: return {'[', ']'};
213 default: return {'\0', '\0'};
214 }
215 }
216
222 static constexpr const char EndCharacter(const ContainerType& type) noexcept {
223 switch (type) {
224 case ContainerType::Group: return '}';
225 case ContainerType::List: return ']';
226 default: return '\0';
227 }
228 }
229
234 constexpr virtual Item::ContainerType ContainerType() const noexcept = 0;
235
240 constexpr std::string_view ContainerTypeToString() const noexcept {
241 return Item::TypeToString(this->ContainerType());
242 }
243
248 constexpr Item::Type Type() const noexcept override {
249 return Item::Type::Container;
250 }
263 inline Base& Add(const Base& item, const StormByte::Config::OnExistingAction& on_existing) {
264 return Add(std::move(*item.Clone()), on_existing);
265 }
266
273 inline Base& Add(Base&& item, const StormByte::Config::OnExistingAction& on_existing) {
274 return Add(item.Move(), on_existing);
275 }
276
282 inline Base& Add(const Base& item) {
283 return Add(std::move(*item.Clone()), m_on_existing_action);
284 }
285
291 inline Base& Add(Base&& item) {
292 return Add(item.Move(), m_on_existing_action);
293 }
294
300 inline Base& Add(Base::PointerType item) {
301 return Add(item, m_on_existing_action);
302 }
303
311
315 void Clear() noexcept;
316
322 bool Exists(const StormByte::Safe::String& path) const;
323
329 inline bool Exists(std::string_view path) const {
330 return Exists(StormByte::Safe::String(path));
331 }
332
338 void Remove(const StormByte::Size& index);
339
347
352 inline void Remove(std::string_view path) {
353 Remove(StormByte::Safe::String(path));
354 }
355
360 constexpr std::span<const Base::PointerType> Items() noexcept {
361 return std::span<const Base::PointerType>(m_items);
362 }
363
368 constexpr std::span<const Base::PointerType> Items() const noexcept {
369 return std::span<const Base::PointerType>(m_items);
370 }
371
376 StormByte::Size Size() const noexcept;
377
382 StormByte::Size Count() const noexcept;
393 void SetOnExistingAction(StormByte::Config::OnExistingAction action) noexcept {
394 m_on_existing_action = action;
395 }
396
402 return m_on_existing_action;
403 }
406 protected:
407 std::vector<Base::PointerType> m_items;
409
417
418 private:
424 virtual StormByte::Safe::String ContentsToString(const int& level) const noexcept;
425
431 static bool IsPathValid(const StormByte::Safe::String& name) noexcept;
432
438 const Base& LookUp(const StormByte::Safe::String& path) const;
439
445 const Base& LookUp(std::queue<StormByte::Safe::String>& path) const;
446
451 void Remove(std::queue<StormByte::Safe::String>& path);
452 };
453}
Configuration document (text or versioned binary).
Definition config.hxx:76
Base class for all configuration items.
Definition base.hxx:65
Holds child items.
Definition container.hxx:61
Base & Add(Base &&item)
Adds an item using the container's current OnExistingAction policy (move).
Definition container.hxx:291
Container()
Constructs an empty Container.
constexpr Item::Type Type() const noexcept override
Gets the item type.
Definition container.hxx:248
StormByte::Safe::String Serialize(const int &indent_level) const noexcept override
Serializes the container to a string.
constexpr std::span< const Base::PointerType > Items() noexcept
Items in this level.
Definition container.hxx:360
Base & operator[](std::string_view path)
Gets a reference to an item by a path view.
Definition container.hxx:168
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:263
Base & Add(const Base &item)
Adds an item using the container's current OnExistingAction policy.
Definition container.hxx:282
StormByte::Config::OnExistingAction GetOnExistingAction() const noexcept
Gets the current OnExistingAction policy.
Definition container.hxx:401
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:273
static constexpr const char EndCharacter(const ContainerType &type) noexcept
Returns the closing character for a given container type.
Definition container.hxx:222
const Base & operator[](std::string_view path) const
Gets a const reference to an item by a path view.
Definition container.hxx:177
Container(const StormByte::Safe::String &name)
Constructs a Container with the given name.
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:209
Base & Add(Base::PointerType item)
Adds an item to the container using current policy.
Definition container.hxx:300
Container(StormByte::Safe::String &&name)
Constructs a Container with the given name (move).
constexpr std::span< const Base::PointerType > Items() const noexcept
Items in this level.
Definition container.hxx:368
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:407
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.
void Remove(const StormByte::Safe::String &path)
Removes an item by path.
virtual ~Container() noexcept override
Destructor.
Base & Add(Base::PointerType item, const StormByte::Config::OnExistingAction &on_existing)
Adds an item to the container.
void Remove(std::string_view path)
Removes an item by a path view.
Definition container.hxx:352
bool operator!=(const Container &container) const noexcept
Inequality operator.
Definition container.hxx:193
virtual PointerType Clone() const=0
Configuration items (values, comments, groups, lists).
Definition hook.hxx:49
Type
Node kind in the document tree.
Definition type.hxx:56
ContainerType
Group ({}) or list ([]).
Definition type.hxx:114
OnExistingAction
Forward declaration of the configuration document.
Definition typedefs.hxx:55
@ 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:52