StormByte-Config 3.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>
47#include <StormByte/safe/queue.hxx>
48#include <StormByte/safe/string.hxx>
49#include <StormByte/safe/vector.hxx>
50
51#include <string_view>
52#include <utility>
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;
116
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[](std::string_view path);
152
160 const Base& operator[](std::string_view path) const;
161
167 bool operator==(const Container& container) const noexcept;
168
174 inline bool operator!=(const Container& container) const noexcept {
175 return !operator==(container);
176 }
177
183 StormByte::Safe::String Serialize(const int& indent_level) const noexcept override;
184
190 static STORMBYTE_FORCE_INLINE constexpr std::pair<const char, const char> EnclosureCharacters(const ContainerType& type) noexcept {
191 switch (type) {
192 case ContainerType::Group: return {'{', '}'};
193 case ContainerType::List: return {'[', ']'};
194 default: return {'\0', '\0'};
195 }
196 }
197
203 static constexpr const char EndCharacter(const ContainerType& type) noexcept {
204 switch (type) {
205 case ContainerType::Group: return '}';
206 case ContainerType::List: return ']';
207 default: return '\0';
208 }
209 }
210
215 constexpr virtual Item::ContainerType ContainerType() const noexcept = 0;
216
221 STORMBYTE_FORCE_INLINE constexpr std::string_view ContainerTypeToString() const noexcept {
222 return Item::TypeToString(this->ContainerType());
223 }
224
229 constexpr Item::Type Type() const noexcept override {
231 }
232
233
244 inline Base& Add(const Base& item, const StormByte::Config::OnExistingAction& on_existing) {
245 return Add(std::move(*item.Clone()), on_existing);
246 }
247
254 inline Base& Add(Base&& item, const StormByte::Config::OnExistingAction& on_existing) {
255 return Add(item.Move(), on_existing);
256 }
257
263 inline Base& Add(const Base& item) {
264 return Add(std::move(*item.Clone()), m_on_existing_action);
265 }
266
272 inline Base& Add(Base&& item) {
273 return Add(item.Move(), m_on_existing_action);
274 }
275
281 inline Base& Add(Base::PointerType item) {
282 return Add(item, m_on_existing_action);
283 }
284
292
296 void Clear() noexcept;
297
303 bool Exists(std::string_view path) const;
304
310 void Remove(const StormByte::Size& index);
311
318 void Remove(std::string_view path);
319
324 const StormByte::Safe::Vector<Base::PointerType>& Items() noexcept {
325 return m_items;
326 }
327
333 return m_items;
334 }
335
340 StormByte::Size Size() const noexcept;
341
346 StormByte::Size Count() const noexcept;
348
358 m_on_existing_action = action;
359 }
360
368
369
370 protected:
373
381
382 private:
388 virtual StormByte::Safe::String ContentsToString(const int& level) const noexcept;
389
395 static bool IsPathValid(const StormByte::Safe::String& name) noexcept;
396
402 const Base& LookUp(const StormByte::Safe::String& path) const;
403
409 const Base& LookUp(StormByte::Safe::Queue<StormByte::Safe::String>& path) const;
410
416 };
417}
418
Configuration document (text or versioned binary).
Definition config.hxx:79
Base() noexcept
Constructor.
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:272
Container()
Constructs an empty Container.
bool Equals(const Base &other) const noexcept override
Polymorphic equality comparison.
void SetOnExistingAction(StormByte::Config::OnExistingAction action) noexcept
Sets the action to take when an item with the same identity already exists.
Definition container.hxx:357
constexpr Item::Type Type() const noexcept override
Gets the item type.
Definition container.hxx:229
StormByte::Safe::String Serialize(const int &indent_level) const noexcept override
Serializes the container to a string.
const StormByte::Safe::Vector< Base::PointerType > & Items() const noexcept
Items in this level.
Definition container.hxx:332
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:244
Base & Add(const Base &item)
Adds an item using the container's current OnExistingAction policy.
Definition container.hxx:263
StormByte::Config::OnExistingAction GetOnExistingAction() const noexcept
Gets the current OnExistingAction policy.
Definition container.hxx:365
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:254
static constexpr const char EndCharacter(const ContainerType &type) noexcept
Returns the closing character for a given container type.
Definition container.hxx:203
StormByte::Safe::Vector< Base::PointerType > m_items
Items stored in the container.
Definition container.hxx:371
const StormByte::Safe::Vector< Base::PointerType > & Items() noexcept
Items in this level.
Definition container.hxx:324
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:190
Base & Add(Base::PointerType item)
Adds an item to the container using current policy.
Definition container.hxx:281
Container(StormByte::Safe::String &&name)
Constructs a Container with the given name (move).
Container(Container &&base) noexcept
Move constructor.
Container & operator=(const Container &base)
Copy assignment operator.
virtual constexpr Item::ContainerType ContainerType() const noexcept=0
Gets the container type.
bool Exists(std::string_view path) const
Checks if an item exists by path.
void Remove(const StormByte::Size &index)
Removes an item by index.
StormByte::Size Count() const noexcept
Gets the total number of items including nested ones.
Container & operator=(Container &&base) noexcept
Move assignment operator.
bool operator==(const Container &container) const noexcept
Equality operator.
constexpr std::string_view ContainerTypeToString() const noexcept
Gets the container type as string.
Definition container.hxx:221
virtual ~Container() noexcept override
Destructor.
Base & Add(Base::PointerType item, const StormByte::Config::OnExistingAction &on_existing)
Adds an item to the container.
StormByte::Config::OnExistingAction m_on_existing_action
Collision policy.
Definition container.hxx:372
virtual PointerType Clone() const=0
STORMBYTE_DECLARE_MAYBE_SAFE(StormByte::Config::Config)
Configuration document items.
Definition base.hxx:55
Type
Node kind in the document tree.
Definition type.hxx:57
@ Container
Group or list.
Definition type.hxx:60
static constexpr std::string_view TypeToString(const Type &t) noexcept
Converts a Type to its string representation.
Definition type.hxx:73
ContainerType
Group ({}) or list ([]).
Definition type.hxx:115
@ Group
Group of named items.
Definition type.hxx:116
@ List
List of unnamed items.
Definition type.hxx:117
OnExistingAction
Forward declaration of the configuration document.
Definition typedefs.hxx:56
@ ThrowException
Throw an exception to indicate the conflict.
Definition typedefs.hxx:59
Root namespace of the StormByte suite.
STL namespace.
#define STORMBYTE_FORCE_INLINE
#define STORMBYTE_CONFIG_PUBLIC
Definition visibility.h:53