libcruft-util/cast.hpp

182 lines
5.1 KiB
C++

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2011-2018 Danny Robson <danny@nerdcruft.net>
*/
#pragma once
#include "debug.hpp"
#include "platform.hpp"
#include <type_traits>
#include <limits>
namespace cruft::cast {
///////////////////////////////////////////////////////////////////////////
/// Safely cast a numeric type to its (un)signed counterpart, aborting if
/// the dynamically checked result is not representable. May be optimised
/// out if NDEBUG is defined.
///
/// The signed/unsigned and unsigned/signed cases are split so we can
/// simplify the out of range tests.
///
/// The same-type case is not provided because we want to error out on
/// unnecessary casts.
template <
typename T,
typename U
>
std::enable_if_t<
sizeof(T) == sizeof(U) &&
std::is_unsigned<T>::value &&
std::is_signed<U>::value,
T
>
sign (const U u)
{
CHECK_GE (u, 0);
return static_cast<T> (u);
}
//-------------------------------------------------------------------------
template <
typename T,
typename U
>
std::enable_if_t<
sizeof(T) == sizeof (U) &&
std::is_signed<T>::value &&
std::is_unsigned<U>::value,
T
>
sign (const U u)
{
CHECK_LT (u, std::numeric_limits<U>::max () / 2);
return static_cast<T> (u);
}
///////////////////////////////////////////////////////////////////////////
// cast to a smaller type of the same signedness and realness and assert
// that both values are still equal.
//
// checks will be compiled out if NDEBUG is defined.
template <
typename NarrowT,
typename WideT,
typename = std::enable_if_t<
std::is_arithmetic_v<NarrowT> &&
std::is_arithmetic_v<WideT> &&
std::is_signed_v<NarrowT> == std::is_signed_v<WideT> &&
std::is_floating_point_v<NarrowT> == std::is_floating_point_v<WideT> &&
sizeof (NarrowT) <= sizeof (WideT),
void
>
>
constexpr NarrowT
narrow (const WideT &val)
{
static_assert (sizeof (NarrowT) <= sizeof (WideT));
#ifndef NDEBUG
auto narrow = static_cast<NarrowT> (val);
CHECK_EQ (narrow, val);
return narrow;
#else
return static_cast<NarrowT> (val);
#endif
}
///////////////////////////////////////////////////////////////////////////
// cast between types checking that equality holds with the result
//
// checks will be compiled out if NDEBUG is defined.
template <typename DstT, typename SrcT>
constexpr DstT
lossless (const SrcT &src)
{
#ifndef NDEBUG
auto dst = static_cast<DstT> (src);
if constexpr (std::is_floating_point_v<SrcT>) {
if (std::isnan (src)) {
// NaNs must remaing as NaN's. They're important.
CHECK (std::is_floating_point_v<DstT>);
CHECK (std::isnan (dst));
}
}
// Cast dst back to src to check round-trip conversion
// is lossless.
CHECK_EQ (static_cast<SrcT> (dst), src);
return dst;
#else
return static_cast<DstT> (src);
#endif
}
///////////////////////////////////////////////////////////////////////////
/// 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 <typename T, typename V>
T*
known (V *v)
{
CHECK (dynamic_cast<T*> (v));
return static_cast<T*> (v);
}
//-------------------------------------------------------------------------
template <typename T, typename V>
T&
known (V &v)
{
CHECK_NOTHROW (dynamic_cast<T> (v));
return static_cast<T> (v);
}
///////////////////////////////////////////////////////////////////////////
/// cast a pointer from one type to another, asserting that the required
/// alignment of the destination type has been satisfied.
template <
typename DstT,
typename SrcT,
typename = std::enable_if_t<std::is_pointer_v<DstT> && std::is_pointer_v<SrcT>>
>
DstT
alignment (SrcT src)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-align"
CHECK_MOD (reinterpret_cast<uintptr_t> (src), alignof (std::remove_pointer_t<DstT>));
return reinterpret_cast<DstT> (src);
#pragma GCC diagnostic pop
}
///////////////////////////////////////////////////////////////////////////
/// cast from SrcT to DstT and damn any consequences.
template <typename DstT, typename SrcT>
DstT ffs (SrcT src)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-align"
#if defined(COMPILER_GCC)
#pragma GCC diagnostic ignored "-Wcast-function-type"
#endif
return reinterpret_cast<DstT> (src);
#pragma GCC diagnostic pop
}
}