AuROXTL/Include/auROXTL/auCastUtils.hpp

148 lines
5.0 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 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);
}
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);
}
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)
{
return static_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> && AuIsRValueReference_v<Z>)>
static constexpr AuConditional_t<AuIsReference_v<T>, T, T &&> AuStaticCast(Z other)
{
return static_cast<AuConditional_t<AuIsReference_v<T>, T, T &&>>(AuMove(other));
}
template <class T, class Z, AU_TEMPLATE_ENABLE_WHEN(!AuIsPointer_v<Z> && !AuIsReference_v<Z>)>
static constexpr T AuStaticCast(Z other)
{
return static_cast<T>(other);
}
template <class T, class Z, AU_TEMPLATE_ENABLE_WHEN(!AuIsBaseOfTemplate_v<AURORA_RUNTIME_AU_SHARED_PTR, Z> &&AuIsRValueReference_v<Z>)>
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>
static constexpr AuConditional_t<AuIsClass_v<T> && !AuIsPointer_v<T> && !AuIsReference_v<Z>, T *, T> AuReinterpretCast(Z other)
{
return reinterpret_cast<AuConditional_t<AuIsClass_v<T> && !AuIsPointer_v<T> && !AuIsReference_v<Z>, T *, T>>(other);
}
template <class T, class Z>
static constexpr AuConditional_t<AuIsClass_v<T> && !AuIsPointer_v<T> && !AuIsReference_v<Z>, const T *, const T> AuReinterpretCast(const Z *other)
{
return reinterpret_cast<AuConditional_t<AuIsClass_v<T> && !AuIsPointer_v<T> && !AuIsReference_v<Z>, const AuRemoveConst_t<T> *, const AuRemoveConst_t<T>>>(other);
}
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()));
}