AuroraRuntime/Include/auROXTL/auCastUtils.hpp
2022-03-11 20:33:41 +00:00

136 lines
3.9 KiB
C++

/***
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 AuSPtr<T> AuStaticCast(const AuSPtr<Z> &other)
{
return AuSPtr<T>(other, static_cast<T *>(other.get()));
}
template<class T, class Z>
static AuSPtr<T> AuStaticCast(AuSPtr<Z> &&other)
{
return AuSPtr<T>(AuMove(other), static_cast<T *>(other.get()));
}
template<class T, class Z>
static AuConditional_t<AuIsPointer_v<T>, T, T *> AuStaticCast(Z *other)
{
return static_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>)>
AuConditional_t<AuIsReference_v<T>, T, T &> AuStaticCast(Z &other)
{
return static_cast<AuConditional_t<AuIsReference_v<T>, T, T &>>(other);
}
template<class T, class Z, AU_TEMPLATE_ENABLE_WHEN(!AuIsBaseOfTemplate_v<AURORA_RUNTIME_AU_SHARED_PTR, Z>)>
static AuConditional_t<AuIsReference_v<T>, T, T &&> AuStaticCast(Z &&other)
{
return static_cast<AuConditional_t<AuIsReference_v<T>, T, T &&>>(other);
}
template<class T, class Z, AU_TEMPLATE_ENABLE_WHEN(!AuIsPointer_v<T> && !AuIsPointer_v<Z>)>
static T AuStaticCast(Z other)
{
return static_cast<T>(other);
}
template<class T, class Z>
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>)>
AuConditional_t<AuIsReference_v<T>, T, T &> AuConstCast(Z &other)
{
return const_cast<AuConditional_t<AuIsReference_v<T>, T, T &>>(other);
}
template<class T, class Z, AU_TEMPLATE_ENABLE_WHEN(!AuIsBaseOfTemplate_v<AURORA_RUNTIME_AU_SHARED_PTR, Z>)>
AuConditional_t<AuIsReference_v<T>, T, T &&> AuConstCast(Z &&other)
{
return const_cast<AuConditional_t<AuIsReference_v<T>, T, T &&>>(other);
}
template<class T, class Z>
AuSPtr<T> AuConstCast(const AuSPtr<Z> &other)
{
return AuSPtr<T>(other, const_cast<T *>(other.get()));
}
template<class T, class Z>
AuSPtr<T> AuConstCast(AuSPtr<Z> &&other)
{
return AuSPtr<T>(AuMove(other), const_cast<T *>(other.get()));
}
template<class T, class Z>
AuConditional_t<AuIsClass_v<T> && !AuIsPointer_v<T> && AuIsPointer_v<Z>, T *, T> AuReinterpretCast(Z other)
{
return reinterpret_cast<AuConditional_t<AuIsClass_v<T> && !AuIsPointer_v<T> &&AuIsPointer_v<Z>, T *, T>>(other);
}
template<class T, class Z>
AuSPtr<T> AuReinterpretCast(const AuSPtr<Z> &other)
{
return AuSPtr<T>(other, reinterpret_cast<T *>(other.get()));
}
template<class T, class Z>
AuSPtr<T> AuReinterpretCast(AuSPtr<Z> &&other)
{
return AuSPtr<T>(AuMove(other), reinterpret_cast<T *>(other.get()));
}
template<class T, class Z>
T *AuDynamicCast(Z *other)
{
return dynamic_cast<T *>(other);
}
template<class T, class Z>
AuSPtr<T> AuDynamicCast(const AuSPtr<Z> &other)
{
return AuSPtr<T>(other, dynamic_cast<T *>(other.get()));
}
template<class T, class Z>
AuSPtr<T> AuDynamicCast(AuSPtr<Z> &&other)
{
return AuSPtr<T>(AuMove(other), dynamic_cast<T *>(other.get()));
}
template<typename T, typename Z>
AuOptional<AuSPtr<T>> AuOptionalSharedDynamicCast(AuOptional<AuSPtr<Z>> &in)
{
if (!in.has_value()) return {};
return AuDynamicCast<T>(in.value());
}
template<typename T, typename Z>
AuOptional<AuSPtr<T>> AuOptionalSharedStaticCast(AuOptional<AuSPtr<Z>> &in)
{
if (!in.has_value()) return {};
return AuStaticPointerCast<T>(in.value());
}
template<class T, class T2>
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>
AuSPtr<T> AuStaticPointerCast(AuSPtr<T2> &&other) noexcept
{
return AuSPtr<T>(AuMove(other), static_cast<typename AuSPtr<T>::element_type *>(other.get()));
}