2021-04-25 21:04:10 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Array.h>
|
|
|
|
#include <AK/BitCast.h>
|
2024-10-30 12:44:20 +00:00
|
|
|
#include <AK/Noncopyable.h>
|
2021-04-25 21:04:10 +00:00
|
|
|
#include <AK/StdLibExtras.h>
|
2021-05-19 15:28:27 +00:00
|
|
|
#include <AK/TypeList.h>
|
2021-04-25 21:04:10 +00:00
|
|
|
|
|
|
|
namespace AK::Detail {
|
|
|
|
|
2021-05-08 19:08:34 +00:00
|
|
|
template<typename T, typename IndexType, IndexType InitialIndex, typename... InTypes>
|
|
|
|
struct VariantIndexOf {
|
|
|
|
static_assert(DependentFalse<T, IndexType, InTypes...>, "Invalid VariantIndex instantiated");
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T, typename IndexType, IndexType InitialIndex, typename InType, typename... RestOfInTypes>
|
|
|
|
struct VariantIndexOf<T, IndexType, InitialIndex, InType, RestOfInTypes...> {
|
|
|
|
consteval IndexType operator()()
|
|
|
|
{
|
|
|
|
if constexpr (IsSame<T, InType>)
|
|
|
|
return InitialIndex;
|
|
|
|
else
|
|
|
|
return VariantIndexOf<T, IndexType, InitialIndex + 1, RestOfInTypes...> {}();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T, typename IndexType, IndexType InitialIndex>
|
|
|
|
struct VariantIndexOf<T, IndexType, InitialIndex> {
|
|
|
|
consteval IndexType operator()() { return InitialIndex; }
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T, typename IndexType, typename... Ts>
|
|
|
|
consteval IndexType index_of()
|
|
|
|
{
|
|
|
|
return VariantIndexOf<T, IndexType, 0, Ts...> {}();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename IndexType, IndexType InitialIndex, typename... Ts>
|
2021-04-25 21:04:10 +00:00
|
|
|
struct Variant;
|
|
|
|
|
2021-05-08 19:08:34 +00:00
|
|
|
template<typename IndexType, IndexType InitialIndex, typename F, typename... Ts>
|
|
|
|
struct Variant<IndexType, InitialIndex, F, Ts...> {
|
|
|
|
static constexpr auto current_index = VariantIndexOf<F, IndexType, InitialIndex, F, Ts...> {}();
|
2021-06-09 16:00:07 +00:00
|
|
|
ALWAYS_INLINE static void delete_(IndexType id, void* data)
|
2021-04-25 21:04:10 +00:00
|
|
|
{
|
2021-05-08 19:08:34 +00:00
|
|
|
if (id == current_index)
|
2021-04-25 21:04:10 +00:00
|
|
|
bit_cast<F*>(data)->~F();
|
|
|
|
else
|
2021-05-08 19:08:34 +00:00
|
|
|
Variant<IndexType, InitialIndex + 1, Ts...>::delete_(id, data);
|
2021-04-25 21:04:10 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 16:00:07 +00:00
|
|
|
ALWAYS_INLINE static void move_(IndexType old_id, void* old_data, void* new_data)
|
2021-04-25 21:04:10 +00:00
|
|
|
{
|
2021-05-08 19:08:34 +00:00
|
|
|
if (old_id == current_index)
|
2021-04-25 21:04:10 +00:00
|
|
|
new (new_data) F(move(*bit_cast<F*>(old_data)));
|
|
|
|
else
|
2021-05-08 19:08:34 +00:00
|
|
|
Variant<IndexType, InitialIndex + 1, Ts...>::move_(old_id, old_data, new_data);
|
2021-04-25 21:04:10 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
ALWAYS_INLINE static void copy_(IndexType old_id, void const* old_data, void* new_data)
|
2021-04-25 21:04:10 +00:00
|
|
|
{
|
2021-05-08 19:08:34 +00:00
|
|
|
if (old_id == current_index)
|
2021-06-09 16:00:07 +00:00
|
|
|
new (new_data) F(*bit_cast<F const*>(old_data));
|
2021-04-25 21:04:10 +00:00
|
|
|
else
|
2021-05-08 19:08:34 +00:00
|
|
|
Variant<IndexType, InitialIndex + 1, Ts...>::copy_(old_id, old_data, new_data);
|
2021-04-25 21:04:10 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-05-08 19:08:34 +00:00
|
|
|
template<typename IndexType, IndexType InitialIndex>
|
|
|
|
struct Variant<IndexType, InitialIndex> {
|
2021-06-09 16:00:07 +00:00
|
|
|
ALWAYS_INLINE static void delete_(IndexType, void*) { }
|
|
|
|
ALWAYS_INLINE static void move_(IndexType, void*, void*) { }
|
2022-04-01 17:58:27 +00:00
|
|
|
ALWAYS_INLINE static void copy_(IndexType, void const*, void*) { }
|
2021-05-19 15:28:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename IndexType, typename... Ts>
|
|
|
|
struct VisitImpl {
|
2022-01-13 17:29:13 +00:00
|
|
|
template<typename RT, typename T, size_t I, typename Fn>
|
2022-01-31 23:41:59 +00:00
|
|
|
static constexpr bool has_explicitly_named_overload()
|
2022-01-13 17:29:13 +00:00
|
|
|
{
|
|
|
|
// If we're not allowed to make a member function pointer and call it directly (without explicitly resolving it),
|
|
|
|
// we have a templated function on our hands (or a function overload set).
|
|
|
|
// in such cases, we don't have an explicitly named overload, and we would have to select it.
|
2022-01-23 14:31:07 +00:00
|
|
|
return requires { (declval<Fn>().*(&Fn::operator()))(declval<T>()); };
|
2022-01-13 17:29:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename ReturnType, typename T, typename Visitor, auto... Is>
|
2022-01-31 23:41:59 +00:00
|
|
|
static constexpr bool should_invoke_const_overload(IndexSequence<Is...>)
|
2022-01-13 17:29:13 +00:00
|
|
|
{
|
|
|
|
// Scan over all the different visitor functions, if none of them are suitable for calling with `T const&`, avoid calling that first.
|
2022-01-23 14:31:07 +00:00
|
|
|
return ((has_explicitly_named_overload<ReturnType, T, Is, typename Visitor::Types::template Type<Is>>()) || ...);
|
2022-01-13 17:29:13 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 14:01:00 +00:00
|
|
|
template<typename Self, typename Visitor, IndexType CurrentIndex = 0>
|
2022-10-16 22:06:11 +00:00
|
|
|
ALWAYS_INLINE static constexpr decltype(auto) visit(Self& self, IndexType id, void const* data, Visitor&& visitor)
|
|
|
|
requires(CurrentIndex < sizeof...(Ts))
|
2021-05-19 15:28:27 +00:00
|
|
|
{
|
|
|
|
using T = typename TypeList<Ts...>::template Type<CurrentIndex>;
|
|
|
|
|
2022-01-13 17:29:13 +00:00
|
|
|
if (id == CurrentIndex) {
|
|
|
|
// Check if Visitor::operator() is an explicitly typed function (as opposed to a templated function)
|
|
|
|
// if so, try to call that with `T const&` first before copying the Variant's const-ness.
|
|
|
|
// This emulates normal C++ call semantics where templated functions are considered last, after all non-templated overloads
|
|
|
|
// are checked and found to be unusable.
|
|
|
|
using ReturnType = decltype(visitor(*bit_cast<T*>(data)));
|
|
|
|
if constexpr (should_invoke_const_overload<ReturnType, T, Visitor>(MakeIndexSequence<Visitor::Types::size>()))
|
|
|
|
return visitor(*bit_cast<AddConst<T>*>(data));
|
|
|
|
|
2022-01-13 14:01:00 +00:00
|
|
|
return visitor(*bit_cast<CopyConst<Self, T>*>(data));
|
2022-01-13 17:29:13 +00:00
|
|
|
}
|
2021-05-19 15:28:27 +00:00
|
|
|
|
|
|
|
if constexpr ((CurrentIndex + 1) < sizeof...(Ts))
|
2022-01-13 14:01:00 +00:00
|
|
|
return visit<Self, Visitor, CurrentIndex + 1>(self, id, data, forward<Visitor>(visitor));
|
2021-05-19 15:28:27 +00:00
|
|
|
else
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
2021-04-25 21:04:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct VariantNoClearTag {
|
|
|
|
explicit VariantNoClearTag() = default;
|
|
|
|
};
|
2021-05-08 19:08:34 +00:00
|
|
|
struct VariantConstructTag {
|
|
|
|
explicit VariantConstructTag() = default;
|
|
|
|
};
|
2021-04-25 21:04:10 +00:00
|
|
|
|
|
|
|
template<typename T, typename Base>
|
|
|
|
struct VariantConstructors {
|
2023-05-01 14:59:46 +00:00
|
|
|
// The pointless `typename Base` constraints are a workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109683
|
2022-10-16 22:06:11 +00:00
|
|
|
ALWAYS_INLINE VariantConstructors(T&& t)
|
2023-05-01 14:59:46 +00:00
|
|
|
requires(requires { T(move(t)); typename Base; })
|
2021-04-25 21:04:10 +00:00
|
|
|
{
|
2021-05-22 05:16:40 +00:00
|
|
|
internal_cast().clear_without_destruction();
|
|
|
|
internal_cast().set(move(t), VariantNoClearTag {});
|
2021-04-25 21:04:10 +00:00
|
|
|
}
|
|
|
|
|
2022-10-16 22:06:11 +00:00
|
|
|
ALWAYS_INLINE VariantConstructors(T const& t)
|
2023-05-01 14:59:46 +00:00
|
|
|
requires(requires { T(t); typename Base; })
|
2021-04-25 21:04:10 +00:00
|
|
|
{
|
2021-05-22 05:16:40 +00:00
|
|
|
internal_cast().clear_without_destruction();
|
|
|
|
internal_cast().set(t, VariantNoClearTag {});
|
2021-04-25 21:04:10 +00:00
|
|
|
}
|
|
|
|
|
2021-09-16 06:20:31 +00:00
|
|
|
ALWAYS_INLINE VariantConstructors() = default;
|
2021-04-25 21:04:10 +00:00
|
|
|
|
|
|
|
private:
|
2021-06-09 16:00:07 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE Base& internal_cast()
|
2021-04-25 21:04:10 +00:00
|
|
|
{
|
|
|
|
// Warning: Internal type shenanigans - VariantsConstrutors<T, Base> <- Base
|
|
|
|
// Not the other way around, so be _really_ careful not to cause issues.
|
2022-05-20 16:38:38 +00:00
|
|
|
return *static_cast<Base*>(this);
|
2021-04-25 21:04:10 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-05-09 03:52:43 +00:00
|
|
|
// Type list deduplication
|
|
|
|
// Since this is a big template mess, each template is commented with how and why it works.
|
|
|
|
struct ParameterPackTag {
|
|
|
|
};
|
|
|
|
|
|
|
|
// Pack<Ts...> is just a way to pass around the type parameter pack Ts
|
|
|
|
template<typename... Ts>
|
|
|
|
struct ParameterPack : ParameterPackTag {
|
|
|
|
};
|
|
|
|
|
|
|
|
// Blank<T> is a unique replacement for T, if T is a duplicate type.
|
|
|
|
template<typename T>
|
|
|
|
struct Blank {
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename A, typename P>
|
|
|
|
inline constexpr bool IsTypeInPack = false;
|
|
|
|
|
|
|
|
// IsTypeInPack<T, Pack<Ts...>> will just return whether 'T' exists in 'Ts'.
|
|
|
|
template<typename T, typename... Ts>
|
|
|
|
inline constexpr bool IsTypeInPack<T, ParameterPack<Ts...>> = (IsSame<T, Ts> || ...);
|
|
|
|
|
|
|
|
// Replaces T with Blank<T> if it exists in Qs.
|
|
|
|
template<typename T, typename... Qs>
|
|
|
|
using BlankIfDuplicate = Conditional<(IsTypeInPack<T, Qs> || ...), Blank<T>, T>;
|
|
|
|
|
2024-02-08 23:52:03 +00:00
|
|
|
template<size_t I, typename...>
|
2021-05-09 03:52:43 +00:00
|
|
|
struct InheritFromUniqueEntries;
|
|
|
|
|
|
|
|
// InheritFromUniqueEntries will inherit from both Qs and Ts, but only scan entries going *forwards*
|
|
|
|
// that is to say, if it's scanning from index I in Qs, it won't scan for duplicates for entries before I
|
|
|
|
// as that has already been checked before.
|
|
|
|
// This makes sure that the search is linear in time (like the 'merge' step of merge sort).
|
2024-02-08 23:52:03 +00:00
|
|
|
template<size_t I, typename... Ts, size_t... Js, typename... Qs>
|
2021-05-09 03:52:43 +00:00
|
|
|
struct InheritFromUniqueEntries<I, ParameterPack<Ts...>, IndexSequence<Js...>, Qs...>
|
|
|
|
: public BlankIfDuplicate<Ts, Conditional<Js <= I, ParameterPack<>, Qs>...>... {
|
|
|
|
|
|
|
|
using BlankIfDuplicate<Ts, Conditional<Js <= I, ParameterPack<>, Qs>...>::BlankIfDuplicate...;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename...>
|
|
|
|
struct InheritFromPacks;
|
|
|
|
|
|
|
|
// InheritFromPacks will attempt to 'merge' the pack 'Ps' with *itself*, but skip the duplicate entries
|
|
|
|
// (via InheritFromUniqueEntries).
|
2024-02-08 23:52:03 +00:00
|
|
|
template<size_t... Is, typename... Ps>
|
2021-05-09 03:52:43 +00:00
|
|
|
struct InheritFromPacks<IndexSequence<Is...>, Ps...>
|
|
|
|
: public InheritFromUniqueEntries<Is, Ps, IndexSequence<Is...>, Ps...>... {
|
|
|
|
|
|
|
|
using InheritFromUniqueEntries<Is, Ps, IndexSequence<Is...>, Ps...>::InheritFromUniqueEntries...;
|
|
|
|
};
|
|
|
|
|
2021-05-17 16:48:55 +00:00
|
|
|
// Just a nice wrapper around InheritFromPacks, which will wrap any parameter packs in ParameterPack (unless it already is one).
|
2021-05-09 03:52:43 +00:00
|
|
|
template<typename... Ps>
|
|
|
|
using MergeAndDeduplicatePacks = InheritFromPacks<MakeIndexSequence<sizeof...(Ps)>, Conditional<IsBaseOf<ParameterPackTag, Ps>, Ps, ParameterPack<Ps>>...>;
|
|
|
|
|
2021-04-25 21:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
struct Empty {
|
2023-07-27 10:14:56 +00:00
|
|
|
constexpr bool operator==(Empty const&) const = default;
|
2021-04-25 21:04:10 +00:00
|
|
|
};
|
|
|
|
|
2022-10-14 18:05:46 +00:00
|
|
|
template<typename T>
|
2023-07-08 02:44:33 +00:00
|
|
|
concept NotLvalueReference = !IsLvalueReference<T>;
|
2022-10-14 18:05:46 +00:00
|
|
|
|
|
|
|
template<NotLvalueReference... Ts>
|
2021-04-25 21:04:10 +00:00
|
|
|
struct Variant
|
2021-05-09 03:52:43 +00:00
|
|
|
: public Detail::MergeAndDeduplicatePacks<Detail::VariantConstructors<Ts, Variant<Ts...>>...> {
|
2022-11-09 15:57:26 +00:00
|
|
|
public:
|
2022-12-23 14:18:15 +00:00
|
|
|
using IndexType = Conditional<(sizeof...(Ts) < 255), u8, size_t>; // Note: size+1 reserved for internal value checks
|
2022-11-09 15:57:26 +00:00
|
|
|
private:
|
2021-05-08 19:08:34 +00:00
|
|
|
static constexpr IndexType invalid_index = sizeof...(Ts);
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
static constexpr IndexType index_of() { return Detail::index_of<T, IndexType, Ts...>(); }
|
2021-04-25 21:04:10 +00:00
|
|
|
|
2021-05-08 19:08:34 +00:00
|
|
|
public:
|
2021-06-02 13:41:48 +00:00
|
|
|
template<typename T>
|
|
|
|
static constexpr bool can_contain()
|
|
|
|
{
|
|
|
|
return index_of<T>() != invalid_index;
|
|
|
|
}
|
|
|
|
|
AK: Remove Variant<Ts...>::operator Variant<NewTs...>()
This is an interface to downcast(), which degrades errors into runtime
errors, and allows seemingly-correct-but-not-quite constructs like the
following to appear to compile, but fail at runtime:
Variant<NonnullRefPtr<T>, U> foo = ...;
Variant<RefPtr<T>, U> bar = foo;
The expectation here is that `foo` is converted to a RefPtr<T> if it
contains one, and remains a U otherwise, but in reality, the
NonnullRefPtr<T> variant is simply dropped on the floor, and the
resulting variant becomes invalid, failing the assertion in downcast().
This commit adds a Variant<Ts...>(Variant<NewTs...>) constructor that
ensures that no alternative can be left out at compiletime, for the
users that were using this interface for merely increasing the number of
alternatives (for instance, LibSQL's Value class).
2021-12-24 22:42:54 +00:00
|
|
|
template<typename... NewTs>
|
2022-10-16 22:06:11 +00:00
|
|
|
Variant(Variant<NewTs...>&& old)
|
|
|
|
requires((can_contain<NewTs>() && ...))
|
AK: Remove Variant<Ts...>::operator Variant<NewTs...>()
This is an interface to downcast(), which degrades errors into runtime
errors, and allows seemingly-correct-but-not-quite constructs like the
following to appear to compile, but fail at runtime:
Variant<NonnullRefPtr<T>, U> foo = ...;
Variant<RefPtr<T>, U> bar = foo;
The expectation here is that `foo` is converted to a RefPtr<T> if it
contains one, and remains a U otherwise, but in reality, the
NonnullRefPtr<T> variant is simply dropped on the floor, and the
resulting variant becomes invalid, failing the assertion in downcast().
This commit adds a Variant<Ts...>(Variant<NewTs...>) constructor that
ensures that no alternative can be left out at compiletime, for the
users that were using this interface for merely increasing the number of
alternatives (for instance, LibSQL's Value class).
2021-12-24 22:42:54 +00:00
|
|
|
: Variant(move(old).template downcast<Ts...>())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... NewTs>
|
2022-10-16 22:06:11 +00:00
|
|
|
Variant(Variant<NewTs...> const& old)
|
|
|
|
requires((can_contain<NewTs>() && ...))
|
AK: Remove Variant<Ts...>::operator Variant<NewTs...>()
This is an interface to downcast(), which degrades errors into runtime
errors, and allows seemingly-correct-but-not-quite constructs like the
following to appear to compile, but fail at runtime:
Variant<NonnullRefPtr<T>, U> foo = ...;
Variant<RefPtr<T>, U> bar = foo;
The expectation here is that `foo` is converted to a RefPtr<T> if it
contains one, and remains a U otherwise, but in reality, the
NonnullRefPtr<T> variant is simply dropped on the floor, and the
resulting variant becomes invalid, failing the assertion in downcast().
This commit adds a Variant<Ts...>(Variant<NewTs...>) constructor that
ensures that no alternative can be left out at compiletime, for the
users that were using this interface for merely increasing the number of
alternatives (for instance, LibSQL's Value class).
2021-12-24 22:42:54 +00:00
|
|
|
: Variant(old.template downcast<Ts...>())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-10-14 18:05:46 +00:00
|
|
|
template<NotLvalueReference... NewTs>
|
2021-04-25 21:04:10 +00:00
|
|
|
friend struct Variant;
|
|
|
|
|
2022-10-16 22:06:11 +00:00
|
|
|
Variant()
|
|
|
|
requires(!can_contain<Empty>())
|
|
|
|
= delete;
|
|
|
|
Variant()
|
|
|
|
requires(can_contain<Empty>())
|
2021-09-19 20:10:51 +00:00
|
|
|
: Variant(Empty())
|
|
|
|
{
|
|
|
|
}
|
2021-08-13 13:01:39 +00:00
|
|
|
|
2024-10-30 12:44:20 +00:00
|
|
|
AK_MAKE_CONDITIONALLY_COPYABLE(Variant, <Ts>&&...);
|
|
|
|
AK_MAKE_CONDITIONALLY_MOVABLE(Variant, <Ts>&&...);
|
|
|
|
AK_MAKE_CONDITIONALLY_DESTRUCTIBLE(Variant, <Ts>&&...);
|
2021-07-02 15:42:50 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
ALWAYS_INLINE Variant(Variant const& old)
|
2022-10-16 22:06:11 +00:00
|
|
|
requires(!(IsTriviallyCopyConstructible<Ts> && ...))
|
2021-05-11 13:48:17 +00:00
|
|
|
: Detail::MergeAndDeduplicatePacks<Detail::VariantConstructors<Ts, Variant<Ts...>>...>()
|
2021-05-22 05:16:40 +00:00
|
|
|
, m_data {}
|
2021-05-08 19:08:34 +00:00
|
|
|
, m_index(old.m_index)
|
2021-04-25 21:04:10 +00:00
|
|
|
{
|
2021-05-08 19:08:34 +00:00
|
|
|
Helper::copy_(old.m_index, old.m_data, m_data);
|
2021-04-25 21:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note: A moved-from variant emulates the state of the object it contains
|
|
|
|
// so if a variant containing an int is moved from, it will still contain that int
|
|
|
|
// and if a variant with a nontrivial move ctor is moved from, it may or may not be valid
|
|
|
|
// but it will still contain the "moved-from" state of the object it previously contained.
|
2021-06-09 16:00:07 +00:00
|
|
|
ALWAYS_INLINE Variant(Variant&& old)
|
2022-10-16 22:06:11 +00:00
|
|
|
requires(!(IsTriviallyMoveConstructible<Ts> && ...))
|
2021-05-09 03:52:43 +00:00
|
|
|
: Detail::MergeAndDeduplicatePacks<Detail::VariantConstructors<Ts, Variant<Ts...>>...>()
|
2021-05-08 19:08:34 +00:00
|
|
|
, m_index(old.m_index)
|
2021-04-25 21:04:10 +00:00
|
|
|
{
|
2021-05-08 19:08:34 +00:00
|
|
|
Helper::move_(old.m_index, old.m_data, m_data);
|
2021-04-25 21:04:10 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 16:00:07 +00:00
|
|
|
ALWAYS_INLINE ~Variant()
|
2022-10-16 22:06:11 +00:00
|
|
|
requires(!(IsTriviallyDestructible<Ts> && ...))
|
2021-04-25 21:04:10 +00:00
|
|
|
{
|
2021-05-08 19:08:34 +00:00
|
|
|
Helper::delete_(m_index, m_data);
|
2021-04-25 21:04:10 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
ALWAYS_INLINE Variant& operator=(Variant const& other)
|
2022-10-16 22:06:11 +00:00
|
|
|
requires(!(IsTriviallyCopyConstructible<Ts> && ...) || !(IsTriviallyDestructible<Ts> && ...))
|
2021-04-25 21:04:10 +00:00
|
|
|
{
|
2021-10-31 20:52:26 +00:00
|
|
|
if (this != &other) {
|
|
|
|
if constexpr (!(IsTriviallyDestructible<Ts> && ...)) {
|
|
|
|
Helper::delete_(m_index, m_data);
|
|
|
|
}
|
|
|
|
m_index = other.m_index;
|
|
|
|
Helper::copy_(other.m_index, other.m_data, m_data);
|
2021-07-03 15:12:12 +00:00
|
|
|
}
|
2021-04-25 21:04:10 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-06-09 16:00:07 +00:00
|
|
|
ALWAYS_INLINE Variant& operator=(Variant&& other)
|
2022-10-16 22:06:11 +00:00
|
|
|
requires(!(IsTriviallyMoveConstructible<Ts> && ...) || !(IsTriviallyDestructible<Ts> && ...))
|
2021-04-25 21:04:10 +00:00
|
|
|
{
|
2021-10-31 20:52:26 +00:00
|
|
|
if (this != &other) {
|
|
|
|
if constexpr (!(IsTriviallyDestructible<Ts> && ...)) {
|
|
|
|
Helper::delete_(m_index, m_data);
|
|
|
|
}
|
|
|
|
m_index = other.m_index;
|
|
|
|
Helper::move_(other.m_index, other.m_data, m_data);
|
2021-07-03 15:12:12 +00:00
|
|
|
}
|
2021-04-25 21:04:10 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-05-09 03:52:43 +00:00
|
|
|
using Detail::MergeAndDeduplicatePacks<Detail::VariantConstructors<Ts, Variant<Ts...>>...>::MergeAndDeduplicatePacks;
|
2021-04-25 21:04:10 +00:00
|
|
|
|
2021-06-27 19:22:36 +00:00
|
|
|
template<typename T, typename StrippedT = RemoveCVReference<T>>
|
2022-10-16 22:06:11 +00:00
|
|
|
void set(T&& t)
|
|
|
|
requires(can_contain<StrippedT>() && requires { StrippedT(forward<T>(t)); })
|
2021-04-25 21:04:10 +00:00
|
|
|
{
|
2021-05-08 19:08:34 +00:00
|
|
|
constexpr auto new_index = index_of<StrippedT>();
|
|
|
|
Helper::delete_(m_index, m_data);
|
|
|
|
new (m_data) StrippedT(forward<T>(t));
|
|
|
|
m_index = new_index;
|
2021-04-25 21:04:10 +00:00
|
|
|
}
|
|
|
|
|
2021-06-27 19:22:36 +00:00
|
|
|
template<typename T, typename StrippedT = RemoveCVReference<T>>
|
2022-10-16 22:06:11 +00:00
|
|
|
void set(T&& t, Detail::VariantNoClearTag)
|
|
|
|
requires(can_contain<StrippedT>() && requires { StrippedT(forward<T>(t)); })
|
2021-04-25 21:04:10 +00:00
|
|
|
{
|
2021-05-08 19:08:34 +00:00
|
|
|
constexpr auto new_index = index_of<StrippedT>();
|
|
|
|
new (m_data) StrippedT(forward<T>(t));
|
|
|
|
m_index = new_index;
|
2021-04-25 21:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2022-10-16 22:06:11 +00:00
|
|
|
T* get_pointer()
|
|
|
|
requires(can_contain<T>())
|
2021-04-25 21:04:10 +00:00
|
|
|
{
|
2021-05-08 19:08:34 +00:00
|
|
|
if (index_of<T>() == m_index)
|
|
|
|
return bit_cast<T*>(&m_data);
|
2021-04-25 21:04:10 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2022-10-16 22:06:11 +00:00
|
|
|
T& get()
|
|
|
|
requires(can_contain<T>())
|
2021-04-25 21:04:10 +00:00
|
|
|
{
|
2021-05-08 19:08:34 +00:00
|
|
|
VERIFY(has<T>());
|
|
|
|
return *bit_cast<T*>(&m_data);
|
2021-04-25 21:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2022-10-16 22:06:11 +00:00
|
|
|
T const* get_pointer() const
|
|
|
|
requires(can_contain<T>())
|
2021-04-25 21:04:10 +00:00
|
|
|
{
|
2021-05-08 19:08:34 +00:00
|
|
|
if (index_of<T>() == m_index)
|
2022-10-16 22:06:11 +00:00
|
|
|
return bit_cast<T const*>(&m_data);
|
2021-04-25 21:04:10 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2022-10-16 22:06:11 +00:00
|
|
|
T const& get() const
|
|
|
|
requires(can_contain<T>())
|
2021-04-25 21:04:10 +00:00
|
|
|
{
|
2021-05-08 19:08:34 +00:00
|
|
|
VERIFY(has<T>());
|
2022-10-16 22:06:11 +00:00
|
|
|
return *bit_cast<T const*>(&m_data);
|
2021-04-25 21:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2022-10-16 22:06:11 +00:00
|
|
|
[[nodiscard]] bool has() const
|
|
|
|
requires(can_contain<T>())
|
2021-04-25 21:04:10 +00:00
|
|
|
{
|
2021-05-08 19:08:34 +00:00
|
|
|
return index_of<T>() == m_index;
|
2021-04-25 21:04:10 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 20:10:15 +00:00
|
|
|
bool operator==(Variant const& other) const
|
|
|
|
{
|
|
|
|
return this->visit([&]<typename T>(T const& self) {
|
|
|
|
if (auto const* p = other.get_pointer<T>())
|
|
|
|
return static_cast<T const&>(self) == static_cast<T const&>(*p);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-25 21:04:10 +00:00
|
|
|
template<typename... Fs>
|
2021-06-09 16:00:07 +00:00
|
|
|
ALWAYS_INLINE decltype(auto) visit(Fs&&... functions)
|
2021-04-25 21:04:10 +00:00
|
|
|
{
|
|
|
|
Visitor<Fs...> visitor { forward<Fs>(functions)... };
|
2022-01-13 14:01:00 +00:00
|
|
|
return VisitHelper::visit(*this, m_index, m_data, move(visitor));
|
2021-04-25 21:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Fs>
|
2021-06-09 16:00:07 +00:00
|
|
|
ALWAYS_INLINE decltype(auto) visit(Fs&&... functions) const
|
2021-04-25 21:04:10 +00:00
|
|
|
{
|
|
|
|
Visitor<Fs...> visitor { forward<Fs>(functions)... };
|
2022-01-13 14:01:00 +00:00
|
|
|
return VisitHelper::visit(*this, m_index, m_data, move(visitor));
|
2021-04-25 21:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... NewTs>
|
2022-11-10 09:12:19 +00:00
|
|
|
decltype(auto) downcast() &&
|
2021-04-25 21:04:10 +00:00
|
|
|
{
|
2022-11-10 09:12:19 +00:00
|
|
|
if constexpr (sizeof...(NewTs) == 1 && (IsSpecializationOf<NewTs, Variant> && ...)) {
|
|
|
|
return move(*this).template downcast_variant<NewTs...>();
|
|
|
|
} else {
|
|
|
|
Variant<NewTs...> instance { Variant<NewTs...>::invalid_index, Detail::VariantConstructTag {} };
|
|
|
|
visit([&](auto& value) {
|
|
|
|
if constexpr (Variant<NewTs...>::template can_contain<RemoveCVReference<decltype(value)>>())
|
|
|
|
instance.set(move(value), Detail::VariantNoClearTag {});
|
|
|
|
});
|
|
|
|
VERIFY(instance.m_index != instance.invalid_index);
|
|
|
|
return instance;
|
|
|
|
}
|
2021-04-25 21:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... NewTs>
|
2022-11-10 09:12:19 +00:00
|
|
|
decltype(auto) downcast() const&
|
2021-04-25 21:04:10 +00:00
|
|
|
{
|
2022-11-10 09:12:19 +00:00
|
|
|
if constexpr (sizeof...(NewTs) == 1 && (IsSpecializationOf<NewTs, Variant> && ...)) {
|
2024-06-04 20:19:15 +00:00
|
|
|
return (*this).downcast_variant(TypeWrapper<NewTs...> {});
|
2022-11-10 09:12:19 +00:00
|
|
|
} else {
|
|
|
|
Variant<NewTs...> instance { Variant<NewTs...>::invalid_index, Detail::VariantConstructTag {} };
|
|
|
|
visit([&](auto const& value) {
|
|
|
|
if constexpr (Variant<NewTs...>::template can_contain<RemoveCVReference<decltype(value)>>())
|
|
|
|
instance.set(value, Detail::VariantNoClearTag {});
|
|
|
|
});
|
|
|
|
VERIFY(instance.m_index != instance.invalid_index);
|
|
|
|
return instance;
|
|
|
|
}
|
2021-04-25 21:04:10 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 16:46:08 +00:00
|
|
|
auto index() const { return m_index; }
|
|
|
|
|
2021-04-25 21:04:10 +00:00
|
|
|
private:
|
2022-11-10 09:12:19 +00:00
|
|
|
template<typename... NewTs>
|
|
|
|
Variant<NewTs...> downcast_variant(TypeWrapper<Variant<NewTs...>>) &&
|
|
|
|
{
|
|
|
|
return move(*this).template downcast<NewTs...>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... NewTs>
|
|
|
|
Variant<NewTs...> downcast_variant(TypeWrapper<Variant<NewTs...>>) const&
|
|
|
|
{
|
|
|
|
return (*this).template downcast<NewTs...>();
|
|
|
|
}
|
|
|
|
|
2021-09-06 10:07:50 +00:00
|
|
|
static constexpr auto data_size = Detail::integer_sequence_generate_array<size_t>(0, IntegerSequence<size_t, sizeof(Ts)...>()).max();
|
|
|
|
static constexpr auto data_alignment = Detail::integer_sequence_generate_array<size_t>(0, IntegerSequence<size_t, alignof(Ts)...>()).max();
|
2021-05-08 19:08:34 +00:00
|
|
|
using Helper = Detail::Variant<IndexType, 0, Ts...>;
|
2021-05-19 15:28:27 +00:00
|
|
|
using VisitHelper = Detail::VisitImpl<IndexType, Ts...>;
|
2021-04-25 21:04:10 +00:00
|
|
|
|
2021-05-22 05:16:40 +00:00
|
|
|
template<typename T_, typename U_>
|
|
|
|
friend struct Detail::VariantConstructors;
|
|
|
|
|
2021-05-08 19:08:34 +00:00
|
|
|
explicit Variant(IndexType index, Detail::VariantConstructTag)
|
|
|
|
: Detail::MergeAndDeduplicatePacks<Detail::VariantConstructors<Ts, Variant<Ts...>>...>()
|
|
|
|
, m_index(index)
|
2021-04-25 21:04:10 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-06-09 16:00:07 +00:00
|
|
|
ALWAYS_INLINE void clear_without_destruction()
|
2021-05-22 05:16:40 +00:00
|
|
|
{
|
|
|
|
__builtin_memset(m_data, 0, data_size);
|
|
|
|
m_index = invalid_index;
|
|
|
|
}
|
|
|
|
|
2021-04-25 21:04:10 +00:00
|
|
|
template<typename... Fs>
|
|
|
|
struct Visitor : Fs... {
|
2022-01-13 17:29:13 +00:00
|
|
|
using Types = TypeList<Fs...>;
|
|
|
|
|
2021-04-25 21:04:10 +00:00
|
|
|
Visitor(Fs&&... args)
|
2021-05-19 15:28:27 +00:00
|
|
|
: Fs(forward<Fs>(args))...
|
2021-04-25 21:04:10 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
using Fs::operator()...;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Note: Make sure not to default-initialize!
|
|
|
|
// VariantConstructors::VariantConstructors(T) will set this to the correct value
|
|
|
|
// So default-constructing to anything will leave the first initialization with that value instead of the correct one.
|
2021-06-09 16:00:07 +00:00
|
|
|
alignas(data_alignment) u8 m_data[data_size];
|
2021-05-08 19:08:34 +00:00
|
|
|
IndexType m_index;
|
2021-04-25 21:04:10 +00:00
|
|
|
};
|
|
|
|
|
2022-12-23 14:18:15 +00:00
|
|
|
template<typename... Ts>
|
|
|
|
struct TypeList<Variant<Ts...>> : TypeList<Ts...> { };
|
|
|
|
|
2021-04-25 21:04:10 +00:00
|
|
|
}
|
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2021-04-25 21:04:10 +00:00
|
|
|
using AK::Empty;
|
|
|
|
using AK::Variant;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|