2012-05-25 15:19:07 +10:00
|
|
|
/*
|
|
|
|
* This file is part of libgim.
|
|
|
|
*
|
|
|
|
* libgim is free software: you can redistribute it and/or modify it under the
|
|
|
|
* terms of the GNU General Public License as published by the Free Software
|
|
|
|
* Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
* version.
|
|
|
|
*
|
|
|
|
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Copyright 2011 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __UTIL_TYPES_CASTS_HPP
|
|
|
|
#define __UTIL_TYPES_CASTS_HPP
|
|
|
|
|
|
|
|
#include "../debug.hpp"
|
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
|
2012-06-08 16:46:03 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-05-25 15:19:07 +10:00
|
|
|
namespace detail {
|
|
|
|
template <typename T, typename V>
|
|
|
|
T
|
|
|
|
_sign_cast (const typename std::enable_if<sizeof(T) == sizeof(V) &&
|
|
|
|
std::is_unsigned<T>::value &&
|
|
|
|
std::is_signed<V>::value, V>::type v)
|
|
|
|
{
|
2015-01-28 14:49:34 +11:00
|
|
|
CHECK_GE (v, 0);
|
2012-05-25 15:19:07 +10:00
|
|
|
return static_cast<T> (v);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T, typename V>
|
|
|
|
T
|
2012-06-13 15:46:44 +10:00
|
|
|
_sign_cast (const typename std::enable_if<sizeof(T) == sizeof(V) &&
|
2012-05-25 15:19:07 +10:00
|
|
|
std::is_signed<T>::value &&
|
|
|
|
std::is_unsigned<V>::value, V>::type v)
|
|
|
|
{
|
2015-01-28 14:49:34 +11:00
|
|
|
CHECK_LT (v, std::numeric_limits<V>::max () / 2);
|
2012-05-25 15:19:07 +10:00
|
|
|
return static_cast<T> (v);
|
|
|
|
}
|
2012-06-13 15:46:44 +10:00
|
|
|
|
|
|
|
|
|
|
|
template <typename T, typename V>
|
|
|
|
T
|
|
|
|
_sign_cast (const typename std::enable_if<std::is_same<T, V>::value, V>::type v)
|
|
|
|
{ return v; }
|
2012-05-25 15:19:07 +10:00
|
|
|
}
|
|
|
|
|
2015-02-17 16:20:46 +11:00
|
|
|
|
2012-05-25 15:19:07 +10: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.
|
|
|
|
template <typename T, typename V>
|
|
|
|
T
|
|
|
|
sign_cast (const V v)
|
|
|
|
{ return detail::_sign_cast<T,V>(v); }
|
|
|
|
|
|
|
|
|
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.
|
2012-06-14 18:31:58 +10:00
|
|
|
|
2015-02-17 16:20:46 +11:00
|
|
|
template <typename T, typename U>
|
2012-05-25 15:19:07 +10:00
|
|
|
T
|
2015-02-17 16:20:46 +11:00
|
|
|
trunc_cast (U u)
|
|
|
|
{
|
|
|
|
auto t = static_cast<T> (u);
|
|
|
|
if (u == u)
|
|
|
|
CHECK_EQ (t, u);
|
|
|
|
else
|
|
|
|
CHECK_NEQ (t, t);
|
|
|
|
|
|
|
|
return t;
|
2012-06-08 16:46:03 +10:00
|
|
|
}
|
|
|
|
|
2012-06-20 16:48:40 +10: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*
|
|
|
|
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&
|
|
|
|
known_cast (V &v) {
|
|
|
|
CHECK_NOTHROW (dynamic_cast<T> (v));
|
|
|
|
return static_cast<T> (v);
|
|
|
|
}
|
|
|
|
|
2012-05-25 15:19:07 +10:00
|
|
|
#endif
|