| 1 |
/* |
| 2 |
* This file is part of libcxxsupport. |
| 3 |
* |
| 4 |
* libcxxsupport is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* libcxxsupport is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with libcxxsupport; if not, write to the Free Software |
| 16 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 |
*/ |
| 18 |
|
| 19 |
/* |
| 20 |
* libcxxsupport is being developed at the Max-Planck-Institut fuer Astrophysik |
| 21 |
* and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt |
| 22 |
* (DLR). |
| 23 |
*/ |
| 24 |
|
| 25 |
/*! \file safe_cast.h |
| 26 |
* Numerical cast operator with additional checks that the value is preserved. |
| 27 |
* |
| 28 |
* Copyright (C) 2009 Max-Planck-Society |
| 29 |
* Author: Martin Reinecke |
| 30 |
*/ |
| 31 |
|
| 32 |
#ifndef PLANCK_SAFE_CAST_H |
| 33 |
#define PLANCK_SAFE_CAST_H |
| 34 |
|
| 35 |
#include <limits> |
| 36 |
#include "error_handling.h" |
| 37 |
|
| 38 |
template<typename T1, typename T2, bool s1, bool s2> struct safe_cast_helper__ |
| 39 |
{}; |
| 40 |
|
| 41 |
template<typename T1, typename T2> struct safe_cast_helper__ <T1,T2,true,true> |
| 42 |
{ |
| 43 |
static T1 cast (const T2 &arg) |
| 44 |
{ |
| 45 |
T1 res = T1(arg); |
| 46 |
planck_assert(T2(res)==arg, "safe_cast: value changed during cast"); |
| 47 |
return res; |
| 48 |
} |
| 49 |
}; |
| 50 |
|
| 51 |
template<typename T1, typename T2> struct safe_cast_helper__ <T1,T2,false,false> |
| 52 |
{ |
| 53 |
static T1 cast (const T2 &arg) |
| 54 |
{ |
| 55 |
T1 res = T1(arg); |
| 56 |
planck_assert(T2(res)==arg, "safe_cast: value changed during cast"); |
| 57 |
return res; |
| 58 |
} |
| 59 |
}; |
| 60 |
|
| 61 |
template<typename T1, typename T2> struct safe_cast_helper__ <T1,T2,true,false> |
| 62 |
{ |
| 63 |
static T1 cast (const T2 &arg) |
| 64 |
{ |
| 65 |
T1 res = T1(arg); |
| 66 |
planck_assert((res>=0) && (T2(res)==arg), |
| 67 |
"safe_cast: value changed during cast"); |
| 68 |
return res; |
| 69 |
} |
| 70 |
}; |
| 71 |
|
| 72 |
template<typename T1, typename T2> struct safe_cast_helper__ <T1,T2,false,true> |
| 73 |
{ |
| 74 |
static T1 cast (const T2 &arg) |
| 75 |
{ |
| 76 |
T1 res = T1(arg); |
| 77 |
planck_assert((arg>=0) && (T2(res)==arg), |
| 78 |
"safe_cast: value changed during cast"); |
| 79 |
return res; |
| 80 |
} |
| 81 |
}; |
| 82 |
|
| 83 |
/*! Tries to cast \a arg from its type to a variable of type \c T1. |
| 84 |
If this conversion leads to a change of the actual value (e.g. due to |
| 85 |
overflow or truncation), an exception is thrown. */ |
| 86 |
template<typename T1, typename T2> inline T1 safe_cast(const T2 &arg) |
| 87 |
{ |
| 88 |
return safe_cast_helper__<T1,T2,std::numeric_limits<T1>::is_signed, |
| 89 |
std::numeric_limits<T2>::is_signed>::cast(arg); |
| 90 |
} |
| 91 |
|
| 92 |
#endif |