safe_cast.h

Go to the documentation of this file.
00001 /*
00002  *  This file is part of libcxxsupport.
00003  *
00004  *  libcxxsupport is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  libcxxsupport is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with libcxxsupport; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00017  */
00018 
00019 /*
00020  *  libcxxsupport is being developed at the Max-Planck-Institut fuer Astrophysik
00021  *  and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
00022  *  (DLR).
00023  */
00024 
00025 /*! \file safe_cast.h
00026  *  Numerical cast operator with additional checks that the value is preserved.
00027  *
00028  *  Copyright (C) 2009 Max-Planck-Society
00029  *  Author: Martin Reinecke
00030  */
00031 
00032 #ifndef PLANCK_SAFE_CAST_H
00033 #define PLANCK_SAFE_CAST_H
00034 
00035 #include <limits>
00036 #include "error_handling.h"
00037 
00038 template<typename T1, typename T2, bool s1, bool s2> struct safe_cast_helper__
00039   {};
00040 
00041 template<typename T1, typename T2> struct safe_cast_helper__ <T1,T2,true,true>
00042   {
00043   static T1 cast (const T2 &arg)
00044     {
00045     T1 res = T1(arg);
00046     planck_assert(T2(res)==arg, "safe_cast: value changed during cast");
00047     return res;
00048     }
00049   };
00050 
00051 template<typename T1, typename T2> struct safe_cast_helper__ <T1,T2,false,false>
00052   {
00053   static T1 cast (const T2 &arg)
00054     {
00055     T1 res = T1(arg);
00056     planck_assert(T2(res)==arg, "safe_cast: value changed during cast");
00057     return res;
00058     }
00059   };
00060 
00061 template<typename T1, typename T2> struct safe_cast_helper__ <T1,T2,true,false>
00062   {
00063   static T1 cast (const T2 &arg)
00064     {
00065     T1 res = T1(arg);
00066     planck_assert((res>=0) && (T2(res)==arg),
00067       "safe_cast: value changed during cast");
00068     return res;
00069     }
00070   };
00071 
00072 template<typename T1, typename T2> struct safe_cast_helper__ <T1,T2,false,true>
00073   {
00074   static T1 cast (const T2 &arg)
00075     {
00076     T1 res = T1(arg);
00077     planck_assert((arg>=0) && (T2(res)==arg),
00078       "safe_cast: value changed during cast");
00079     return res;
00080     }
00081   };
00082 
00083 /*! Tries to cast \a arg from its type to a variable of type \c T1.
00084     If this conversion leads to a change of the actual value (e.g. due to
00085     overflow or truncation), an exception is thrown. */
00086 template<typename T1, typename T2> inline T1 safe_cast(const T2 &arg)
00087   {
00088   return safe_cast_helper__<T1,T2,std::numeric_limits<T1>::is_signed,
00089     std::numeric_limits<T2>::is_signed>::cast(arg);
00090   }
00091 
00092 #endif

Generated on Thu Oct 8 14:48:51 2015 for LevelS C++ support library