Numeric.h++

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002    commonc++ - A C++ Common Class Library
00003    Copyright (C) 2005-2012  Mark A Lindner
00004 
00005    This file is part of commonc++.
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011 
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Library General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public
00018    License along with this library; if not, write to the Free
00019    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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 }; // namespace ccxx
00094 
00095 #endif // __ccxx_Numeric_hxx
00096 
00097 /* end of header file */
Generated on Sat Nov 26 16:49:07 2011 for libcommonc++ by  doxygen 1.6.3