AK: Allow as to perform dynamic cast where necessary

This aligns the behavior of `as` with `is` and `as_if`.
This commit is contained in:
Tim Ledbetter 2025-01-31 11:07:36 +00:00 committed by Alexander Kalenik
parent 1f87a09503
commit fb56da4144

View File

@ -35,22 +35,6 @@ ALWAYS_INLINE bool is(NonnullRefPtr<InputType> const& input)
return is<OutputType>(*input);
}
template<typename OutputType, typename InputType>
ALWAYS_INLINE CopyConst<InputType, OutputType>* as(InputType* input)
{
static_assert(IsBaseOf<InputType, OutputType>);
VERIFY(!input || is<OutputType>(*input));
return static_cast<CopyConst<InputType, OutputType>*>(input);
}
template<typename OutputType, typename InputType>
ALWAYS_INLINE CopyConst<InputType, OutputType>& as(InputType& input)
{
static_assert(IsBaseOf<InputType, OutputType>);
VERIFY(is<OutputType>(input));
return static_cast<CopyConst<InputType, OutputType>&>(input);
}
template<typename OutputType, typename InputType>
ALWAYS_INLINE CopyConst<InputType, OutputType>* as_if(InputType& input)
{
@ -71,6 +55,24 @@ ALWAYS_INLINE CopyConst<InputType, OutputType>* as_if(InputType* input)
return as_if<OutputType>(*input);
}
template<typename OutputType, typename InputType>
ALWAYS_INLINE CopyConst<InputType, OutputType>& as(InputType& input)
{
auto* result = as_if<OutputType>(input);
VERIFY(result);
return *result;
}
template<typename OutputType, typename InputType>
ALWAYS_INLINE CopyConst<InputType, OutputType>* as(InputType* input)
{
if (!input)
return nullptr;
auto* result = as_if<OutputType>(input);
VERIFY(result);
return result;
}
}
#if USING_AK_GLOBALLY