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>
|
|
|
|
#include <AK/StdLibExtras.h>
|
|
|
|
|
|
|
|
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...> {}();
|
|
|
|
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-05-08 19:08:34 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-05-08 19:08:34 +00:00
|
|
|
static void copy_(IndexType old_id, const 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(*bit_cast<F*>(old_data));
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Visitor>
|
2021-05-08 19:08:34 +00:00
|
|
|
static void visit_(IndexType id, void* data, Visitor&& visitor)
|
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
|
|
|
visitor(*bit_cast<F*>(data));
|
|
|
|
else
|
2021-05-08 19:08:34 +00:00
|
|
|
Variant<IndexType, InitialIndex + 1, Ts...>::visit_(id, data, forward<Visitor>(visitor));
|
2021-04-25 21:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Visitor>
|
2021-05-08 19:08:34 +00:00
|
|
|
static void visit_(IndexType id, const void* data, Visitor&& visitor)
|
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
|
|
|
visitor(*bit_cast<const F*>(data));
|
|
|
|
else
|
2021-05-08 19:08:34 +00:00
|
|
|
Variant<IndexType, InitialIndex + 1, Ts...>::visit_(id, data, forward<Visitor>(visitor));
|
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> {
|
|
|
|
static void delete_(IndexType, void*) { }
|
|
|
|
static void move_(IndexType, void*, void*) { }
|
|
|
|
static void copy_(IndexType, const void*, void*) { }
|
2021-04-25 21:04:10 +00:00
|
|
|
template<typename Visitor>
|
2021-05-08 19:08:34 +00:00
|
|
|
static void visit_(IndexType, void*, Visitor&&) { }
|
2021-04-25 21:04:10 +00:00
|
|
|
template<typename Visitor>
|
2021-05-08 19:08:34 +00:00
|
|
|
static void visit_(IndexType, const void*, Visitor&&) { }
|
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 {
|
|
|
|
VariantConstructors(T&& t)
|
|
|
|
{
|
|
|
|
internal_cast().template set<T>(forward<T>(t), VariantNoClearTag {});
|
|
|
|
}
|
|
|
|
|
|
|
|
VariantConstructors() { }
|
|
|
|
|
|
|
|
Base& operator=(const T& value)
|
|
|
|
{
|
|
|
|
Base variant { value };
|
|
|
|
internal_cast() = move(variant);
|
|
|
|
return internal_cast();
|
|
|
|
}
|
|
|
|
|
|
|
|
Base& operator=(T&& value)
|
|
|
|
{
|
|
|
|
Base variant { move(value) };
|
|
|
|
internal_cast() = move(variant);
|
|
|
|
return internal_cast();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
[[nodiscard]] Base& internal_cast()
|
|
|
|
{
|
|
|
|
// Warning: Internal type shenanigans - VariantsConstrutors<T, Base> <- Base
|
|
|
|
// Not the other way around, so be _really_ careful not to cause issues.
|
|
|
|
return *reinterpret_cast<Base*>(this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
struct Empty {
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename... Ts>
|
|
|
|
struct Variant
|
|
|
|
: public Detail::VariantConstructors<Ts, Variant<Ts...>>... {
|
2021-05-08 19:08:34 +00:00
|
|
|
private:
|
|
|
|
using IndexType = Conditional<sizeof...(Ts) < 255, u8, size_t>; // Note: size+1 reserved for internal value checks
|
|
|
|
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-04-25 21:04:10 +00:00
|
|
|
template<typename... NewTs>
|
|
|
|
friend struct Variant;
|
|
|
|
|
|
|
|
Variant(const Variant& old)
|
|
|
|
: 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::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.
|
|
|
|
Variant(Variant&& old)
|
|
|
|
: 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
|
|
|
}
|
|
|
|
|
|
|
|
~Variant()
|
|
|
|
{
|
2021-05-08 19:08:34 +00:00
|
|
|
Helper::delete_(m_index, m_data);
|
2021-04-25 21:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Variant& operator=(const Variant& other)
|
|
|
|
{
|
2021-05-08 19:08:34 +00:00
|
|
|
m_index = other.m_index;
|
|
|
|
Helper::copy_(other.m_index, other.m_data, m_data);
|
2021-04-25 21:04:10 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Variant& operator=(Variant&& other)
|
|
|
|
{
|
2021-05-08 19:08:34 +00:00
|
|
|
m_index = other.m_index;
|
|
|
|
Helper::move_(other.m_index, other.m_data, m_data);
|
2021-04-25 21:04:10 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
using Detail::VariantConstructors<Ts, Variant<Ts...>>::VariantConstructors...;
|
|
|
|
|
|
|
|
template<typename T>
|
2021-05-08 19:08:34 +00:00
|
|
|
void set(T&& t) requires(index_of<T>() != invalid_index)
|
2021-04-25 21:04:10 +00:00
|
|
|
{
|
2021-05-08 19:08:34 +00:00
|
|
|
using StrippedT = RemoveCV<RemoveReference<T>>;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void set(T&& t, Detail::VariantNoClearTag)
|
|
|
|
{
|
2021-05-08 19:08:34 +00:00
|
|
|
using StrippedT = RemoveCV<RemoveReference<T>>;
|
|
|
|
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>
|
|
|
|
T* get_pointer()
|
|
|
|
{
|
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>
|
2021-05-08 19:08:34 +00:00
|
|
|
[[gnu::noinline]] T& get()
|
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>
|
2021-05-08 19:08:34 +00:00
|
|
|
[[gnu::noinline]] const T* get_pointer() const
|
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<const T*>(&m_data);
|
2021-04-25 21:04:10 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2021-05-08 19:08:34 +00:00
|
|
|
[[gnu::noinline]] const T& get() const
|
2021-04-25 21:04:10 +00:00
|
|
|
{
|
2021-05-08 19:08:34 +00:00
|
|
|
VERIFY(has<T>());
|
|
|
|
return *bit_cast<const T*>(&m_data);
|
2021-04-25 21:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
[[nodiscard]] bool has() const
|
|
|
|
{
|
2021-05-08 19:08:34 +00:00
|
|
|
return index_of<T>() == m_index;
|
2021-04-25 21:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Fs>
|
|
|
|
void visit(Fs&&... functions)
|
|
|
|
{
|
|
|
|
Visitor<Fs...> visitor { forward<Fs>(functions)... };
|
2021-05-08 19:08:34 +00:00
|
|
|
Helper::visit_(m_index, m_data, visitor);
|
2021-04-25 21:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Fs>
|
|
|
|
void visit(Fs&&... functions) const
|
|
|
|
{
|
|
|
|
Visitor<Fs...> visitor { forward<Fs>(functions)... };
|
2021-05-08 19:08:34 +00:00
|
|
|
Helper::visit_(m_index, m_data, visitor);
|
2021-04-25 21:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... NewTs>
|
|
|
|
Variant<NewTs...> downcast() &&
|
|
|
|
{
|
2021-05-08 19:08:34 +00:00
|
|
|
Variant<NewTs...> instance { Variant<NewTs...>::invalid_index, Detail::VariantConstructTag {} };
|
|
|
|
visit([&](auto& value) {
|
|
|
|
if constexpr (Variant<NewTs...>::template can_contain<RemoveCV<RemoveReference<decltype(value)>>>())
|
|
|
|
instance.set(move(value), Detail::VariantNoClearTag {});
|
|
|
|
});
|
|
|
|
VERIFY(instance.m_index != instance.invalid_index);
|
2021-04-25 21:04:10 +00:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... NewTs>
|
|
|
|
Variant<NewTs...> downcast() &
|
|
|
|
{
|
2021-05-08 19:08:34 +00:00
|
|
|
Variant<NewTs...> instance { Variant<NewTs...>::invalid_index, Detail::VariantConstructTag {} };
|
|
|
|
visit([&](const auto& value) {
|
|
|
|
if constexpr (Variant<NewTs...>::template can_contain<RemoveCV<RemoveReference<decltype(value)>>>())
|
|
|
|
instance.set(value, Detail::VariantNoClearTag {});
|
|
|
|
});
|
|
|
|
VERIFY(instance.m_index != instance.invalid_index);
|
2021-04-25 21:04:10 +00:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2021-05-08 19:08:34 +00:00
|
|
|
template<typename T>
|
|
|
|
static constexpr bool can_contain()
|
|
|
|
{
|
|
|
|
return index_of<T>() != invalid_index;
|
|
|
|
}
|
|
|
|
|
2021-04-25 21:04:10 +00:00
|
|
|
private:
|
|
|
|
static constexpr auto data_size = integer_sequence_generate_array<size_t>(0, IntegerSequence<size_t, sizeof(Ts)...>()).max();
|
|
|
|
static constexpr auto data_alignment = 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-04-25 21:04:10 +00:00
|
|
|
|
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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Fs>
|
|
|
|
struct Visitor : Fs... {
|
|
|
|
Visitor(Fs&&... args)
|
|
|
|
: Fs(args)...
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
using Fs::operator()...;
|
|
|
|
};
|
|
|
|
|
|
|
|
alignas(data_alignment) u8 m_data[data_size];
|
|
|
|
// 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-05-08 19:08:34 +00:00
|
|
|
IndexType m_index;
|
2021-04-25 21:04:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::Empty;
|
|
|
|
using AK::Variant;
|