Numeric.h++
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef __ccxx_Numeric_hxx
00024 #define __ccxx_Numeric_hxx
00025
00026 #include <limits>
00027
00028 #ifdef min
00029 #undef min
00030 #endif
00031
00032 #ifdef max
00033 #undef max
00034 #endif
00035
00036 #include <commonc++/OutOfBoundsException.h++>
00037
00038 namespace ccxx {
00039
00048 template<typename S, typename T>
00049 inline T numeric_cast(S value) throw(OutOfBoundsException)
00050 {
00051 if((value < std::numeric_limits<T>::min())
00052 || (value > std::numeric_limits<T>::max()))
00053 throw OutOfBoundsException();
00054 else
00055 return(static_cast<T>(value));
00056 }
00057
00064 template<typename S, typename T>
00065 inline T numeric_clipping_cast(S value) throw()
00066 {
00067 if(value < std::numeric_limits<T>::min())
00068 return(std::numeric_limits<T>::min());
00069 else if(value > std::numeric_limits<T>::max())
00070 return(std::numeric_limits<T>::max());
00071 else
00072 return(static_cast<T>(value));
00073 }
00074
00083 template<typename T>
00084 inline T numeric_clip(T value, T min, T max) throw()
00085 {
00086 if(value < min)
00087 return(min);
00088 else if(value > max)
00089 return(max);
00090 else return(value);
00091 }
00092
00093 };
00094
00095 #endif // __ccxx_Numeric_hxx
00096
00097