4#ifndef GREM_CORE_DATA_VARIANT_HPP
5#define GREM_CORE_DATA_VARIANT_HPP
16#include <initializer_list>
32template <
typename... Ts>
38concept bool_convertible = std::is_convertible_v<T, bool>;
40template <
class T,
class U>
41[[nodiscard]]
constexpr auto&& forwardLike(U&& x)
noexcept {
42 constexpr bool IS_CONST = std::is_const_v<std::remove_reference_t<T>>;
43 if constexpr (std::is_lvalue_reference_v<T&&>) {
44 if constexpr (IS_CONST) {
45 return std::as_const(x);
47 return static_cast<U&
>(x);
50 if constexpr (IS_CONST) {
51 return std::move(std::as_const(x));
59struct ConstantList {};
61template <std::size_t
N,
typename Union,
auto... MemberPointers>
62struct n_member_pointers {
63 using type = ConstantList<MemberPointers..., &Union::head>;
66template <std::size_t
N,
typename Union,
auto... MemberPointers>
67requires(
N > 0)
struct n_member_pointers<N, Union, MemberPointers...> : n_member_pointers<
N - 1, typename Union::Tail, MemberPointers..., &Union::tail> {};
69template <std::
size_t N,
typename Union>
70using n_member_pointers_t =
typename n_member_pointers<N, Union>::type;
72template <
auto... MemberPointers,
typename U>
73[[nodiscard]]
constexpr auto& getUnionMemberImplementation(ConstantList<MemberPointers...>, U&& u)
noexcept {
74 return (std::forward<U>(u).*....*MemberPointers);
77template <std::
size_t Index,
typename U>
78[[nodiscard]]
constexpr auto& getUnionMember(U& u)
noexcept {
79 return getUnionMemberImplementation(n_member_pointers_t<Index, U>{}, u);
82template <
typename... Ts>
84 constexpr UnionStorage() =
default;
86 template <std::size_t Index,
typename... Args>
87 UnionStorage(std::in_place_index_t<Index>, Args&&...) =
delete;
90template <
typename First,
typename... Rest>
91union UnionStorage<First, Rest...> {
92 static constexpr std::size_t size = 1 +
sizeof...(Rest);
95 using Tail = UnionStorage<Rest...>;
97 constexpr UnionStorage()
100 template <
typename... Args>
101 constexpr UnionStorage(std::in_place_index_t<0>, Args&&... args)
102 : head(std::forward<Args>(args)...) {}
104 template <std::size_t Index,
typename... Args>
105 constexpr UnionStorage(std::in_place_index_t<Index>, Args&&... args)
106 : tail(std::in_place_index<Index - 1>, std::forward<Args>(args)...) {}
108 constexpr UnionStorage(
const UnionStorage& other, std::size_t otherIndex) {
109 [&]<std::size_t... Indices>(std::index_sequence<Indices...>) ->
void {
110 (void)(((otherIndex == Indices) ? (construct<Indices>(getUnionMember<Indices>(other)),
true) :
false) || ...);
111 }(std::make_index_sequence<size>{});
114 constexpr UnionStorage(UnionStorage&& other, std::size_t otherIndex) {
115 [&]<std::size_t... Indices>(std::index_sequence<Indices...>) ->
void {
116 (void)(((otherIndex == Indices) ? (construct<Indices>(std::move(getUnionMember<Indices>(other))),
true) :
false) || ...);
117 }(std::make_index_sequence<size>{});
120 ~UnionStorage() =
default;
121 constexpr ~UnionStorage()
requires(!std::is_trivially_destructible_v<Head> || !std::is_trivially_destructible_v<Tail>) {}
123 constexpr UnionStorage(
const UnionStorage&) =
default;
124 constexpr UnionStorage(UnionStorage&&) noexcept(
125 std::is_nothrow_move_constructible_v<Head> &&
126 std::is_nothrow_move_constructible_v<Tail>) = default;
127 constexpr UnionStorage& operator=(const UnionStorage&) = default;
128 constexpr UnionStorage& operator=(UnionStorage&&) noexcept(
129 std::is_nothrow_move_assignable_v<Head> &&
130 std::is_nothrow_move_assignable_v<Tail>) = default;
132 template <std::
size_t Index, typename... Args>
133 constexpr
void construct(Args&&... args) {
134 std::construct_at(&getUnionMember<Index>(*
this), std::forward<Args>(args)...);
137 constexpr void destroy(std::size_t index)
noexcept {
138 [&]<std::size_t... Indices>(std::index_sequence<Indices...>) ->
void {
139 (void)(((index == Indices) ? (std::destroy_at(&getUnionMember<Indices>(*
this)),
true) :
false) || ...);
140 }(std::make_index_sequence<size>{});
147template <
typename T, std::size_t Index,
typename... Ts>
148struct VariantIndexImpl;
150template <
typename T, std::size_t Index,
typename First,
typename... Rest>
151struct VariantIndexImpl<
T, Index, First, Rest...> : VariantIndexImpl<T, Index + 1, Rest...> {};
153template <
typename T, std::size_t Index,
typename... Rest>
154struct VariantIndexImpl<
T, Index,
T, Rest...> : std::integral_constant<std::size_t, Index> {};
159template <
typename T,
typename V>
160struct variant_has_alternative;
162template <
typename T,
typename First,
typename... Rest>
163struct variant_has_alternative<
T,
Variant<First, Rest...>> : variant_has_alternative<T, Variant<Rest...>> {};
166struct variant_has_alternative<
T,
Variant<>> : std::false_type {};
168template <
typename T,
typename... Rest>
169struct variant_has_alternative<
T,
Variant<
T, Rest...>> : std::true_type {};
178template <
typename T,
typename V>
182template <
typename T,
typename V>
185template <
typename T,
typename... Ts>
186struct variant_index<T,
Variant<Ts...>> : detail::VariantIndexImpl<T, 0, Ts...> {};
195template <
typename T,
typename V>
199template <std::
size_t Index,
typename V>
200struct variant_alternative;
202template <std::size_t Index,
typename First,
typename... Rest>
203struct variant_alternative<Index,
Variant<First, Rest...>> : variant_alternative<Index - 1, Variant<Rest...>> {};
205template <
typename T,
typename... Rest>
206struct variant_alternative<0, Variant<T, Rest...>> {
217template <std::
size_t Index,
typename V>
224template <
typename... Ts>
225struct variant_size<
Variant<Ts...>> : std::integral_constant<std::size_t, sizeof...(Ts)> {};
265 return std::strong_ordering::equal;
271struct std::hash<
grem::Monostate> {
287 [[nodiscard]] const
char*
what() const noexcept
override {
288 return "Bad variant access.";
292template <
typename... Ts>
295 static constexpr bool HAS_DEFAULT_CONSTRUCTOR = std::is_default_constructible_v<variant_alternative_t<0, Variant>>;
296 static constexpr bool HAS_COPY_CONSTRUCTOR = (std::is_copy_constructible_v<Ts> && ...);
297 static constexpr bool HAS_MOVE_CONSTRUCTOR = (std::is_move_constructible_v<Ts> && ...);
298 static constexpr bool HAS_COPY_ASSIGNMENT = ((std::is_copy_constructible_v<Ts> && std::is_copy_assignable_v<Ts>) && ...);
299 static constexpr bool HAS_MOVE_ASSIGNMENT = ((std::is_move_constructible_v<Ts> && std::is_move_assignable_v<Ts>) && ...);
300 static constexpr bool HAS_TRIVIAL_COPY_CONSTRUCTOR = (std::is_trivially_copy_constructible_v<Ts> && ...);
301 static constexpr bool HAS_TRIVIAL_MOVE_CONSTRUCTOR = (std::is_trivially_move_constructible_v<Ts> && ...);
302 static constexpr bool HAS_TRIVIAL_COPY_ASSIGNMENT =
303 ((std::is_trivially_copy_constructible_v<Ts> && std::is_trivially_copy_assignable_v<Ts> && std::is_trivially_destructible_v<Ts>) && ...);
304 static constexpr bool HAS_TRIVIAL_MOVE_ASSIGNMENT =
305 ((std::is_trivially_move_constructible_v<Ts> && std::is_trivially_move_assignable_v<Ts> && std::is_trivially_destructible_v<Ts>) && ...);
306 static constexpr bool HAS_TRIVIAL_DESTRUCTOR = (std::is_trivially_destructible_v<Ts> && ...);
308 template <
typename T>
309 struct SelectAlternativeTypeOverload {
310 template <
typename U>
311 T operator()(T, U&& u)
requires(
requires { T(std::forward<U>(u)); });
314 struct SelectAlternativeTypeOverloadSet : SelectAlternativeTypeOverload<Ts>... {
315 using SelectAlternativeTypeOverload<Ts>::operator()...;
318 template <
typename U>
319 static constexpr auto F(U&& u) ->
decltype(SelectAlternativeTypeOverloadSet{}(std::forward<U>(u), std::forward<U>(u)));
322 using GREM_private_DerivedFromVariantTag = void;
333 std::conditional_t<
sizeof...(Ts) < 255ull, std::uint8_t,
334 std::conditional_t<
sizeof...(Ts) < 65535ull, std::uint16_t,
335 std::conditional_t<
sizeof...(Ts) < 4294967295ull, std::uint32_t,
352 : storage(std::in_place_index<0>)
353 , activeTypeIndex(0) {}
367 template <
typename U>
368 constexpr Variant(U&& value)
noexcept(std::is_nothrow_constructible_v<decltype(F(std::forward<U>(value))), U>)
370 std::is_constructible_v<
decltype(F(std::forward<U>(value))), U>)
384 template <
typename T,
typename... Args>
405 template <
typename T,
typename U,
typename... Args>
406 constexpr explicit Variant(std::in_place_type_t<T> type, std::initializer_list<U> ilist, Args&&... args)
423 template <std::size_t Index,
typename... Args>
424 constexpr explicit Variant(std::in_place_index_t<Index>
index, Args&&... args)
425 requires(Index <
sizeof...(Ts) && std::is_constructible_v<variant_alternative_t<Index, Variant>, Args...>)
426 : storage(
index, std::forward<Args>(args)...)
427 , activeTypeIndex(Index) {}
444 template <std::size_t Index,
typename U,
typename... Args>
445 constexpr explicit Variant(std::in_place_index_t<Index>
index, std::initializer_list<U> ilist, Args&&... args)
446 requires(Index <
sizeof...(Ts) && std::is_constructible_v<variant_alternative_t<Index, Variant>, std::initializer_list<U>&, Args...>)
447 : storage(
index, ilist, std::forward<Args>(args)...)
448 , activeTypeIndex(Index) {}
456 constexpr ~Variant()
requires(HAS_TRIVIAL_DESTRUCTOR) =
default;
459 constexpr Variant(
const Variant& other)
requires(!HAS_COPY_CONSTRUCTOR) =
delete;
462 constexpr Variant(
const Variant& other)
requires(HAS_COPY_CONSTRUCTOR && HAS_TRIVIAL_COPY_CONSTRUCTOR) =
default;
465 constexpr Variant(
const Variant& other)
requires(HAS_COPY_CONSTRUCTOR && !HAS_TRIVIAL_COPY_CONSTRUCTOR)
466 : storage(other.storage, other.activeTypeIndex)
467 , activeTypeIndex(other.activeTypeIndex) {}
470 constexpr Variant(
Variant&& other)
noexcept requires(HAS_MOVE_CONSTRUCTOR && HAS_TRIVIAL_MOVE_CONSTRUCTOR) =
default;
474 (std::is_nothrow_move_constructible_v<Ts> && ...))
475 requires(HAS_MOVE_CONSTRUCTOR && !HAS_TRIVIAL_MOVE_CONSTRUCTOR)
476 : storage(std::
move(other.storage), other.activeTypeIndex)
477 , activeTypeIndex(other.activeTypeIndex) {}
492 if (
this == &other) {
495 const auto copyAssign = [&]<
size_t OtherIndex,
typename Other>(std::in_place_index_t<OtherIndex>,
const Other& other) ->
void {
496 if (activeTypeIndex == OtherIndex) {
498 }
else if constexpr (std::is_nothrow_copy_constructible_v<Other> || !std::is_nothrow_move_constructible_v<Other>) {
504 [&]<std::size_t... Indices>(std::index_sequence<Indices...>) ->
void {
505 if (!(((other.activeTypeIndex == Indices) ? (copyAssign(std::in_place_index<Indices>, other.template access<Ts, Indices>()),
true) :
false) || ...)) {
508 }(std::make_index_sequence<
sizeof...(Ts)>{});
513 constexpr Variant&
operator=(
Variant&& other)
noexcept requires(HAS_MOVE_ASSIGNMENT && HAS_TRIVIAL_MOVE_ASSIGNMENT) =
default;
522 ((std::is_nothrow_move_constructible_v<Ts> &&
523 std::is_nothrow_move_assignable_v<Ts>) &&
524 ...))
requires(HAS_MOVE_ASSIGNMENT && !HAS_TRIVIAL_MOVE_ASSIGNMENT) {
525 if (
this == &other) {
528 const auto moveAssign = [&]<
size_t OtherIndex,
typename Other>(std::in_place_index_t<OtherIndex>, Other& other) ->
void {
529 if (activeTypeIndex == OtherIndex) {
535 [&]<std::size_t... Indices>(std::index_sequence<Indices...>) ->
void {
536 if (!(((other.activeTypeIndex == Indices) ? (moveAssign(std::in_place_index<Indices>, other.template access<Ts, Indices>()),
true) :
false) || ...)) {
539 }(std::make_index_sequence<
sizeof...(Ts)>{});
558 template <
typename U>
560 std::is_nothrow_assignable_v<decltype(F(std::forward<U>(value)))&, U> && std::is_nothrow_constructible_v<decltype(F(std::forward<U>(value))), U>)
562 std::is_assignable_v<decltype(F(std::forward<U>(value))), U>) {
563 using T =
decltype(F(std::forward<U>(value)));
565 as<T>() = std::forward<U>(value);
567 if constexpr (std::is_nothrow_constructible_v<T, U> || !std::is_nothrow_move_constructible_v<T>) {
590 template <
typename T,
typename... Args>
611 template <
typename T,
typename U,
typename... Args>
612 constexpr T&
emplace(std::initializer_list<U> ilist, Args&&... args)
631 template <std::size_t Index,
typename... Args>
632 constexpr auto&
emplace(Args&&... args)
requires(std::is_constructible_v<variant_alternative_t<Index, Variant>, Args...>) {
633 static_assert(Index <
sizeof...(Ts));
635 storage.template construct<Index>(std::forward<Args>(args)...);
636 activeTypeIndex = Index;
656 template <std::size_t Index,
typename U,
typename... Args>
657 constexpr auto&
emplace(std::initializer_list<U> ilist, Args&&... args)
658 requires(std::is_constructible_v<variant_alternative_t<Index, Variant>, std::initializer_list<U>&, Args...>) {
659 static_assert(Index <
sizeof...(Ts));
661 storage.template construct<Index>(ilist, std::forward<Args>(args)...);
662 activeTypeIndex = Index;
679 ((std::is_nothrow_move_constructible_v<Ts> && std::is_nothrow_swappable_v<Ts>) && ...)) {
680 if (activeTypeIndex == other.activeTypeIndex) {
682 [&]<std::size_t... Indices>(std::index_sequence<Indices...>) ->
void {
683 (void)(((activeTypeIndex == Indices) ? ((
swap(access<Ts, Indices>(), other.template access<Ts, Indices>())),
true) :
false) || ...);
684 }(std::make_index_sequence<
sizeof...(Ts)>{});
686 Variant old = std::move(*
this);
687 *
this = std::move(other);
688 other = std::move(old);
717 return activeTypeIndex;
727 return activeTypeIndex ==
npos;
739 template <
typename T>
754 template <std::
size_t Index>
755 [[nodiscard]]
constexpr bool is() const noexcept {
756 return activeTypeIndex == Index;
783 template <
typename T>
813 template <
typename T>
843 template <
typename T>
873 template <
typename T>
904 template <std::
size_t Index>
905 [[nodiscard]]
constexpr auto&
as() &
noexcept {
907 return detail::getUnionMember<Index>(storage);
935 template <std::
size_t Index>
936 [[nodiscard]]
constexpr const auto&
as() const& noexcept {
938 return detail::getUnionMember<Index>(storage);
966 template <std::
size_t Index>
967 [[nodiscard]]
constexpr auto&&
as() &&
noexcept {
969 return std::move(detail::getUnionMember<Index>(storage));
997 template <std::
size_t Index>
998 [[nodiscard]]
constexpr const auto&&
as() const&& noexcept {
1000 return std::move(detail::getUnionMember<Index>(storage));
1017 template <
typename T>
1039 template <
typename T>
1061 template <
typename T>
1066 return std::move(
as<T>());
1083 template <
typename T>
1088 return std::move(
as<T>());
1106 template <std::
size_t Index>
1107 [[nodiscard]]
constexpr auto&
get() & {
1129 template <std::
size_t Index>
1130 [[nodiscard]]
constexpr const auto&
get() const& {
1152 template <std::
size_t Index>
1153 [[nodiscard]]
constexpr auto&&
get() && {
1175 template <std::
size_t Index>
1176 [[nodiscard]]
constexpr const auto&&
get() const&& {
1196 template <
typename T>
1215 template <
typename T>
1233 template <std::
size_t Index>
1234 [[nodiscard]]
constexpr auto*
get_if() noexcept {
1252 template <std::
size_t Index>
1253 [[nodiscard]]
constexpr const auto*
get_if() const noexcept {
1274 template <
typename Visitor,
typename V>
1275 static constexpr decltype(
auto)
visit(Visitor&& visitor, V&& variant) {
1276 using R = std::common_reference_t<decltype(visitor(detail::forwardLike<V>(std::declval<Ts>())))...>;
1277 if constexpr (std::is_void_v<R>) {
1278 [&]<std::size_t... Indices>(std::index_sequence<Indices...>) ->
void {
1279 if (!(((variant.activeTypeIndex == Indices) ? (visitor(std::forward<V>(variant).
template access<Ts, Indices>()),
true) :
false) || ...)) {
1282 }(std::make_index_sequence<
sizeof...(Ts)>{});
1283 }
else if constexpr (std::is_lvalue_reference_v<R>) {
1284 std::remove_reference_t<R>* result =
nullptr;
1285 [&]<std::size_t... Indices>(std::index_sequence<Indices...>) ->
void {
1286 if (!(((variant.activeTypeIndex == Indices) ? ((result = &visitor(std::forward<V>(variant).
template access<Ts, Indices>())),
true) :
false) || ...)) {
1289 }(std::make_index_sequence<
sizeof...(Ts)>{});
1297 constexpr ~MaybeValue() {}
1301 bool hasValue =
false;
1303 constexpr ~Result() {
1305 std::destroy_at(&value.actual);
1309 [&]<std::size_t... Indices>(std::index_sequence<Indices...>) ->
void {
1310 if (!(((variant.activeTypeIndex == Indices) ? (std::construct_at(&result.value.actual, visitor(std::forward<V>(variant).
template access<Ts, Indices>())),
true)
1315 }(std::make_index_sequence<
sizeof...(Ts)>{});
1316 result.hasValue =
true;
1317 return R{std::move(result.value.actual)};
1337 template <
typename Visitor>
1339 using R = std::common_reference_t<
decltype(visitor(std::in_place_type<Ts>))...>;
1340 if constexpr (std::is_void_v<R>) {
1341 [&]<std::size_t... Indices>(std::index_sequence<Indices...>) ->
void {
1342 if (!(((
index == Indices) ? (visitor(std::in_place_type<Ts>),
true) :
false) || ...)) {
1345 }(std::make_index_sequence<
sizeof...(Ts)>{});
1346 }
else if constexpr (std::is_lvalue_reference_v<R>) {
1347 [&]<std::size_t... Indices>(std::index_sequence<Indices...>) ->
void {
1348 std::remove_reference_t<R>* result =
nullptr;
1349 if (!(((
index == Indices) ? ((result = &visitor(std::in_place_type<Ts>)),
true) :
false) || ...)) {
1353 }(std::make_index_sequence<
sizeof...(Ts)>{});
1360 constexpr ~MaybeValue() {}
1364 bool hasValue =
false;
1366 constexpr ~Result() {
1368 std::destroy_at(&value.actual);
1372 [&]<std::size_t... Indices>(std::index_sequence<Indices...>) ->
void {
1373 if (!(((
index == Indices) ? (std::construct_at(&result.value.actual, visitor(std::in_place_type<Ts>)),
true) :
false) || ...)) {
1376 }(std::make_index_sequence<
sizeof...(Ts)>{});
1377 result.hasValue =
true;
1378 return R{std::move(result.value.actual)};
1395 template <
typename Function>
1397 return std::forward<Function>(function)(std::in_place_type<Ts>...);
1401 template <
typename... Us>
1404 void destroy() noexcept {
1405 if constexpr (!HAS_TRIVIAL_DESTRUCTOR) {
1406 storage.destroy(activeTypeIndex);
1408 activeTypeIndex =
npos;
1411 template <
typename T,
size_t Index>
1412 [[nodiscard]]
constexpr T& access() &
noexcept {
1413 if (std::is_constant_evaluated()) {
1414 return detail::getUnionMember<Index>(storage);
1416 return *std::launder(
reinterpret_cast<T*
>(&storage));
1419 template <
typename T,
size_t Index>
1420 [[nodiscard]]
constexpr const T& access() const& noexcept {
1421 if (std::is_constant_evaluated()) {
1422 return detail::getUnionMember<Index>(storage);
1424 return *std::launder(
reinterpret_cast<const T*
>(&storage));
1427 template <
typename T,
size_t Index>
1428 [[nodiscard]]
constexpr T&& access() &&
noexcept {
1429 if (std::is_constant_evaluated()) {
1430 return std::move(detail::getUnionMember<Index>(storage));
1432 return std::move(*std::launder(
reinterpret_cast<T*
>(&storage)));
1435 template <
typename T,
size_t Index>
1436 [[nodiscard]]
constexpr const T&& access() const&& noexcept {
1437 if (std::is_constant_evaluated()) {
1438 return std::move(detail::getUnionMember<Index>(storage));
1440 return std::move(*std::launder(
reinterpret_cast<const T*
>(&storage)));
1443 detail::UnionStorage<Ts..., Monostate> storage;
1464template <
typename Visitor,
typename V>
1465constexpr decltype(
auto)
visit(Visitor&& visitor, V&& variant)
requires(
requires {
typename std::remove_reference_t<V>::GREM_private_DerivedFromVariantTag; }) {
1466 return std::remove_reference_t<V>::visit(std::forward<Visitor>(visitor), std::forward<V>(variant));
1482template <
typename T,
typename... Ts>
1484 return variant.template is<T>();
1500template <std::size_t Index,
typename... Ts>
1502 return variant.template is<Index>();
1520template <
typename T,
typename... Ts>
1522 return variant.template
get<T>();
1540template <
typename T,
typename... Ts>
1542 return variant.template
get<T>();
1560template <std::size_t Index,
typename... Ts>
1580template <std::size_t Index,
typename... Ts>
1600template <
typename T,
typename... Ts>
1622template <
typename T,
typename... Ts>
1643template <std::size_t Index,
typename... Ts>
1664template <std::size_t Index,
typename... Ts>
1682template <
typename... Ts>
1684 { a.template as<Ts>() == b.template as<Ts>() } -> detail::bool_convertible;
1686 if (a.index() != b.index()) {
1689 if (a.valueless_by_exception()) {
1692 return visit([&]<
typename T>(
const T& value) ->
bool {
return value == b.template as<T>(); }, a);
1707template <
typename... Ts>
1709 { a.template as<Ts>() != b.template as<Ts>() } -> detail::bool_convertible;
1711 if (a.index() != b.index()) {
1714 if (a.valueless_by_exception()) {
1717 return visit([&]<
typename T>(
const T& value) ->
bool {
return value != b.template as<T>(); }, a);
1732template <
typename... Ts>
1734 { a.template as<Ts>() < b.template as<Ts>() } -> detail::bool_convertible;
1736 if (a.valueless_by_exception() && b.valueless_by_exception()) {
1739 if (a.valueless_by_exception()) {
1742 if (b.valueless_by_exception()) {
1745 if (a.index() != b.index()) {
1746 return a.index() < b.index();
1748 return visit([&]<
typename T>(
const T& value) ->
bool {
return value < b.template as<T>(); }, a);
1763template <
typename... Ts>
1765 { a.template as<Ts>() <= b.template as<Ts>() } -> detail::bool_convertible;
1767 if (a.valueless_by_exception() && b.valueless_by_exception()) {
1770 if (a.valueless_by_exception()) {
1773 if (b.valueless_by_exception()) {
1776 if (a.index() != b.index()) {
1777 return a.index() <= b.index();
1779 return visit([&]<
typename T>(
const T& value) ->
bool {
return value <= b.template as<T>(); }, a);
1794template <
typename... Ts>
1796 { a.template as<Ts>() > b.template as<Ts>() } -> detail::bool_convertible;
1798 if (a.valueless_by_exception() && b.valueless_by_exception()) {
1801 if (a.valueless_by_exception()) {
1804 if (b.valueless_by_exception()) {
1807 if (a.index() != b.index()) {
1808 return a.index() > b.index();
1810 return visit([&]<
typename T>(
const T& value) ->
bool {
return value > b.template as<T>(); }, a);
1825template <
typename... Ts>
1827 { a.template as<Ts>() >= b.template as<Ts>() } -> detail::bool_convertible;
1829 if (a.valueless_by_exception() && b.valueless_by_exception()) {
1832 if (a.valueless_by_exception()) {
1835 if (b.valueless_by_exception()) {
1838 if (a.index() != b.index()) {
1839 return a.index() >= b.index();
1841 return visit([&]<
typename T>(
const T& value) ->
bool {
return value >= b.template as<T>(); }, a);
1856template <
typename... Ts>
1859 return std::strong_ordering::equal;
1862 return std::strong_ordering::less;
1865 return std::strong_ordering::greater;
1870 return visit([&]<
typename T>(
const T& value) -> std::common_comparison_category_t<std::compare_three_way_result_t<Ts>...> {
return value <=> b.template as<T>(); }, a);
1879template <
typename... Functors>
1881 using Functors::operator()...;
1884template <
typename... Functors>
1889template <
typename V>
1893 template <
typename... Functors>
1894 constexpr decltype(
auto)
operator()(Functors&&... functors)
const {
1895 return visit(
Overloaded{std::forward<Functors>(functors)...}, variant);
1921template <
typename V>
1922[[nodiscard]]
constexpr detail::Matcher<V>
match(V&& variant) {
1923 return detail::Matcher<V>{std::forward<V>(variant)};
1928template <
typename... Ts>
1929struct std::hash<
grem::Variant<Ts...>> {
1931 return visit(hasher, variant);
1935 template <
typename T>
1938 [[nodiscard]] std::size_t operator()(
const T& value)
const {
1939 return hasher(value);
1943 [[no_unique_address]] std::hash<T> hasher;
1946 class HashVisitor :
public Hash<Ts>... {
1948 using Hash<Ts>::operator()...;
1951 [[no_unique_address]] HashVisitor hasher;
1957template <
typename Signature>
1958struct match_case_type;
1960template <
typename Arg>
1961struct match_case_type<void(Arg)> {
1965template <
typename Arg>
1966struct match_case_type<void(Arg&)> {
1970template <
typename Arg>
1971struct match_case_type<void(Arg&&)> {
1975template <
typename Arg>
1976struct match_case_type<void(const Arg&)> {
1980template <
typename Arg>
1981struct match_case_type<void(const Arg&&)> {
1985template <
typename Signature>
1986using match_case_type_t =
typename match_case_type<Signature>::type;
1988template <
typename... Ts>
1989consteval Variant<Ts...> matchVariantType(
const Variant<Ts...>&);
1991template <
typename T>
1992consteval typename T::value_type matchVariantType(
const T&);
1997#define GREM_MATCH(...) switch (auto&& GREM_private_matchVariant = __VA_ARGS__; GREM_private_matchVariant.index())
1998#define GREM_CASE(...) \
1999 case grem::variant_index_v<grem::detail::match_case_type_t<void(__VA_ARGS__)>, decltype(grem::detail::matchVariantType(GREM_private_matchVariant))>: \
2000 if ([[maybe_unused]] __VA_ARGS__ = GREM_private_matchVariant.template as<grem::detail::match_case_type_t<void(__VA_ARGS__)>>(); true)
2001#define GREM_CASE_DEFAULT(...) \
2003 if ([[maybe_unused]] __VA_ARGS__ = GREM_private_matchVariant; true)
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
Tagged union value type that holds a value of one of the given types.
Definition Variant.hpp:293
constexpr const T * get_if() const noexcept
Access the underlying value with the given type if it is the currently active alternative.
Definition Variant.hpp:1216
constexpr Variant(std::in_place_index_t< Index > index, std::initializer_list< U > ilist, Args &&... args)
Construct a variant alternative in-place given its index, with an initializer list as the first const...
Definition Variant.hpp:445
constexpr const auto & get() const &
Access the underlying value with the given index.
Definition Variant.hpp:1130
constexpr ~Variant()=default
Trivial destructor.
constexpr Variant(Variant &&other) noexcept((std::is_nothrow_move_constructible_v< Ts > &&...))
Move constructor.
Definition Variant.hpp:473
constexpr auto && get() &&
Access the underlying value with the given index.
Definition Variant.hpp:1153
constexpr const T & as() const &noexcept
Access the underlying value with the given type without a safety check.
Definition Variant.hpp:814
constexpr Variant & operator=(const Variant &other)=default
Trivial copy assignment.
constexpr Variant & operator=(Variant &&other) noexcept=default
Trivial move assignment.
constexpr Variant & operator=(Variant &&other) noexcept(((std::is_nothrow_move_constructible_v< Ts > &&//NOLINT(performance-noexcept-move-constructor, cppcoreguidelines-noexcept-move-operations) std::is_nothrow_move_assignable_v< Ts >) &&...))
Move assignment.
Definition Variant.hpp:521
constexpr Variant(std::in_place_type_t< T > type, std::initializer_list< U > ilist, Args &&... args)
Construct a variant alternative in-place given its type, with an initializer list as the first constr...
Definition Variant.hpp:406
constexpr Variant & operator=(U &&value) noexcept(std::is_nothrow_assignable_v< decltype(F(std::forward< U >(value)))&, U > &&std::is_nothrow_constructible_v< decltype(F(std::forward< U >(value))), U >)
Converting assignment.
Definition Variant.hpp:559
constexpr const T && get() const &&
Access the underlying value with the given type.
Definition Variant.hpp:1084
constexpr auto * get_if() noexcept
Access the underlying value with the given index if it is the currently active alternative.
Definition Variant.hpp:1234
constexpr void swap(Variant &other) noexcept(((std::is_nothrow_move_constructible_v< Ts > &&std::is_nothrow_swappable_v< Ts >) &&...))
Swap this variant's value with that of another.
Definition Variant.hpp:678
constexpr T && get() &&
Access the underlying value with the given type.
Definition Variant.hpp:1062
constexpr T & emplace(Args &&... args)
Construct an alternative given its type, destroying the old value.
Definition Variant.hpp:591
constexpr const T && as() const &&noexcept
Access the underlying value with the given type without a safety check.
Definition Variant.hpp:874
friend class Variant
JSON value type using the default allocator.
Definition Variant.hpp:1402
static constexpr decltype(auto) visit(Visitor &&visitor, V &&variant)
Call a visitor functor with the currently active underlying value of a variant.
Definition Variant.hpp:1275
static constexpr index_type npos
Definition Variant.hpp:342
constexpr ~Variant()
Destructor.
Definition Variant.hpp:451
constexpr auto & as() &noexcept
Access the underlying value with the given index without a safety check.
Definition Variant.hpp:905
constexpr const auto && get() const &&
Access the underlying value with the given index.
Definition Variant.hpp:1176
constexpr T * get_if() noexcept
Access the underlying value with the given type if it is the currently active alternative.
Definition Variant.hpp:1197
constexpr const auto & as() const &noexcept
Access the underlying value with the given index without a safety check.
Definition Variant.hpp:936
friend constexpr void swap(Variant &a, Variant &b) noexcept(noexcept(a.swap(b)))
Swap the values of two variants.
Definition Variant.hpp:705
constexpr Variant() noexcept(std::is_nothrow_default_constructible_v< variant_alternative_t< 0, Variant > >)
Default-construct a variant with the first variant alternative, if it is default-constructible.
Definition Variant.hpp:351
constexpr Variant & operator=(const Variant &other)=delete
Deleted copy assignment.
constexpr Variant(U &&value) noexcept(std::is_nothrow_constructible_v< decltype(F(std::forward< U >(value))), U >)
Converting constructor.
Definition Variant.hpp:368
constexpr T && as() &&noexcept
Access the underlying value with the given type without a safety check.
Definition Variant.hpp:844
constexpr Variant(std::in_place_index_t< Index > index, Args &&... args)
Construct a variant alternative in-place given its index.
Definition Variant.hpp:424
constexpr Variant(std::in_place_type_t< T > type, Args &&... args)
Construct a variant alternative in-place given its type.
Definition Variant.hpp:385
constexpr Variant(const Variant &other)=default
Trivial copy constructor.
constexpr T & emplace(std::initializer_list< U > ilist, Args &&... args)
Construct an alternative given its type, with an initializer list as the first constructor argument,...
Definition Variant.hpp:612
constexpr index_type index() const noexcept
Definition Variant.hpp:716
std::conditional_t< sizeof...(Ts)< 255ull, std::uint8_t, std::conditional_t< sizeof...(Ts)< 65535ull, std::uint16_t, std::conditional_t< sizeof...(Ts)< 4294967295ull, std::uint32_t, std::uint64_t > > > index_type
Index type used to encode the active alternative type.
Definition Variant.hpp:332
constexpr Variant(const Variant &other)
Copy constructor.
Definition Variant.hpp:465
constexpr bool is() const noexcept
Definition Variant.hpp:740
constexpr auto && as() &&noexcept
Access the underlying value with the given index without a safety check.
Definition Variant.hpp:967
constexpr Variant(const Variant &other)=delete
Deleted copy constructor.
static constexpr decltype(auto) apply(Function &&function)
Call a functor with all alternative types.
Definition Variant.hpp:1396
static constexpr decltype(auto) visitIndex(index_type index, Visitor &&visitor)
Call a visitor functor with the type corresponding to a variant index.
Definition Variant.hpp:1338
constexpr const auto * get_if() const noexcept
Access the underlying value with the given index if it is the currently active alternative.
Definition Variant.hpp:1253
constexpr bool is() const noexcept
Check if the variant currently holds the alternative with the given index.
Definition Variant.hpp:755
constexpr Variant(Variant &&other) noexcept=default
Trivial move constructor.
constexpr T & get() &
Access the underlying value with the given type.
Definition Variant.hpp:1018
constexpr const auto && as() const &&noexcept
Access the underlying value with the given index without a safety check.
Definition Variant.hpp:998
constexpr auto & emplace(Args &&... args)
Construct an alternative given its index, destroying the old value.
Definition Variant.hpp:632
constexpr auto & get() &
Access the underlying value with the given index.
Definition Variant.hpp:1107
constexpr const T & get() const &
Access the underlying value with the given type.
Definition Variant.hpp:1040
constexpr T & as() &noexcept
Access the underlying value with the given type without a safety check.
Definition Variant.hpp:784
constexpr Variant & operator=(const Variant &other)
Copy assignment.
Definition Variant.hpp:491
constexpr bool valueless_by_exception() const noexcept
Check if the variant is in the valueless by exception state.
Definition Variant.hpp:726
constexpr auto & emplace(std::initializer_list< U > ilist, Args &&... args)
Construct an alternative given its index, with an initializer list as the first constructor argument,...
Definition Variant.hpp:657
@ U
'u'
Definition keyboard.hpp:91
@ N
'n'
Definition keyboard.hpp:84
@ T
't'
Definition keyboard.hpp:90
json::VariantBase< std::allocator > Variant
JSON value type using the default allocator.
Definition json.hpp:1094
std::function< Signature > Function
Definition Function.hpp:14
constexpr tuple_element_t< Index, Pair< T1, T2 > > & get(Pair< T1, T2 > &t) noexcept
Definition Pair.hpp:202
constexpr bool variant_has_alternative_v
Check if a variant type has a given type as one of its possible alternatives.
Definition Variant.hpp:179
constexpr std::size_t variant_size_v
Get the number of alternative types of a variant type.
Definition Variant.hpp:234
typename variant_alternative< Index, V >::type variant_alternative_t
Get the type of the variant alternative with a given index in a variant type.
Definition Variant.hpp:218
constexpr T * get_if(Variant< Ts... > *variant) noexcept
Access the underlying value with the given type of a variant if it is the currently active alternativ...
Definition Variant.hpp:1601
constexpr bool operator==(const Indirect< A, AllocatorA > &a, const Indirect< B, AllocatorB > &b)
Definition Indirect.hpp:246
constexpr auto move(input_range auto &&range, auto outputIterator)
Definition algorithms.hpp:669
constexpr decltype(auto) visit(Visitor &&visitor, V &&variant)
Call a visitor functor with the currently active underlying value of a variant.
Definition Variant.hpp:1465
constexpr bool operator<(Span< T, N > a, Span< T, M > b)
Definition Span.hpp:324
constexpr detail::Matcher< V > match(V &&variant)
Choose a function overload to execute based on the active alternative of a variant.
Definition Variant.hpp:1922
constexpr std::compare_three_way_result_t< A, B > operator<=>(const Indirect< A, AllocatorA > &a, const Indirect< B, AllocatorB > &b)
Definition Indirect.hpp:273
constexpr bool holds_alternative(const Variant< Ts... > &variant) noexcept
Check if a variant currently holds the alternative with the given type.
Definition Variant.hpp:1483
constexpr bool operator<=(Span< T, N > a, Span< T, M > b)
Definition Span.hpp:329
constexpr bool operator>=(Span< T, N > a, Span< T, M > b)
Definition Span.hpp:339
constexpr std::size_t variant_index_v
Get the index of the variant alternative with a given type in a variant type.
Definition Variant.hpp:196
constexpr bool operator!=(Span< T, N > a, Span< T, M > b)
Definition Span.hpp:319
constexpr bool operator>(Span< T, N > a, Span< T, M > b)
Definition Span.hpp:334
Overloaded(Functors...) -> Overloaded< Functors... >
Exception type that is thrown on an attempt to erroneously access an inactive alternative of a Varian...
Definition Variant.hpp:284
const char * what() const noexcept override
Definition Variant.hpp:287
BadVariantAccess() noexcept=default
Unit type for representing an empty alternative in Variant.
Definition Variant.hpp:242
Visitor type for combining callable objects, such as lambdas, into an overload set.
Definition Variant.hpp:1880
std::size_t operator()(const grem::Monostate &) const
Definition Variant.hpp:272
std::size_t operator()(const grem::Variant< Ts... > &variant) const
Definition Variant.hpp:1930