mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 09:00:22 +00:00
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:
parent
4da913bfab
commit
65fa0c2774
Notes:
sideshowbarker
2024-07-18 23:08:38 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/65fa0c27747
1 changed files with 9 additions and 5 deletions
|
@ -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));
|
||||
|
|
Loading…
Reference in a new issue