2020-07-26 11:25:02 -03:00
|
|
|
/*
|
2024-10-04 08:19:50 -03:00
|
|
|
* Copyright (c) 2020-2021, Andreas Kling <andreas@ladybird.org>
|
2020-07-26 11:25:02 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-26 11:25:02 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2021-02-10 04:25:09 -03:00
|
|
|
#include <AK/Assertions.h>
|
2024-08-08 10:21:13 -03:00
|
|
|
#include <AK/Concepts.h>
|
2022-12-10 17:29:55 -03:00
|
|
|
#include <AK/Forward.h>
|
2021-01-17 10:34:01 -03:00
|
|
|
#include <AK/Platform.h>
|
2020-07-26 11:25:02 -03:00
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
|
|
template<typename OutputType, typename InputType>
|
2021-01-17 10:34:01 -03:00
|
|
|
ALWAYS_INLINE bool is(InputType& input)
|
2020-07-26 11:25:02 -03:00
|
|
|
{
|
2025-08-22 11:35:27 -03:00
|
|
|
static_assert(!SameAs<RemoveCVReference<OutputType>, RemoveCVReference<InputType>>);
|
2025-09-21 09:13:01 -03:00
|
|
|
if constexpr (requires { input.template fast_is<RemoveCVReference<OutputType>>(); }) {
|
|
|
|
|
return input.template fast_is<RemoveCVReference<OutputType>>();
|
2021-01-17 10:34:01 -03:00
|
|
|
}
|
2021-01-01 11:33:30 -03:00
|
|
|
return dynamic_cast<CopyConst<InputType, OutputType>*>(&input);
|
2020-07-26 11:25:02 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename OutputType, typename InputType>
|
2021-01-17 10:34:01 -03:00
|
|
|
ALWAYS_INLINE bool is(InputType* input)
|
2020-07-26 11:25:02 -03:00
|
|
|
{
|
2021-01-01 11:33:30 -03:00
|
|
|
return input && is<OutputType>(*input);
|
2020-07-26 11:25:02 -03:00
|
|
|
}
|
|
|
|
|
|
2022-12-10 17:29:55 -03:00
|
|
|
template<typename OutputType, typename InputType>
|
|
|
|
|
ALWAYS_INLINE bool is(NonnullRefPtr<InputType> const& input)
|
|
|
|
|
{
|
|
|
|
|
return is<OutputType>(*input);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-20 13:17:23 -03:00
|
|
|
template<typename OutputType, typename InputType>
|
|
|
|
|
ALWAYS_INLINE CopyConst<InputType, OutputType>* as_if(InputType& input)
|
|
|
|
|
{
|
2026-05-07 18:16:39 -03:00
|
|
|
if constexpr (requires { input.template fast_as<RemoveCVReference<OutputType>>(); }) {
|
|
|
|
|
return input.template fast_as<RemoveCVReference<OutputType>>();
|
|
|
|
|
} else if constexpr (requires { input.template fast_is<RemoveCVReference<OutputType>>(); static_cast<CopyConst<InputType, OutputType>*>(&input); }) {
|
2025-12-15 22:47:29 -03:00
|
|
|
if (!is<OutputType>(input))
|
|
|
|
|
return nullptr;
|
2025-01-31 06:27:05 -03:00
|
|
|
return static_cast<CopyConst<InputType, OutputType>*>(&input);
|
|
|
|
|
}
|
2025-08-22 11:33:35 -03:00
|
|
|
return dynamic_cast<CopyConst<InputType, OutputType>*>(&input);
|
2025-01-20 13:17:23 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename OutputType, typename InputType>
|
|
|
|
|
ALWAYS_INLINE CopyConst<InputType, OutputType>* as_if(InputType* input)
|
|
|
|
|
{
|
|
|
|
|
if (!input)
|
|
|
|
|
return nullptr;
|
|
|
|
|
return as_if<OutputType>(*input);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-31 08:07:36 -03:00
|
|
|
template<typename OutputType, typename InputType>
|
|
|
|
|
ALWAYS_INLINE CopyConst<InputType, OutputType>& as(InputType& input)
|
|
|
|
|
{
|
|
|
|
|
auto* result = as_if<OutputType>(input);
|
|
|
|
|
VERIFY(result);
|
|
|
|
|
return *result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename OutputType, typename InputType>
|
|
|
|
|
ALWAYS_INLINE CopyConst<InputType, OutputType>* as(InputType* input)
|
|
|
|
|
{
|
|
|
|
|
if (!input)
|
|
|
|
|
return nullptr;
|
|
|
|
|
auto* result = as_if<OutputType>(input);
|
|
|
|
|
VERIFY(result);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-16 20:08:40 -03:00
|
|
|
template<typename OutputType, typename InputType>
|
|
|
|
|
ALWAYS_INLINE CopyConst<InputType, OutputType>* bridge_cast(InputType input)
|
|
|
|
|
{
|
|
|
|
|
#ifdef AK_HAS_OBJC_ARC
|
|
|
|
|
return (__bridge CopyConst<InputType, OutputType>*)(input);
|
|
|
|
|
#else
|
|
|
|
|
return static_cast<CopyConst<InputType, OutputType>*>(input);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-26 11:25:02 -03:00
|
|
|
}
|
|
|
|
|
|
2022-11-26 08:18:30 -03:00
|
|
|
#if USING_AK_GLOBALLY
|
2025-01-21 11:12:05 -03:00
|
|
|
using AK::as;
|
2025-01-20 13:17:23 -03:00
|
|
|
using AK::as_if;
|
2025-03-16 20:08:40 -03:00
|
|
|
using AK::bridge_cast;
|
2020-07-26 11:25:02 -03:00
|
|
|
using AK::is;
|
2022-11-26 08:18:30 -03:00
|
|
|
#endif
|