StormByte 3.0.0
C++26 foundation of the StormByte suite
Loading...
Searching...
No Matches
variant.txx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This is the base for StormByte Suite libraries.
5 *
6 * StormByte 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 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.
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 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. 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
40namespace StormByte::Safe {
41 template<Type::SafeValue... Ts>
42 requires (sizeof...(Ts) > 0)
43 template<class T>
44 constexpr std::size_t Variant<Ts...>::AlternativeIndex() noexcept {
45 std::size_t exact = npos;
46 std::size_t current = 0;
47 (([&] {
48 if constexpr (Type::SameAs<std::remove_cvref_t<T>, Ts>)
49 exact = current;
50 ++current;
51 }()), ...);
52 if (exact != npos)
53 return exact;
54 std::size_t found = npos;
55 current = 0;
56 bool ambiguous = false;
57 (([&] {
58 if constexpr (Type::ConstructibleFrom<Ts, T> && !Type::SameAs<std::remove_cvref_t<T>, Variant>) {
59 if (found == npos)
60 found = current;
61 else
62 ambiguous = true;
63 }
64 ++current;
65 }()), ...);
66 return ambiguous ? npos : found;
67 }
68
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));
74 try {
75 return new (block) Alternative(std::forward<Args>(args)...);
76 } catch (...) {
77 Heap::Free(block);
78 throw;
79 }
80 }
81
82 template<Type::SafeValue... Ts>
83 requires (sizeof...(Ts) > 0)
84 void Variant<Ts...>::Release() noexcept {
85 if (m_storage == nullptr)
86 return;
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();
92 ++current;
93 };
94 (destroy(static_cast<Ts*>(nullptr)), ...);
95 Heap::Free(m_storage);
96 m_storage = nullptr;
97 m_index = npos;
98 }
99
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>();
105 m_index = 0;
106 }
107
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)...);
114 m_index = I;
115 }
116
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>();
123 }
124
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())
129 return;
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();
134 }, value);
135 }
136
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())
141 return;
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();
146 }, value);
147 }
148
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())
153 return;
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;
160 }
161 ++current;
162 };
163 (copy(static_cast<Ts*>(nullptr)), ...);
164 }
165
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;
171 }
172
173 template<Type::SafeValue... Ts>
174 requires (sizeof...(Ts) > 0)
175 Variant<Ts...>::~Variant() noexcept {
176 Release();
177 }
178
179 template<Type::SafeValue... Ts>
180 requires (sizeof...(Ts) > 0)
181 Variant<Ts...>& Variant<Ts...>::operator=(const Variant& other) {
182 if (this == &other)
183 return *this;
184 Variant copied(other);
185 swap(copied);
186 return *this;
187 }
188
189 template<Type::SafeValue... Ts>
190 requires (sizeof...(Ts) > 0)
191 Variant<Ts...>& Variant<Ts...>::operator=(Variant&& other) noexcept {
192 if (this != &other) {
193 Release();
194 m_storage = other.m_storage;
195 m_index = other.m_index;
196 other.m_storage = nullptr;
197 other.m_index = npos;
198 }
199 return *this;
200 }
201
202 template<Type::SafeValue... Ts>
203 requires (sizeof...(Ts) > 0)
204 std::size_t Variant<Ts...>::index() const noexcept {
205 return m_index;
206 }
207
208 template<Type::SafeValue... Ts>
209 requires (sizeof...(Ts) > 0)
210 bool Variant<Ts...>::valueless_by_exception() const noexcept {
211 return m_index == npos;
212 }
213
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)...);
220 Release();
221 m_storage = created;
222 m_index = I;
223 return *created;
224 }
225
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)...);
231 Release();
232 m_storage = created;
233 m_index = AlternativeIndex<T>();
234 return *created;
235 }
236
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);
242 }
243
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)
248 return false;
249 if (valueless_by_exception())
250 return true;
251 bool equal = false;
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);
257 ++current;
258 };
259 (compare(static_cast<Ts*>(nullptr)), ...);
260 return equal;
261 }
262
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);
276 ++current;
277 };
278 (compare(static_cast<Ts*>(nullptr)), ...);
279 return order;
280 }
281
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...>>;
287 if (m_index == I)
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));
291 else
292 throw BadVariantAccess("wrong alternative");
293 }
294
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));
302 }
303
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>();
307 }
308
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);
314 }
315
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);
321 }
322
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");
328 return *alternative;
329 }
330
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");
336 return *alternative;
337 }
338
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);
345 }
346
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);
353 }
354
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>())
358 return nullptr;
359 return static_cast<T*>(value->m_storage);
360 }
361
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>())
365 return nullptr;
366 return static_cast<const T*>(value->m_storage);
367 }
368
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));
374 }
375
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));
381 }
382
383 template<Type::SafeValue... Ts>
384 void swap(Variant<Ts...>& left, Variant<Ts...>& right) noexcept {
385 left.swap(right);
386 }
387}