2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
4 * This is the base for StormByte Suite libraries.
6 * StormByte 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 source in this repository.
20 * They do not cover other StormByte modules or any third-party material
21 * shipped with this repository (including everything under thirdparty/),
22 * which remains under its own license.
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.
28 * StormByte 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.
33 * You should have received a copy of the GNU Lesser General Public License
34 * version 3 along with StormByte. If not, see
35 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
37 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
40namespace StormByte::Safe {
41 template<Type::SafeValue... Ts>
42 requires (sizeof...(Ts) > 0)
44 constexpr std::size_t Variant<Ts...>::AlternativeIndex() noexcept {
45 std::size_t exact = npos;
46 std::size_t current = 0;
48 if constexpr (Type::SameAs<std::remove_cvref_t<T>, Ts>)
54 std::size_t found = npos;
56 bool ambiguous = false;
58 if constexpr (Type::ConstructibleFrom<Ts, T> && !Type::SameAs<std::remove_cvref_t<T>, Variant>) {
66 return ambiguous ? npos : found;
69 template<Type::SafeValue... Ts>
70 requires (sizeof...(Ts) > 0)
71 template<class Alternative, class... Args>
72 Alternative* Variant<Ts...>::Make(Args&&... args) {
73 void* block = Heap::Allocate(sizeof(Alternative));
75 return new (block) Alternative(std::forward<Args>(args)...);
82 template<Type::SafeValue... Ts>
83 requires (sizeof...(Ts) > 0)
84 void Variant<Ts...>::Release() noexcept {
85 if (m_storage == nullptr)
87 std::size_t current = 0;
88 auto destroy = [&](auto* unused) {
89 using Alternative = std::remove_pointer_t<decltype(unused)>;
90 if (current == m_index)
91 static_cast<Alternative*>(m_storage)->~Alternative();
94 (destroy(static_cast<Ts*>(nullptr)), ...);
95 Heap::Free(m_storage);
100 template<Type::SafeValue... Ts>
101 requires (sizeof...(Ts) > 0)
102 Variant<Ts...>::Variant(): m_storage(nullptr), m_index(npos) {
103 using First = std::tuple_element_t<0, std::tuple<Ts...>>;
104 m_storage = Make<First>();
108 template<Type::SafeValue... Ts>
109 requires (sizeof...(Ts) > 0)
110 template<std::size_t I, class... Args>
111 Variant<Ts...>::Variant(std::in_place_index_t<I>, Args&&... args): m_storage(nullptr), m_index(npos) {
112 using Alternative = std::tuple_element_t<I, std::tuple<Ts...>>;
113 m_storage = Make<Alternative>(std::forward<Args>(args)...);
117 template<Type::SafeValue... Ts>
118 requires (sizeof...(Ts) > 0)
119 template<class T, class... Args>
120 Variant<Ts...>::Variant(std::in_place_type_t<T>, Args&&... args): m_storage(nullptr), m_index(npos) {
121 m_storage = Make<T>(std::forward<Args>(args)...);
122 m_index = AlternativeIndex<T>();
125 template<Type::SafeValue... Ts>
126 requires (sizeof...(Ts) > 0)
127 Variant<Ts...>::Variant(const std::variant<Ts...>& value): m_storage(nullptr), m_index(npos) {
128 if (value.valueless_by_exception())
130 std::visit([&](const auto& alternative) {
131 using Alternative = std::remove_cvref_t<decltype(alternative)>;
132 m_storage = Make<Alternative>(alternative);
133 m_index = value.index();
137 template<Type::SafeValue... Ts>
138 requires (sizeof...(Ts) > 0)
139 Variant<Ts...>::Variant(std::variant<Ts...>&& value): m_storage(nullptr), m_index(npos) {
140 if (value.valueless_by_exception())
142 std::visit([&](auto& alternative) {
143 using Alternative = std::remove_cvref_t<decltype(alternative)>;
144 m_storage = Make<Alternative>(std::move(alternative));
145 m_index = value.index();
149 template<Type::SafeValue... Ts>
150 requires (sizeof...(Ts) > 0)
151 Variant<Ts...>::Variant(const Variant& other): m_storage(nullptr), m_index(npos) {
152 if (other.valueless_by_exception())
154 std::size_t current = 0;
155 auto copy = [&](auto* unused) {
156 using Alternative = std::remove_pointer_t<decltype(unused)>;
157 if (current == other.m_index) {
158 m_storage = Make<Alternative>(*static_cast<const Alternative*>(other.m_storage));
159 m_index = other.m_index;
163 (copy(static_cast<Ts*>(nullptr)), ...);
166 template<Type::SafeValue... Ts>
167 requires (sizeof...(Ts) > 0)
168 Variant<Ts...>::Variant(Variant&& other) noexcept: m_storage(other.m_storage), m_index(other.m_index) {
169 other.m_storage = nullptr;
170 other.m_index = npos;
173 template<Type::SafeValue... Ts>
174 requires (sizeof...(Ts) > 0)
175 Variant<Ts...>::~Variant() noexcept {
179 template<Type::SafeValue... Ts>
180 requires (sizeof...(Ts) > 0)
181 Variant<Ts...>& Variant<Ts...>::operator=(const Variant& other) {
184 Variant copied(other);
189 template<Type::SafeValue... Ts>
190 requires (sizeof...(Ts) > 0)
191 Variant<Ts...>& Variant<Ts...>::operator=(Variant&& other) noexcept {
192 if (this != &other) {
194 m_storage = other.m_storage;
195 m_index = other.m_index;
196 other.m_storage = nullptr;
197 other.m_index = npos;
202 template<Type::SafeValue... Ts>
203 requires (sizeof...(Ts) > 0)
204 std::size_t Variant<Ts...>::index() const noexcept {
208 template<Type::SafeValue... Ts>
209 requires (sizeof...(Ts) > 0)
210 bool Variant<Ts...>::valueless_by_exception() const noexcept {
211 return m_index == npos;
214 template<Type::SafeValue... Ts>
215 requires (sizeof...(Ts) > 0)
216 template<std::size_t I, class... Args>
217 auto& Variant<Ts...>::emplace(Args&&... args) {
218 using Alternative = std::tuple_element_t<I, std::tuple<Ts...>>;
219 Alternative* created = Make<Alternative>(std::forward<Args>(args)...);
226 template<Type::SafeValue... Ts>
227 requires (sizeof...(Ts) > 0)
228 template<class T, class... Args>
229 T& Variant<Ts...>::emplace(Args&&... args) {
230 T* created = Make<T>(std::forward<Args>(args)...);
233 m_index = AlternativeIndex<T>();
237 template<Type::SafeValue... Ts>
238 requires (sizeof...(Ts) > 0)
239 void Variant<Ts...>::swap(Variant& other) noexcept {
240 std::swap(m_storage, other.m_storage);
241 std::swap(m_index, other.m_index);
244 template<Type::SafeValue... Ts>
245 requires (sizeof...(Ts) > 0)
246 bool Variant<Ts...>::operator==(const Variant& other) const {
247 if (m_index != other.m_index)
249 if (valueless_by_exception())
252 std::size_t current = 0;
253 auto compare = [&](auto* unused) {
254 using Alternative = std::remove_pointer_t<decltype(unused)>;
255 if (current == m_index)
256 equal = *static_cast<const Alternative*>(m_storage) == *static_cast<const Alternative*>(other.m_storage);
259 (compare(static_cast<Ts*>(nullptr)), ...);
263 template<Type::SafeValue... Ts>
264 requires (sizeof...(Ts) > 0)
265 STORMBYTE_FORCE_INLINE std::strong_ordering Variant<Ts...>::operator<=>(const Variant& other) const {
266 if (m_index != other.m_index)
267 return m_index <=> other.m_index;
268 if (valueless_by_exception())
269 return std::strong_ordering::equal;
270 std::strong_ordering order = std::strong_ordering::equal;
271 std::size_t current = 0;
272 auto compare = [&](auto* unused) {
273 using Alternative = std::remove_pointer_t<decltype(unused)>;
274 if (current == m_index)
275 order = *static_cast<const Alternative*>(m_storage) <=> *static_cast<const Alternative*>(other.m_storage);
278 (compare(static_cast<Ts*>(nullptr)), ...);
282 template<Type::SafeValue... Ts>
283 requires (sizeof...(Ts) > 0)
284 template<std::size_t I, class Visitor>
285 decltype(auto) Variant<Ts...>::VisitIndex(Visitor&& visitor) const {
286 using Alternative = std::tuple_element_t<I, std::tuple<Ts...>>;
288 return std::invoke(std::forward<Visitor>(visitor), *static_cast<const Alternative*>(m_storage));
289 if constexpr (I + 1 < sizeof...(Ts))
290 return VisitIndex<I + 1>(std::forward<Visitor>(visitor));
292 throw BadVariantAccess("wrong alternative");
295 template<Type::SafeValue... Ts>
296 requires (sizeof...(Ts) > 0)
297 template<class Visitor>
298 decltype(auto) Variant<Ts...>::Visit(Visitor&& visitor) const {
299 if (valueless_by_exception())
300 throw BadVariantAccess("wrong alternative");
301 return VisitIndex<0>(std::forward<Visitor>(visitor));
304 template<class T, Type::SafeValue... Ts>
305 bool holds_alternative(const Variant<Ts...>& value) noexcept {
306 return value.index() == Variant<Ts...>::template AlternativeIndex<T>();
309 template<std::size_t I, Type::SafeValue... Ts>
310 auto& get(Variant<Ts...>& value) {
311 if (value.index() != I)
312 throw BadVariantAccess("wrong alternative");
313 return *get_if<I>(&value);
316 template<std::size_t I, Type::SafeValue... Ts>
317 const auto& get(const Variant<Ts...>& value) {
318 if (value.index() != I)
319 throw BadVariantAccess("wrong alternative");
320 return *get_if<I>(&value);
323 template<class T, Type::SafeValue... Ts>
324 T& get(Variant<Ts...>& value) {
325 T* alternative = get_if<T>(&value);
326 if (alternative == nullptr)
327 throw BadVariantAccess("wrong alternative");
331 template<class T, Type::SafeValue... Ts>
332 const T& get(const Variant<Ts...>& value) {
333 const T* alternative = get_if<T>(&value);
334 if (alternative == nullptr)
335 throw BadVariantAccess("wrong alternative");
339 template<std::size_t I, Type::SafeValue... Ts>
340 auto* get_if(Variant<Ts...>* value) noexcept {
341 using Alternative = std::tuple_element_t<I, std::tuple<Ts...>>;
342 if (value == nullptr || value->index() != I)
343 return static_cast<Alternative*>(nullptr);
344 return static_cast<Alternative*>(value->m_storage);
347 template<std::size_t I, Type::SafeValue... Ts>
348 const auto* get_if(const Variant<Ts...>* value) noexcept {
349 using Alternative = std::tuple_element_t<I, std::tuple<Ts...>>;
350 if (value == nullptr || value->index() != I)
351 return static_cast<const Alternative*>(nullptr);
352 return static_cast<const Alternative*>(value->m_storage);
355 template<class T, Type::SafeValue... Ts>
356 T* get_if(Variant<Ts...>* value) noexcept {
357 if (value == nullptr || value->index() != Variant<Ts...>::template AlternativeIndex<T>())
359 return static_cast<T*>(value->m_storage);
362 template<class T, Type::SafeValue... Ts>
363 const T* get_if(const Variant<Ts...>* value) noexcept {
364 if (value == nullptr || value->index() != Variant<Ts...>::template AlternativeIndex<T>())
366 return static_cast<const T*>(value->m_storage);
369 template<class Visitor, Type::SafeValue... Ts>
370 decltype(auto) visit(Visitor&& visitor, Variant<Ts...>& value) {
371 if (value.valueless_by_exception())
372 throw BadVariantAccess("wrong alternative");
373 return value.Visit(std::forward<Visitor>(visitor));
376 template<class Visitor, Type::SafeValue... Ts>
377 decltype(auto) visit(Visitor&& visitor, const Variant<Ts...>& value) {
378 if (value.valueless_by_exception())
379 throw BadVariantAccess("wrong alternative");
380 return value.Visit(std::forward<Visitor>(visitor));
383 template<Type::SafeValue... Ts>
384 void swap(Variant<Ts...>& left, Variant<Ts...>& right) noexcept {