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