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>
|
2021-01-17 16:34:01 +03:00
|
|
|
#include <AK/Platform.h>
|
2020-07-26 17:25:02 +03:00
|
|
|
#include <AK/StdLibExtras.h>
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::is;
|
2021-06-24 20:53:42 +03:00
|
|
|
using AK::verify_cast;
|