mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-09 09:34:57 +09:00
AK+Everywhere: Make Variant::visit() respect the Variant's constness
...and fix all the instances of visit() taking non-const arguments.
This commit is contained in:
parent
d55c130df5
commit
9de33629da
Notes:
sideshowbarker
2024-07-17 20:55:23 +09:00
Author: https://github.com/alimpfard
Commit: 9de33629da
Pull-request: https://github.com/SerenityOS/serenity/pull/11866
7 changed files with 44 additions and 31 deletions
12
AK/Variant.h
12
AK/Variant.h
|
@ -80,16 +80,16 @@ struct Variant<IndexType, InitialIndex> {
|
|||
|
||||
template<typename IndexType, typename... Ts>
|
||||
struct VisitImpl {
|
||||
template<typename Visitor, IndexType CurrentIndex = 0>
|
||||
ALWAYS_INLINE static constexpr decltype(auto) visit(IndexType id, const void* data, Visitor&& visitor) requires(CurrentIndex < sizeof...(Ts))
|
||||
template<typename Self, typename Visitor, IndexType CurrentIndex = 0>
|
||||
ALWAYS_INLINE static constexpr decltype(auto) visit(Self& self, IndexType id, const void* data, Visitor&& visitor) requires(CurrentIndex < sizeof...(Ts))
|
||||
{
|
||||
using T = typename TypeList<Ts...>::template Type<CurrentIndex>;
|
||||
|
||||
if (id == CurrentIndex)
|
||||
return visitor(*bit_cast<T*>(data));
|
||||
return visitor(*bit_cast<CopyConst<Self, T>*>(data));
|
||||
|
||||
if constexpr ((CurrentIndex + 1) < sizeof...(Ts))
|
||||
return visit<Visitor, CurrentIndex + 1>(id, data, forward<Visitor>(visitor));
|
||||
return visit<Self, Visitor, CurrentIndex + 1>(self, id, data, forward<Visitor>(visitor));
|
||||
else
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
@ -367,14 +367,14 @@ public:
|
|||
ALWAYS_INLINE decltype(auto) visit(Fs&&... functions)
|
||||
{
|
||||
Visitor<Fs...> visitor { forward<Fs>(functions)... };
|
||||
return VisitHelper::visit(m_index, m_data, move(visitor));
|
||||
return VisitHelper::visit(*this, m_index, m_data, move(visitor));
|
||||
}
|
||||
|
||||
template<typename... Fs>
|
||||
ALWAYS_INLINE decltype(auto) visit(Fs&&... functions) const
|
||||
{
|
||||
Visitor<Fs...> visitor { forward<Fs>(functions)... };
|
||||
return VisitHelper::visit(m_index, m_data, move(visitor));
|
||||
return VisitHelper::visit(*this, m_index, m_data, move(visitor));
|
||||
}
|
||||
|
||||
template<typename... NewTs>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue