2012-05-25 15:19:07 +10:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* 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/.
|
2012-05-25 15:19:07 +10:00
|
|
|
*
|
2018-08-20 15:08:15 +10:00
|
|
|
* Copyright 2011-2018 Danny Robson <danny@nerdcruft.net>
|
2012-05-25 15:19:07 +10:00
|
|
|
*/
|
|
|
|
|
2018-08-20 15:08:15 +10:00
|
|
|
#pragma once
|
2012-05-25 15:19:07 +10:00
|
|
|
|
2020-02-27 11:49:41 +11:00
|
|
|
|
|
|
|
#include "concepts.hpp"
|
2019-05-17 12:26:08 +10:00
|
|
|
#include "debug/assert.hpp"
|
|
|
|
#include "debug/validate.hpp"
|
2018-08-20 15:08:15 +10:00
|
|
|
#include "platform.hpp"
|
2012-05-25 15:19:07 +10:00
|
|
|
|
2020-02-17 14:28:43 +11:00
|
|
|
#include <string_view>
|
2012-05-25 15:19:07 +10:00
|
|
|
#include <type_traits>
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
namespace cruft::cast {
|
2018-01-16 15:11:15 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
/// 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);
|
2016-10-25 17:48:55 +11:00
|
|
|
}
|
|
|
|
|
2012-06-08 16:46:03 +10:00
|
|
|
|
2018-01-16 15:11:15 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2018-12-26 15:10:21 +11:00
|
|
|
/// Cast to a smaller type of the same signedness and realness and assert
|
|
|
|
/// that both values are still equal.
|
|
|
|
///
|
|
|
|
/// Any runtime checks will be compiled out if NDEBUG is defined.
|
|
|
|
///
|
|
|
|
/// Identity casts are allowed so as to simplify the use of this routine
|
|
|
|
/// in template code.
|
2018-01-30 16:25:28 +11:00
|
|
|
template <
|
2020-02-27 11:49:41 +11:00
|
|
|
concepts::arithmetic NarrowT,
|
|
|
|
concepts::arithmetic WideT
|
2018-01-30 16:25:28 +11:00
|
|
|
>
|
2020-02-27 11:49:41 +11:00
|
|
|
requires
|
|
|
|
(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))
|
2018-01-16 15:11:15 +11:00
|
|
|
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;
|
2016-10-25 17:48:55 +11:00
|
|
|
#else
|
2018-01-16 15:11:15 +11:00
|
|
|
return static_cast<NarrowT> (val);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-10-25 17:48:55 +11:00
|
|
|
|
2018-01-16 15:11:15 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2018-12-26 15:10:21 +11:00
|
|
|
/// Cast between types checking that exact equality holds if the result is
|
|
|
|
/// casted back to the original type.
|
|
|
|
///
|
|
|
|
/// Runtime checks will be compiled out if NDEBUG is defined.
|
2018-01-16 15:11:15 +11:00
|
|
|
template <typename DstT, typename SrcT>
|
|
|
|
constexpr DstT
|
|
|
|
lossless (const SrcT &src)
|
|
|
|
{
|
|
|
|
#ifndef NDEBUG
|
2019-12-03 09:34:02 +11:00
|
|
|
// GCC insists that the initial static_cast to DstT is a floating
|
|
|
|
// point comparison if we pass in a bool and a float.
|
|
|
|
//
|
|
|
|
// The only way around this is to ignore the warning locally (we use
|
|
|
|
// almost_equal inside CHECK_EQ anyway, so it should not be a concern).
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wfloat-equal"
|
|
|
|
|
2018-01-16 15:11:15 +11:00
|
|
|
auto dst = static_cast<DstT> (src);
|
|
|
|
|
|
|
|
if constexpr (std::is_floating_point_v<SrcT>) {
|
2018-05-10 12:45:20 +10:00
|
|
|
if (std::isnan (src)) {
|
2018-01-16 15:11:15 +11:00
|
|
|
// 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);
|
2019-12-03 09:34:02 +11:00
|
|
|
|
|
|
|
#pragma GCC diagnostic pop
|
2018-01-16 15:11:15 +11:00
|
|
|
return dst;
|
|
|
|
#else
|
2018-12-06 15:58:23 +11:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wshorten-64-to-32"
|
|
|
|
if constexpr (std::is_enum_v<DstT>) {
|
|
|
|
auto const val = static_cast<std::underlying_type_t<DstT>> (src);
|
|
|
|
return static_cast<DstT> (val);
|
|
|
|
} else {
|
|
|
|
return static_cast<DstT> (src);
|
|
|
|
}
|
|
|
|
#pragma GCC diagnostic pop
|
2016-10-25 17:48:55 +11:00
|
|
|
#endif
|
2018-01-16 15:11:15 +11:00
|
|
|
}
|
2016-10-25 17:48:55 +11:00
|
|
|
|
2012-06-20 16:48:40 +10:00
|
|
|
|
2018-01-16 15:11:15 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2018-12-26 15:10:21 +11:00
|
|
|
/// Assert if the value is not a pointer to a subclass of T, else return
|
2018-01-16 15:11:15 +11:00
|
|
|
/// the converted value. Note: this is only a debug-time check and is
|
|
|
|
/// compiled out in optimised builds.
|
2018-10-22 18:22:02 +11:00
|
|
|
template <
|
|
|
|
typename T,
|
|
|
|
typename V
|
|
|
|
>
|
|
|
|
std::enable_if_t<std::is_pointer_v<T>,T>
|
|
|
|
known (V *const v)
|
2018-01-16 15:11:15 +11:00
|
|
|
{
|
2018-10-22 18:22:02 +11:00
|
|
|
CHECK (dynamic_cast<T> (v));
|
|
|
|
return static_cast<T> (v);
|
2018-01-16 15:11:15 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
2018-10-22 18:22:02 +11:00
|
|
|
template <
|
|
|
|
typename T,
|
|
|
|
typename V
|
|
|
|
>
|
|
|
|
std::enable_if_t<std::is_reference_v<T>, T>
|
2018-01-16 15:11:15 +11:00
|
|
|
known (V &v)
|
|
|
|
{
|
|
|
|
CHECK_NOTHROW (dynamic_cast<T> (v));
|
2018-10-22 18:22:02 +11:00
|
|
|
return reinterpret_cast<T> (v);
|
2018-01-16 15:11:15 +11:00
|
|
|
}
|
2018-05-09 17:47:47 +10:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2018-12-26 15:10:21 +11:00
|
|
|
/// Cast a pointer from one type to another, asserting that the required
|
2018-05-09 17:47:47 +10:00
|
|
|
/// alignment of the destination type has been satisfied.
|
2018-12-26 15:10:21 +11:00
|
|
|
///
|
|
|
|
/// Runtime checks will be compiled out if NDEBUG is defined.
|
2018-05-10 12:52:01 +10:00
|
|
|
template <
|
|
|
|
typename DstT,
|
|
|
|
typename SrcT,
|
|
|
|
typename = std::enable_if_t<std::is_pointer_v<DstT> && std::is_pointer_v<SrcT>>
|
|
|
|
>
|
|
|
|
DstT
|
|
|
|
alignment (SrcT src)
|
2018-05-09 17:47:47 +10:00
|
|
|
{
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wcast-align"
|
2019-05-05 09:29:07 +10:00
|
|
|
#ifdef COMPILER_GCC
|
|
|
|
#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
|
|
|
|
#endif
|
2018-05-10 14:54:03 +10:00
|
|
|
CHECK_MOD (reinterpret_cast<uintptr_t> (src), alignof (std::remove_pointer_t<DstT>));
|
2018-05-10 12:52:01 +10:00
|
|
|
return reinterpret_cast<DstT> (src);
|
2018-05-09 17:47:47 +10:00
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
}
|
2018-05-10 13:52:49 +10:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2018-12-26 15:10:21 +11:00
|
|
|
/// Cast from SrcT to DstT and damn any consequences; just make it compile.
|
2018-05-10 13:52:49 +10:00
|
|
|
template <typename DstT, typename SrcT>
|
|
|
|
DstT ffs (SrcT src)
|
|
|
|
{
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wcast-align"
|
2019-01-04 17:11:53 +11:00
|
|
|
#pragma GCC diagnostic ignored "-Wold-style-cast"
|
2018-08-20 15:08:15 +10:00
|
|
|
#if defined(COMPILER_GCC)
|
|
|
|
#pragma GCC diagnostic ignored "-Wcast-function-type"
|
|
|
|
#endif
|
2019-01-04 17:11:53 +11:00
|
|
|
return (DstT)src;
|
2018-05-10 13:52:49 +10:00
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
}
|
2018-12-03 15:29:04 +11:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
/// Cast from SrcT to DstT, performing sanity checks on the src and dst
|
|
|
|
/// values before returning the result.
|
|
|
|
template <typename DstT, typename SrcT>
|
|
|
|
DstT sanity (SrcT src)
|
|
|
|
{
|
|
|
|
cruft::debug::sanity (src);
|
|
|
|
DstT dst = static_cast<DstT> (src);
|
|
|
|
cruft::debug::sanity (dst);
|
|
|
|
return dst;
|
|
|
|
}
|
2018-05-10 13:52:49 +10:00
|
|
|
}
|