AK: Make is<T>(input) use input.fast_is<T>() if available

This allows classes to provide an optimized is<T> via the fast_is<T>()
member function.
This commit is contained in:
Andreas Kling 2021-01-17 14:34:01 +01:00
parent 4da913bfab
commit 65fa0c2774
Notes: sideshowbarker 2024-07-18 23:08:38 +09:00

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in input and binary forms, with or without
@ -26,24 +26,28 @@
#pragma once
#include <AK/Platform.h>
#include <AK/StdLibExtras.h>
namespace AK {
template<typename OutputType, typename InputType>
inline bool is(InputType& input)
ALWAYS_INLINE bool is(InputType& input)
{
if constexpr (requires { input.template fast_is<OutputType>(); }) {
return input.template fast_is<OutputType>();
}
return dynamic_cast<CopyConst<InputType, OutputType>*>(&input);
}
template<typename OutputType, typename InputType>
inline bool is(InputType* input)
ALWAYS_INLINE bool is(InputType* input)
{
return input && is<OutputType>(*input);
}
template<typename OutputType, typename InputType>
inline CopyConst<InputType, OutputType>* downcast(InputType* input)
ALWAYS_INLINE CopyConst<InputType, OutputType>* downcast(InputType* input)
{
static_assert(IsBaseOf<InputType, OutputType>::value);
ASSERT(!input || is<OutputType>(*input));
@ -51,7 +55,7 @@ inline CopyConst<InputType, OutputType>* downcast(InputType* input)
}
template<typename OutputType, typename InputType>
inline CopyConst<InputType, OutputType>& downcast(InputType& input)
ALWAYS_INLINE CopyConst<InputType, OutputType>& downcast(InputType& input)
{
static_assert(IsBaseOf<InputType, OutputType>::value);
ASSERT(is<OutputType>(input));