2012-05-25 15:19:07 +10:00
|
|
|
/*
|
2015-04-13 18:05:28 +10:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2012-05-25 15:19:07 +10:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2012-05-25 15:19:07 +10:00
|
|
|
*
|
|
|
|
* Copyright 2011 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
2015-11-17 16:19:27 +11:00
|
|
|
#ifndef __UTIL_CAST_HPP
|
|
|
|
#define __UTIL_CAST_HPP
|
2012-05-25 15:19:07 +10:00
|
|
|
|
2017-11-22 16:49:37 +11:00
|
|
|
#include "debug.hpp"
|
2012-05-25 15:19:07 +10:00
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
|
2018-01-16 15:11:15 +11:00
|
|
|
namespace util::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);
|
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-01-30 16:25:28 +11:00
|
|
|
// cast to a smaller type of the same signedness and realness and assert
|
|
|
|
// that both values are still equal.
|
2018-01-16 15:11:15 +11:00
|
|
|
//
|
|
|
|
// checks will be compiled out if NDEBUG is defined.
|
2018-01-30 16:25:28 +11:00
|
|
|
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
|
|
|
|
>
|
|
|
|
>
|
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
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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 (util::is_nan (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;
|
2016-10-25 17:48:55 +11:00
|
|
|
|
2018-01-16 15:11:15 +11:00
|
|
|
#else
|
|
|
|
return static_cast<DstT> (src);
|
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
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
/// 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);
|
|
|
|
}
|
|
|
|
};
|
2012-06-20 16:48:40 +10:00
|
|
|
|
2012-05-25 15:19:07 +10:00
|
|
|
#endif
|