2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
4 * This file is part of StormByte-Config.
6 * StormByte-Config original source is dual-licensed:
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)
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>).
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.
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.
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.
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>.
38 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
43#include <StormByte/config/item/comment.hxx>
44#include <StormByte/config/item/group.hxx>
45#include <StormByte/config/item/list.hxx>
46#include <StormByte/config/item/value.hxx>
47#include <StormByte/safe/binary.hxx>
48#include <StormByte/type_traits.hxx>
52namespace StormByte::Config::Item {
54 [[noreturn]] void FailAs(const Base& item, const char* wanted) {
55 throw Exception("Item {} is not {}", static_cast<std::string_view>(item.Name()), wanted);
58 Value& RequireValue(Base& item) {
59 if (item.Type() != Item::Type::Integer && item.Type() != Item::Type::Double &&
60 item.Type() != Item::Type::Bool && item.Type() != Item::Type::String &&
61 item.Type() != Item::Type::Binary)
62 FailAs(item, "Value");
63 return static_cast<Value&>(item);
66 const Value& RequireValue(const Base& item) {
67 if (item.Type() != Item::Type::Integer && item.Type() != Item::Type::Double &&
68 item.Type() != Item::Type::Bool && item.Type() != Item::Type::String &&
69 item.Type() != Item::Type::Binary)
70 FailAs(item, "Value");
71 return static_cast<const Value&>(item);
76 decltype(auto) Base::As() {
77 if constexpr (StormByte::Type::SameAs<T, Value>) {
78 return RequireValue(*this);
79 } else if constexpr (StormByte::Type::SameAs<T, Container>) {
80 if (this->Type() != Item::Type::Container)
81 FailAs(*this, "Container");
82 return static_cast<Container&>(*this);
83 } else if constexpr (StormByte::Type::SameAs<T, Group>) {
84 if (this->Type() != Item::Type::Container)
85 FailAs(*this, "Group");
86 auto& container = static_cast<Container&>(*this);
87 if (container.ContainerType() != Item::ContainerType::Group)
88 FailAs(*this, "Group");
89 return static_cast<Group&>(*this);
90 } else if constexpr (StormByte::Type::SameAs<T, List>) {
91 if (this->Type() != Item::Type::Container)
92 FailAs(*this, "List");
93 auto& container = static_cast<Container&>(*this);
94 if (container.ContainerType() != Item::ContainerType::List)
95 FailAs(*this, "List");
96 return static_cast<List&>(*this);
97 } else if constexpr (StormByte::Type::SameAs<T, Integer>) {
98 return static_cast<int&>(RequireValue(*this));
99 } else if constexpr (StormByte::Type::SameAs<T, Double>) {
100 return static_cast<double>(RequireValue(*this));
101 } else if constexpr (StormByte::Type::SameAs<T, Bool>) {
102 return static_cast<bool&>(RequireValue(*this));
103 } else if constexpr (StormByte::Type::SameAs<T, Text>) {
104 return static_cast<StormByte::Safe::String&>(RequireValue(*this));
105 } else if constexpr (StormByte::Type::SameAs<T, Binary>) {
106 return static_cast<StormByte::Safe::Binary&>(RequireValue(*this));
107 } else if constexpr (requires { T::CommentTag; }) {
108 if (this->Type() != Item::Type::Comment)
109 FailAs(*this, "Comment");
110 return static_cast<T&>(*this);
112 static_assert(StormByte::Type::SameAs<T, Value>, "Unsupported As<T>");
117 decltype(auto) Base::As() const {
118 if constexpr (StormByte::Type::SameAs<T, Value>) {
119 return RequireValue(*this);
120 } else if constexpr (StormByte::Type::SameAs<T, Container>) {
121 if (this->Type() != Item::Type::Container)
122 FailAs(*this, "Container");
123 return static_cast<const Container&>(*this);
124 } else if constexpr (StormByte::Type::SameAs<T, Group>) {
125 if (this->Type() != Item::Type::Container)
126 FailAs(*this, "Group");
127 const auto& container = static_cast<const Container&>(*this);
128 if (container.ContainerType() != Item::ContainerType::Group)
129 FailAs(*this, "Group");
130 return static_cast<const Group&>(*this);
131 } else if constexpr (StormByte::Type::SameAs<T, List>) {
132 if (this->Type() != Item::Type::Container)
133 FailAs(*this, "List");
134 const auto& container = static_cast<const Container&>(*this);
135 if (container.ContainerType() != Item::ContainerType::List)
136 FailAs(*this, "List");
137 return static_cast<const List&>(*this);
138 } else if constexpr (StormByte::Type::SameAs<T, Integer>) {
139 return static_cast<const int&>(RequireValue(*this));
140 } else if constexpr (StormByte::Type::SameAs<T, Double>) {
141 return static_cast<double>(RequireValue(*this));
142 } else if constexpr (StormByte::Type::SameAs<T, Bool>) {
143 return static_cast<const bool&>(RequireValue(*this));
144 } else if constexpr (StormByte::Type::SameAs<T, Text>) {
145 return static_cast<const StormByte::Safe::String&>(RequireValue(*this));
146 } else if constexpr (StormByte::Type::SameAs<T, Binary>) {
147 return static_cast<const StormByte::Safe::Binary&>(RequireValue(*this));
148 } else if constexpr (requires { T::CommentTag; }) {
149 if (this->Type() != Item::Type::Comment)
150 FailAs(*this, "Comment");
151 return static_cast<const T&>(*this);
153 static_assert(StormByte::Type::SameAs<T, Value>, "Unsupported As<T>");