2020-07-26 14:25:02 +00:00
|
|
|
/*
|
2021-01-17 13:34:01 +00:00
|
|
|
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
2020-07-26 14:25:02 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-26 14:25:02 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-02-10 07:25:09 +00:00
|
|
|
#include <AK/Assertions.h>
|
2022-12-10 20:29:55 +00:00
|
|
|
#include <AK/Forward.h>
|
2021-01-17 13:34:01 +00:00
|
|
|
#include <AK/Platform.h>
|
2020-07-26 14:25:02 +00:00
|
|
|
#include <AK/StdLibExtras.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<typename OutputType, typename InputType>
|
2021-01-17 13:34:01 +00:00
|
|
|
ALWAYS_INLINE bool is(InputType& input)
|
2020-07-26 14:25:02 +00:00
|
|
|
{
|
2021-01-17 13:34:01 +00:00
|
|
|
if constexpr (requires { input.template fast_is<OutputType>(); }) {
|
|
|
|
return input.template fast_is<OutputType>();
|
|
|
|
}
|
2021-01-01 14:33:30 +00:00
|
|
|
return dynamic_cast<CopyConst<InputType, OutputType>*>(&input);
|
2020-07-26 14:25:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename OutputType, typename InputType>
|
2021-01-17 13:34:01 +00:00
|
|
|
ALWAYS_INLINE bool is(InputType* input)
|
2020-07-26 14:25:02 +00:00
|
|
|
{
|
2021-01-01 14:33:30 +00:00
|
|
|
return input && is<OutputType>(*input);
|
2020-07-26 14:25:02 +00:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:29:55 +00:00
|
|
|
template<typename OutputType, typename InputType>
|
|
|
|
ALWAYS_INLINE bool is(NonnullRefPtr<InputType> const& input)
|
|
|
|
{
|
|
|
|
return is<OutputType>(*input);
|
|
|
|
}
|
|
|
|
|
2020-07-26 14:25:02 +00:00
|
|
|
template<typename OutputType, typename InputType>
|
2021-06-24 17:53:42 +00:00
|
|
|
ALWAYS_INLINE CopyConst<InputType, OutputType>* verify_cast(InputType* input)
|
2020-07-26 14:25:02 +00:00
|
|
|
{
|
2021-04-10 13:59:06 +00:00
|
|
|
static_assert(IsBaseOf<InputType, OutputType>);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!input || is<OutputType>(*input));
|
2020-07-26 14:25:02 +00:00
|
|
|
return static_cast<CopyConst<InputType, OutputType>*>(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename OutputType, typename InputType>
|
2021-06-24 17:53:42 +00:00
|
|
|
ALWAYS_INLINE CopyConst<InputType, OutputType>& verify_cast(InputType& input)
|
2020-07-26 14:25:02 +00:00
|
|
|
{
|
2021-04-10 13:59:06 +00:00
|
|
|
static_assert(IsBaseOf<InputType, OutputType>);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(is<OutputType>(input));
|
2020-07-26 14:25:02 +00:00
|
|
|
return static_cast<CopyConst<InputType, OutputType>&>(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2020-07-26 14:25:02 +00:00
|
|
|
using AK::is;
|
2021-06-24 17:53:42 +00:00
|
|
|
using AK::verify_cast;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|