2022-04-01 04:06:53 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: auCastUtils.hpp
|
|
|
|
Date: 2022-2-10
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
template <class T, class Z>
|
|
|
|
static constexpr AuSPtr<T> AuStaticCast(const AuSPtr<Z> &other)
|
|
|
|
{
|
|
|
|
return AuSPtr<T>(other, static_cast<T *>(other.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class Z>
|
|
|
|
static constexpr AuSPtr<T> AuStaticCast(AuSPtr<Z> &&other)
|
|
|
|
{
|
|
|
|
return AuSPtr<T>(AuMove(other), static_cast<T *>(other.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class Z>
|
|
|
|
static constexpr AuConditional_t<AuIsPointer_v<T>, T, T *> AuStaticCast(Z *other)
|
|
|
|
{
|
|
|
|
return static_cast<AuConditional_t<AuIsPointer_v<T>, T, T *>>(other);
|
|
|
|
}
|
|
|
|
|
2024-05-19 04:01:56 +00:00
|
|
|
template <class T, class Z>
|
|
|
|
static constexpr AuConditional_t<AuIsPointer_v<T>, const AuRemoveConst_t<T>, const AuRemoveConst_t<T> *> AuStaticCast(const Z *other)
|
|
|
|
{
|
|
|
|
return static_cast<AuConditional_t<AuIsPointer_v<T>, const AuRemoveConst_t<T>, const AuRemoveConst_t<T> *>>(other);
|
|
|
|
}
|
|
|
|
|
2022-05-02 17:16:52 +00:00
|
|
|
template <class T, class Z, AU_TEMPLATE_ENABLE_WHEN(!AuIsBaseOfTemplate_v<AURORA_RUNTIME_AU_SHARED_PTR, Z> && AuIsLValueReference_v<Z>)>
|
|
|
|
static constexpr AuConditional_t<AuIsReference_v<T>, T, T &> AuStaticCast(Z other)
|
2022-04-01 04:06:53 +00:00
|
|
|
{
|
|
|
|
return static_cast<AuConditional_t<AuIsReference_v<T>, T, T &>>(AuForward(other));
|
|
|
|
}
|
|
|
|
|
2022-05-02 17:16:52 +00:00
|
|
|
template <class T, class Z, AU_TEMPLATE_ENABLE_WHEN(!AuIsBaseOfTemplate_v<AURORA_RUNTIME_AU_SHARED_PTR, Z> && AuIsRValueReference_v<Z>)>
|
|
|
|
static constexpr AuConditional_t<AuIsReference_v<T>, T, T &&> AuStaticCast(Z other)
|
2022-04-01 04:06:53 +00:00
|
|
|
{
|
|
|
|
return static_cast<AuConditional_t<AuIsReference_v<T>, T, T &&>>(AuMove(other));
|
|
|
|
}
|
|
|
|
|
2022-05-02 17:16:52 +00:00
|
|
|
template <class T, class Z, AU_TEMPLATE_ENABLE_WHEN(!AuIsPointer_v<Z> && !AuIsReference_v<Z>)>
|
2022-04-01 04:06:53 +00:00
|
|
|
static constexpr T AuStaticCast(Z other)
|
|
|
|
{
|
|
|
|
return static_cast<T>(other);
|
|
|
|
}
|
|
|
|
|
2022-05-20 23:51:05 +00:00
|
|
|
template <class T, class Z, AU_TEMPLATE_ENABLE_WHEN(!AuIsBaseOfTemplate_v<AURORA_RUNTIME_AU_SHARED_PTR, Z> &&AuIsRValueReference_v<Z>)>
|
2022-04-01 04:06:53 +00:00
|
|
|
static constexpr AuConditional_t<AuIsPointer_v<T>, T, T *> AuConstCast(Z *other)
|
|
|
|
{
|
|
|
|
return const_cast<AuConditional_t<AuIsPointer_v<T>, T, T *>>(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class Z, AU_TEMPLATE_ENABLE_WHEN(!AuIsBaseOfTemplate_v<AURORA_RUNTIME_AU_SHARED_PTR, Z>)>
|
|
|
|
static constexpr AuConditional_t<AuIsReference_v<T>, T, T &> AuConstCast(Z &other)
|
|
|
|
{
|
|
|
|
return const_cast<AuConditional_t<AuIsReference_v<T>, T, T &>>(AuForward(other));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class Z, AU_TEMPLATE_ENABLE_WHEN(!AuIsBaseOfTemplate_v<AURORA_RUNTIME_AU_SHARED_PTR, Z>)>
|
|
|
|
static constexpr AuConditional_t<AuIsReference_v<T>, T, T &&> AuConstCast(Z &&other)
|
|
|
|
{
|
|
|
|
return const_cast<AuConditional_t<AuIsReference_v<T>, T, T &&>>(AuMove(other));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class Z>
|
|
|
|
static constexpr AuSPtr<T> AuConstCast(const AuSPtr<Z> &other)
|
|
|
|
{
|
|
|
|
return AuSPtr<T>(other, const_cast<T *>(other.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class Z>
|
|
|
|
static constexpr AuSPtr<T> AuConstCast(AuSPtr<Z> &&other)
|
|
|
|
{
|
|
|
|
return AuSPtr<T>(AuMove(other), const_cast<T *>(other.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class Z>
|
2022-05-20 23:51:05 +00:00
|
|
|
static constexpr AuConditional_t<AuIsClass_v<T> && !AuIsPointer_v<T> && !AuIsReference_v<Z>, T *, T> AuReinterpretCast(Z other)
|
2022-04-01 04:06:53 +00:00
|
|
|
{
|
2022-05-20 23:51:05 +00:00
|
|
|
return reinterpret_cast<AuConditional_t<AuIsClass_v<T> && !AuIsPointer_v<T> && !AuIsReference_v<Z>, T *, T>>(other);
|
2022-04-01 04:06:53 +00:00
|
|
|
}
|
|
|
|
|
2024-05-19 04:01:56 +00:00
|
|
|
template <class T, class Z>
|
2024-06-19 22:01:45 +00:00
|
|
|
static constexpr AuConditional_t<AuIsClass_v<T> && !AuIsPointer_v<T> && !AuIsReference_v<Z>, const T *, const T> AuReinterpretCast(const Z *other)
|
2024-05-19 04:01:56 +00:00
|
|
|
{
|
2024-06-19 22:01:45 +00:00
|
|
|
return reinterpret_cast<AuConditional_t<AuIsClass_v<T> && !AuIsPointer_v<T> && !AuIsReference_v<Z>, const AuRemoveConst_t<T> *, const AuRemoveConst_t<T>>>(other);
|
2024-05-19 04:01:56 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 04:06:53 +00:00
|
|
|
template <class T, class Z>
|
|
|
|
static constexpr AuSPtr<T> AuReinterpretCast(const AuSPtr<Z> &other)
|
|
|
|
{
|
|
|
|
return AuSPtr<T>(other, reinterpret_cast<T *>(other.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class Z>
|
|
|
|
static constexpr AuSPtr<T> AuReinterpretCast(AuSPtr<Z> &&other)
|
|
|
|
{
|
|
|
|
return AuSPtr<T>(AuMove(other), reinterpret_cast<T *>(other.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class Z>
|
|
|
|
static T *AuDynamicCast(Z *other)
|
|
|
|
{
|
|
|
|
return dynamic_cast<T *>(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class Z>
|
|
|
|
static AuSPtr<T> AuDynamicCast(const AuSPtr<Z> &other)
|
|
|
|
{
|
|
|
|
return AuSPtr<T>(other, dynamic_cast<T *>(other.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class Z>
|
|
|
|
static AuSPtr<T> AuDynamicCast(AuSPtr<Z> &&other)
|
|
|
|
{
|
|
|
|
return AuSPtr<T>(AuMove(other), dynamic_cast<T *>(other.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, typename Z>
|
|
|
|
static AuOptional<AuSPtr<T>> AuOptionalSharedDynamicCast(AuOptional<AuSPtr<Z>> &in)
|
|
|
|
{
|
|
|
|
if (!in.has_value()) return {};
|
|
|
|
return AuDynamicCast<T>(in.value());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, typename Z>
|
|
|
|
static AuOptional<AuSPtr<T>> AuOptionalSharedStaticCast(AuOptional<AuSPtr<Z>> &in)
|
|
|
|
{
|
|
|
|
if (!in.has_value()) return {};
|
|
|
|
return AuStaticPointerCast<T>(in.value());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class T2>
|
|
|
|
static AuSPtr<T> AuStaticPointerCast(const AuSPtr<T2> &other) noexcept
|
|
|
|
{
|
|
|
|
return AuSPtr<T>(other, static_cast<typename AuSPtr<T>::element_type *>(other.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class T2>
|
|
|
|
static AuSPtr<T> AuStaticPointerCast(AuSPtr<T2> &&other) noexcept
|
|
|
|
{
|
|
|
|
return AuSPtr<T>(AuMove(other), static_cast<typename AuSPtr<T>::element_type *>(other.get()));
|
|
|
|
}
|