StormByte-Config 2.0.0
C++26 configuration-document module of the StormByte suite
 
Loading...
Searching...
No Matches
base.txx
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
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/type_traits.hxx>
48
49#include <string_view>
50
51namespace StormByte::Config::Item {
52 namespace {
53 [[noreturn]] void FailAs(const Base& item, const char* wanted) {
54 throw Exception("Item {} is not {}", static_cast<std::string_view>(item.Name()), wanted);
55 }
56
57 Value& RequireValue(Base& item) {
58 if (item.Type() != Item::Type::Integer && item.Type() != Item::Type::Double &&
59 item.Type() != Item::Type::Bool && item.Type() != Item::Type::String &&
60 item.Type() != Item::Type::Binary)
61 FailAs(item, "Value");
62 return static_cast<Value&>(item);
63 }
64
65 const Value& RequireValue(const Base& item) {
66 if (item.Type() != Item::Type::Integer && item.Type() != Item::Type::Double &&
67 item.Type() != Item::Type::Bool && item.Type() != Item::Type::String &&
68 item.Type() != Item::Type::Binary)
69 FailAs(item, "Value");
70 return static_cast<const Value&>(item);
71 }
72 }
73
74 template<class T>
75 decltype(auto) Base::As() {
76 if constexpr (StormByte::Type::SameAs<T, Value>) {
77 return RequireValue(*this);
78 } else if constexpr (StormByte::Type::SameAs<T, Container>) {
79 if (this->Type() != Item::Type::Container)
80 FailAs(*this, "Container");
81 return static_cast<Container&>(*this);
82 } else if constexpr (StormByte::Type::SameAs<T, Group>) {
83 if (this->Type() != Item::Type::Container)
84 FailAs(*this, "Group");
85 auto& container = static_cast<Container&>(*this);
86 if (container.ContainerType() != Item::ContainerType::Group)
87 FailAs(*this, "Group");
88 return static_cast<Group&>(*this);
89 } else if constexpr (StormByte::Type::SameAs<T, List>) {
90 if (this->Type() != Item::Type::Container)
91 FailAs(*this, "List");
92 auto& container = static_cast<Container&>(*this);
93 if (container.ContainerType() != Item::ContainerType::List)
94 FailAs(*this, "List");
95 return static_cast<List&>(*this);
96 } else if constexpr (StormByte::Type::SameAs<T, Integer>) {
97 return static_cast<int&>(RequireValue(*this));
98 } else if constexpr (StormByte::Type::SameAs<T, Double>) {
99 return static_cast<double>(RequireValue(*this));
100 } else if constexpr (StormByte::Type::SameAs<T, Bool>) {
101 return static_cast<bool&>(RequireValue(*this));
102 } else if constexpr (StormByte::Type::SameAs<T, Text>) {
103 return static_cast<StormByte::String::String&>(RequireValue(*this));
104 } else if constexpr (StormByte::Type::SameAs<T, Binary>) {
105 return static_cast<StormByte::BinaryData&>(RequireValue(*this));
106 } else if constexpr (requires { T::CommentTag; }) {
107 if (this->Type() != Item::Type::Comment)
108 FailAs(*this, "Comment");
109 return static_cast<T&>(*this);
110 } else {
111 static_assert(StormByte::Type::SameAs<T, Value>, "Unsupported As<T>");
112 }
113 }
114
115 template<class T>
116 decltype(auto) Base::As() const {
117 if constexpr (StormByte::Type::SameAs<T, Value>) {
118 return RequireValue(*this);
119 } else if constexpr (StormByte::Type::SameAs<T, Container>) {
120 if (this->Type() != Item::Type::Container)
121 FailAs(*this, "Container");
122 return static_cast<const Container&>(*this);
123 } else if constexpr (StormByte::Type::SameAs<T, Group>) {
124 if (this->Type() != Item::Type::Container)
125 FailAs(*this, "Group");
126 const auto& container = static_cast<const Container&>(*this);
127 if (container.ContainerType() != Item::ContainerType::Group)
128 FailAs(*this, "Group");
129 return static_cast<const Group&>(*this);
130 } else if constexpr (StormByte::Type::SameAs<T, List>) {
131 if (this->Type() != Item::Type::Container)
132 FailAs(*this, "List");
133 const auto& container = static_cast<const Container&>(*this);
134 if (container.ContainerType() != Item::ContainerType::List)
135 FailAs(*this, "List");
136 return static_cast<const List&>(*this);
137 } else if constexpr (StormByte::Type::SameAs<T, Integer>) {
138 return static_cast<const int&>(RequireValue(*this));
139 } else if constexpr (StormByte::Type::SameAs<T, Double>) {
140 return static_cast<double>(RequireValue(*this));
141 } else if constexpr (StormByte::Type::SameAs<T, Bool>) {
142 return static_cast<const bool&>(RequireValue(*this));
143 } else if constexpr (StormByte::Type::SameAs<T, Text>) {
144 return static_cast<const StormByte::String::String&>(RequireValue(*this));
145 } else if constexpr (StormByte::Type::SameAs<T, Binary>) {
146 return static_cast<const StormByte::BinaryData&>(RequireValue(*this));
147 } else if constexpr (requires { T::CommentTag; }) {
148 if (this->Type() != Item::Type::Comment)
149 FailAs(*this, "Comment");
150 return static_cast<const T&>(*this);
151 } else {
152 static_assert(StormByte::Type::SameAs<T, Value>, "Unsupported As<T>");
153 }
154 }
155}