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
|
|
|
|
2015-11-17 16:19:27 +11:00
|
|
|
#include "./debug.hpp"
|
2012-05-25 15:19:07 +10:00
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
|
2015-11-17 16:37:51 +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,
|
2012-05-25 15:19:07 +10:00
|
|
|
T
|
2015-11-17 16:37:51 +11:00
|
|
|
>
|
|
|
|
sign_cast (const U u)
|
|
|
|
{
|
|
|
|
CHECK_GE (u, 0);
|
|
|
|
return static_cast<T> (u);
|
|
|
|
}
|
2012-06-13 15:46:44 +10:00
|
|
|
|
2015-11-17 16:37:51 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <
|
|
|
|
typename T,
|
|
|
|
typename U
|
|
|
|
>
|
|
|
|
std::enable_if_t<
|
|
|
|
sizeof(T) == sizeof (U) &&
|
|
|
|
std::is_signed<T>::value &&
|
|
|
|
std::is_unsigned<U>::value,
|
2012-06-13 15:46:44 +10:00
|
|
|
T
|
2015-11-17 16:37:51 +11:00
|
|
|
>
|
|
|
|
sign_cast (const U u)
|
|
|
|
{
|
|
|
|
CHECK_LT (u, std::numeric_limits<U>::max () / 2);
|
|
|
|
return static_cast<T> (u);
|
2012-05-25 15:19:07 +10:00
|
|
|
}
|
|
|
|
|
2015-02-17 16:20:46 +11:00
|
|
|
|
|
|
|
///----------------------------------------------------------------------------
|
2016-10-25 17:48:55 +11:00
|
|
|
#if !defined(NDEBUG)
|
2015-02-17 16:20:46 +11:00
|
|
|
/// assert if the value cannot be cast loslessly from U to T, else return the
|
|
|
|
/// converted value.Note: this is only a debug-time check and is compiled out
|
|
|
|
/// in optimised builds.
|
2016-06-29 17:53:44 +10:00
|
|
|
///
|
|
|
|
/// relies on equality operators being present and behaving sanely. attempts
|
|
|
|
/// to preserve NaN values.
|
|
|
|
///
|
|
|
|
/// allows the identity cast (if only to make cross-platform porting easier)
|
2016-10-25 17:48:55 +11:00
|
|
|
template <typename DstT, typename SrcT>
|
|
|
|
constexpr
|
2015-11-17 16:38:34 +11:00
|
|
|
std::enable_if_t<
|
2016-10-25 17:48:55 +11:00
|
|
|
std::is_arithmetic<SrcT>::value &&
|
|
|
|
std::is_arithmetic<DstT>::value,
|
|
|
|
DstT
|
2015-11-17 16:38:34 +11:00
|
|
|
>
|
2016-10-25 17:48:55 +11:00
|
|
|
trunc_cast (const SrcT src)
|
2015-02-17 16:20:46 +11:00
|
|
|
{
|
2016-10-25 17:48:55 +11:00
|
|
|
auto dst = static_cast<DstT> (src);
|
|
|
|
|
2017-01-23 21:48:12 +11:00
|
|
|
if (!util::is_nan (src)) {
|
2016-10-25 17:48:55 +11:00
|
|
|
CHECK_EQ (static_cast<SrcT> (dst), src);
|
|
|
|
} else {
|
|
|
|
CHECK_NEQ (dst, dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dst;
|
2012-06-08 16:46:03 +10:00
|
|
|
}
|
|
|
|
|
2016-10-25 17:48:55 +11:00
|
|
|
#else
|
|
|
|
|
|
|
|
// in release mode we don't give a fuck what happens. cast away all safety.
|
|
|
|
template <typename DstT, typename SrcT>
|
|
|
|
DstT
|
|
|
|
trunc_cast (const SrcT src)
|
|
|
|
{
|
|
|
|
return static_cast<DstT> (src);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2012-06-20 16:48:40 +10:00
|
|
|
|
2015-11-17 16:38:59 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-02-17 16:20:46 +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.
|
2012-06-20 16:48:40 +10:00
|
|
|
template <typename T, typename V>
|
|
|
|
T*
|
2015-11-17 16:38:59 +11:00
|
|
|
known_cast (V *v)
|
|
|
|
{
|
2015-01-28 14:49:34 +11:00
|
|
|
CHECK (dynamic_cast<T*> (v));
|
2012-08-15 17:44:29 +10:00
|
|
|
return static_cast<T*> (v);
|
2012-06-20 16:48:40 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-17 16:20:46 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-06-20 16:48:40 +10:00
|
|
|
template <typename T, typename V>
|
|
|
|
T&
|
2015-11-17 16:38:59 +11:00
|
|
|
known_cast (V &v)
|
|
|
|
{
|
2012-06-20 16:48:40 +10:00
|
|
|
CHECK_NOTHROW (dynamic_cast<T> (v));
|
|
|
|
return static_cast<T> (v);
|
|
|
|
}
|
|
|
|
|
2012-05-25 15:19:07 +10:00
|
|
|
#endif
|