StormByte 3.0.0
C++26 foundation of the StormByte suite
Loading...
Searching...
No Matches
variant.hxx
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
40#pragma once
41
47
48#include <compare>
49#include <cstddef>
50#include <functional>
51#include <new>
52#include <tuple>
53#include <type_traits>
54#include <utility>
55#include <variant>
56
61namespace StormByte {
66 namespace Safe {
71 struct Monostate {
76 friend constexpr bool operator==(Monostate, Monostate) noexcept {
77 return true;
78 }
79
84 friend STORMBYTE_FORCE_INLINE constexpr std::strong_ordering operator<=>(Monostate, Monostate) noexcept {
85 return std::strong_ordering::equal;
86 }
87 };
88 }
89}
90
91template<>
93
94template<>
96
97namespace StormByte::Safe {
105 template<Type::SafeValue... Ts>
106 requires (sizeof...(Ts) > 0)
108 public:
109 static constexpr std::size_t npos = static_cast<std::size_t>(-1);
110
116 template<class T>
117 static constexpr std::size_t AlternativeIndex() noexcept;
118
124
131 template<class T>
132 requires (Variant::template AlternativeIndex<T>() != npos)
133 Variant(T&& value): m_storage(nullptr), m_index(npos) {
134 constexpr std::size_t index = AlternativeIndex<T>();
135 using Alternative = std::tuple_element_t<index, std::tuple<Ts...>>;
136 m_storage = Make<Alternative>(std::forward<T>(value));
137 m_index = index;
138 }
139
148 template<std::size_t I, class... Args>
149 explicit Variant(std::in_place_index_t<I> index, Args&&... args);
150
159 template<class T, class... Args>
160 explicit Variant(std::in_place_type_t<T> tag, Args&&... args);
161
167 explicit Variant(const std::variant<Ts...>& value);
168
174 explicit Variant(std::variant<Ts...>&& value);
175
181 Variant(const Variant& other);
182
187 Variant(Variant&& other) noexcept;
188
192 ~Variant() noexcept;
193
200 Variant& operator=(const Variant& other);
201
207 Variant& operator=(Variant&& other) noexcept;
208
216 template<class T>
217 requires (Variant::template AlternativeIndex<T>() != npos)
218 Variant& operator=(T&& value) {
219 emplace<std::tuple_element_t<Variant::template AlternativeIndex<T>(), std::tuple<Ts...>>>(std::forward<T>(value));
220 return *this;
221 }
222
227 std::size_t index() const noexcept;
228
233 bool valueless_by_exception() const noexcept;
234
243 template<std::size_t I, class... Args>
244 auto& emplace(Args&&... args);
245
254 template<class T, class... Args>
255 T& emplace(Args&&... args);
256
261 void swap(Variant& other) noexcept;
262
268 bool operator==(const Variant& other) const;
269
275 STORMBYTE_FORCE_INLINE std::strong_ordering operator<=>(const Variant& other) const;
276
282 STORMBYTE_FORCE_INLINE explicit operator std::variant<Ts...>() const {
284 throw BadVariantAccess("wrong alternative");
285 return Visit([](const auto& alternative) -> std::variant<Ts...> {
286 return alternative;
287 });
288 }
289
290 private:
299 template<class Alternative, class... Args>
300 static Alternative* Make(Args&&... args);
301
305 void Release() noexcept;
306
315 template<std::size_t I, class Visitor>
316 decltype(auto) VisitIndex(Visitor&& visitor) const;
317
325 template<class Visitor>
326 decltype(auto) Visit(Visitor&& visitor) const;
327
328 void* m_storage;
329 std::size_t m_index;
330
331 template<std::size_t I, Type::SafeValue... Us>
332 friend auto* get_if(Variant<Us...>* value) noexcept;
333
334 template<std::size_t I, Type::SafeValue... Us>
335 friend const auto* get_if(const Variant<Us...>* value) noexcept;
336
337 template<class T, Type::SafeValue... Us>
338 friend T* get_if(Variant<Us...>* value) noexcept;
339
340 template<class T, Type::SafeValue... Us>
341 friend const T* get_if(const Variant<Us...>* value) noexcept;
342
343 template<class Visitor, Type::SafeValue... Us>
344 friend decltype(auto) visit(Visitor&& visitor, Variant<Us...>& value);
345
346 template<class Visitor, Type::SafeValue... Us>
347 friend decltype(auto) visit(Visitor&& visitor, const Variant<Us...>& value);
348 };
349
357 template<class T, Type::SafeValue... Ts>
358 bool holds_alternative(const Variant<Ts...>& value) noexcept;
359
368 template<std::size_t I, Type::SafeValue... Ts>
369 auto& get(Variant<Ts...>& value);
370
379 template<std::size_t I, Type::SafeValue... Ts>
380 const auto& get(const Variant<Ts...>& value);
381
390 template<class T, Type::SafeValue... Ts>
391 T& get(Variant<Ts...>& value);
392
401 template<class T, Type::SafeValue... Ts>
402 const T& get(const Variant<Ts...>& value);
403
411 template<std::size_t I, Type::SafeValue... Ts>
412 auto* get_if(Variant<Ts...>* value) noexcept;
413
421 template<std::size_t I, Type::SafeValue... Ts>
422 const auto* get_if(const Variant<Ts...>* value) noexcept;
423
431 template<class T, Type::SafeValue... Ts>
432 T* get_if(Variant<Ts...>* value) noexcept;
433
441 template<class T, Type::SafeValue... Ts>
442 const T* get_if(const Variant<Ts...>* value) noexcept;
443
453 template<class Visitor, Type::SafeValue... Ts>
454 decltype(auto) visit(Visitor&& visitor, Variant<Ts...>& value);
455
465 template<class Visitor, Type::SafeValue... Ts>
466 decltype(auto) visit(Visitor&& visitor, const Variant<Ts...>& value);
467
474 template<Type::SafeValue... Ts>
475 void swap(Variant<Ts...>& left, Variant<Ts...>& right) noexcept;
476}
477
478template<StormByte::Type::SafeValue... Ts>
479struct StormByte::Type::IsSafe<StormByte::Safe::Variant<Ts...>>: std::true_type {};
480
481template<StormByte::Type::SafeValue... Ts>
482struct StormByte::Type::IsSafeValue<StormByte::Safe::Variant<Ts...>>: std::true_type {};
483
484template<StormByte::Type::SafeValue... Ts>
491 STORMBYTE_FORCE_INLINE std::size_t operator()(const StormByte::Safe::Variant<Ts...>& value) const noexcept {
492 std::size_t hash = Hash<std::size_t>{}(value.index());
493 if (value.valueless_by_exception())
494 return hash;
495 std::size_t current = 0;
496 auto step = [&](auto* unused) {
497 using Alternative = std::remove_pointer_t<decltype(unused)>;
498 if (current == value.index()) {
499 const Alternative* alternative = get_if<Alternative>(&value);
500 if (alternative != nullptr)
501 hash = HashCombine(hash, Hash<Alternative>{}(*alternative));
502 }
503 ++current;
504 };
505 (step(static_cast<Ts*>(nullptr)), ...);
506 return hash;
507 }
508};
509
510template<>
520
521template<StormByte::Type::SafeValue... Ts>
522struct std::hash<StormByte::Safe::Variant<Ts...>> {
528 std::size_t operator()(const StormByte::Safe::Variant<Ts...>& value) const noexcept {
529 return StormByte::Safe::Hash<StormByte::Safe::Variant<Ts...>>{}(value);
530 }
531};
532
533template<>
534struct std::hash<StormByte::Safe::Monostate> {
539 std::size_t operator()(StormByte::Safe::Monostate value) const noexcept {
541 }
542};
543
A variant was read with the wrong alternative, or while valueless.
Definition exception.hxx:347
std::variant stored on Base's heap.
Definition variant.hxx:107
static constexpr std::size_t npos
No alternative.
Definition variant.hxx:109
Variant()
Default-construct the first alternative.
Variant(std::in_place_index_t< I > index, Args &&... args)
Construct the alternative at I.
Variant(const Variant &other)
Copy the active alternative into a new Base block.
static constexpr std::size_t AlternativeIndex() noexcept
Index of the single alternative constructible from T, or npos.
auto & emplace(Args &&... args)
Construct the alternative at I, replacing the current one only after construction.
~Variant() noexcept
Destroy the active alternative and release its block.
bool valueless_by_exception() const noexcept
Whether a throwing operation left this variant without an alternative.
void swap(Variant &other) noexcept
Exchange storage.
Variant(std::in_place_type_t< T > tag, Args &&... args)
Construct the alternative T.
std::size_t index() const noexcept
Index of the active alternative, or npos when valueless.
Variant(std::variant< Ts... > &&value)
Move a caller-owned std::variant by copying the alternative into Base storage.
Variant(const std::variant< Ts... > &value)
Copy a caller-owned std::variant.
Variant(Variant &&other) noexcept
Take the active block.
friend decltype(auto) visit(Visitor &&visitor, Variant< Us... > &value)
friend auto * get_if(Variant< Us... > *value) noexcept
Value permitted in opaque Safe collections.
Definition safe.hxx:409
Instantiation of std::variant, after stripping cv and references.
Definition wrappers.hxx:177
Private Base heap used by StormByte::Safe::Shared, StormByte::Safe::Unique and StormByte::Safe::Clona...
Owned values that cross a DLL without the caller's CRT.
bool holds_alternative(const Variant< Ts... > &value) noexcept
Whether value holds T.
STORMBYTE_FORCE_INLINE std::size_t HashCombine(std::size_t seed, std::size_t next) noexcept
Mix an existing hash with another.
Definition hash.hxx:83
auto * get_if(Variant< Ts... > *value) noexcept
Pointer to the alternative at I, or null.
constexpr First & get(Pair< First, Second > &pair) noexcept
Access the first pair member.
void swap(Binary &left, Binary &right) noexcept
Exchange two sequences.
Definition binary.hxx:760
decltype(auto) visit(Visitor &&visitor, Variant< Ts... > &value)
Call visitor with the active alternative.
@ Release
Release.
Definition memory_order.hxx:64
STORMBYTE_FORCE_INLINE std::size_t HashBytes(std::span< const std::byte > bytes) noexcept
Mix a byte range with FNV-1a.
Definition hash.hxx:66
Root namespace of the StormByte suite.
Octet count. Defined in byte_size.hxx. Forwarded so Type::Numeral can name it.
STL namespace.
#define STORMBYTE_FORCE_INLINE
Definition platform.h:134
STORMBYTE_FORCE_INLINE std::size_t operator()(StormByte::Safe::Monostate) const noexcept
Hash an empty alternative.
Definition variant.hxx:516
STORMBYTE_FORCE_INLINE std::size_t operator()(const StormByte::Safe::Variant< Ts... > &value) const noexcept
Hash the index and the active alternative.
Definition variant.hxx:491
Cross-module hash.
Definition hash.hxx:95
Empty alternative.
Definition variant.hxx:71
friend constexpr bool operator==(Monostate, Monostate) noexcept
Two empty alternatives are equal.
Definition variant.hxx:76
friend STORMBYTE_FORCE_INLINE constexpr std::strong_ordering operator<=>(Monostate, Monostate) noexcept
Two empty alternatives compare equal.
Definition variant.hxx:84
Recognizes collection values with Base-controlled lifetime.
Definition safe.hxx:347
Recognizes Base Safe components, not universal ABI compatibility.
Definition safe.hxx:138
std::size_t operator()(StormByte::Safe::Monostate value) const noexcept
Hashes an empty alternative.
Definition variant.hxx:539
std::size_t operator()(const StormByte::Safe::Variant< Ts... > &value) const noexcept
Hashes value.
Definition variant.hxx:528
#define STORMBYTE_PUBLIC_TYPE
Definition visibility.h:57