mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
AK: Add explicit Variant conversion operators
This allows converting between Variants of different types with less pain.
This commit is contained in:
parent
dff3439ad0
commit
bda19a9ff3
Notes:
sideshowbarker
2024-07-18 11:27:46 +09:00
Author: https://github.com/alimpfard Commit: https://github.com/SerenityOS/serenity/commit/bda19a9ff3a Pull-request: https://github.com/SerenityOS/serenity/pull/8264 Reviewed-by: https://github.com/Lubrsi ✅
1 changed files with 13 additions and 1 deletions
14
AK/Variant.h
14
AK/Variant.h
|
@ -331,7 +331,7 @@ public:
|
|||
}
|
||||
|
||||
template<typename... NewTs>
|
||||
Variant<NewTs...> downcast() &
|
||||
Variant<NewTs...> downcast() const&
|
||||
{
|
||||
Variant<NewTs...> instance { Variant<NewTs...>::invalid_index, Detail::VariantConstructTag {} };
|
||||
visit([&](const auto& value) {
|
||||
|
@ -342,6 +342,18 @@ public:
|
|||
return instance;
|
||||
}
|
||||
|
||||
template<typename... NewTs>
|
||||
explicit operator Variant<NewTs...>() &&
|
||||
{
|
||||
return downcast<NewTs...>();
|
||||
}
|
||||
|
||||
template<typename... NewTs>
|
||||
explicit operator Variant<NewTs...>() const&
|
||||
{
|
||||
return downcast<NewTs...>();
|
||||
}
|
||||
|
||||
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();
|
||||
|
|
Loading…
Reference in a new issue