cast: allow cast::known for non-class types

This commit is contained in:
Danny Robson 2021-04-07 12:52:50 +10:00
parent 67c5ef5d78
commit de1d9649a3

View File

@ -144,35 +144,6 @@ namespace cruft::cast {
} }
///////////////////////////////////////////////////////////////////////////
/// Assert if the value is not a pointer to a subclass of T, else return
/// the converted value. Note: this is only a debug-time check and is
/// compiled out in optimised builds.
template <
concepts::pointer T,
typename V
>
T
known (V *const v)
{
CHECK (dynamic_cast<T> (v));
return static_cast<T> (v);
}
//-------------------------------------------------------------------------
template <
concepts::reference T,
typename V
>
T
known (V &v)
{
CHECK_NOTHROW (dynamic_cast<T> (v));
return reinterpret_cast<T> (v);
}
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
/// Cast a pointer from one type to another, asserting that the required /// Cast a pointer from one type to another, asserting that the required
/// alignment of the destination type has been satisfied. /// alignment of the destination type has been satisfied.
@ -196,6 +167,38 @@ namespace cruft::cast {
} }
///////////////////////////////////////////////////////////////////////////
/// Assert if the value is not a pointer to a subclass of T, else return
/// the converted value. Note: this is only a debug-time check and is
/// compiled out in optimised builds.
template <
concepts::pointer T,
typename V
>
T
known (V *const v)
{
if constexpr (std::is_class_v<V>) {
CHECK (dynamic_cast<T> (v));
}
return alignment<T> (v);
}
//-------------------------------------------------------------------------
template <
concepts::reference T,
typename V
>
T
known (V &v)
{
CHECK_NOTHROW (dynamic_cast<T> (v));
return reinterpret_cast<T> (v);
}
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
/// Cast from SrcT to DstT and damn any consequences; just make it compile. /// Cast from SrcT to DstT and damn any consequences; just make it compile.
template <typename DstT, typename SrcT> template <typename DstT, typename SrcT>