1 // -*- Mode: C++; tab-width: 2; -*- 2 // vi: set ts=2: 3 // 4 5 #ifndef BALL_COMMON_LIMITS_H 6 #define BALL_COMMON_LIMITS_H 7 8 #ifndef BALL_CONFIG_CONFIG_H 9 # include <BALL/CONFIG/config.h> 10 #endif 11 12 #ifndef BALL_COMMON_GLOBAL_H 13 # include <BALL/COMMON/global.h> 14 #endif 15 16 #ifdef BALL_HAS_NUMERIC_LIMITS 17 # include <limits> 18 #else 19 # ifdef BALL_HAS_LIMITS_H 20 # include <limits.h> 21 # endif 22 # ifdef BALL_HAS_VALUES_H 23 # include <limits.h> 24 # endif 25 #endif 26 27 # ifdef BALL_HAS_FLOAT_H 28 # include <float.h> 29 # endif 30 31 namespace BALL 32 { 33 34 35 /** Numeric limits class. 36 This template class describes the minimum and maximum values for 37 the differnet data types. 38 It a substitute for the <tt>numeric_limits</tt> class described in the 39 ISO/ANSI standard (chapter 18.2.1.1), as most compiler still do not support 40 this feature (or more precisely: the header file \<limits\> is not implemented). 41 If \<limits\> exists, Limits is just a wrapper around the <tt>numeric_limits</tt> class, 42 otherwise it returns the value from the standard C header \<limits.h\>. \par 43 44 \ingroup Common 45 */ 46 template <typename T> 47 class BALL_EXPORT Limits 48 { 49 public: 50 51 #ifdef BALL_HAS_NUMERIC_LIMITS 52 53 54 /** Return the minimum value. 55 @return T the minimum value for this datatype 56 */ min()57 static T min() 58 { 59 return std::numeric_limits<T>::min(); 60 } 61 62 /** Return the maximum value. 63 @return T the maximum value for this datatype 64 */ max()65 static T max() 66 { 67 return std::numeric_limits<T>::max(); 68 } 69 #else 70 static T min() 71 { 72 return (T)0; 73 } 74 static T max() 75 { 76 return (T)0; 77 } 78 #endif 79 }; 80 81 #ifndef BALL_HAS_NUMERIC_LIMITS 82 83 template <> 84 class BALL_EXPORT Limits<float> 85 { 86 public: 87 min()88 static float min() 89 { 90 return FLT_MIN; 91 } 92 max()93 static float max() 94 { 95 return FLT_MAX; 96 } 97 }; 98 99 template <> 100 class BALL_EXPORT Limits<double> 101 { 102 public: 103 min()104 static double min() 105 { 106 return DBL_MIN; 107 } 108 max()109 static double max() 110 { 111 return DBL_MAX; 112 } 113 }; 114 115 template <> 116 class BALL_EXPORT Limits<bool> 117 { 118 public: 119 min()120 static bool min() 121 { 122 return false; 123 } 124 max()125 static bool max() 126 { 127 return true; 128 } 129 }; 130 131 template <> 132 class BALL_EXPORT Limits<char> 133 { 134 public: 135 min()136 static char min() 137 { 138 return CHAR_MIN; 139 } 140 max()141 static char max() 142 { 143 return CHAR_MAX; 144 } 145 }; 146 147 template <> 148 class BALL_EXPORT Limits<signed char> 149 { 150 public: 151 min()152 static signed char min() 153 { 154 return SCHAR_MIN; 155 } 156 max()157 static signed char max() 158 { 159 return SCHAR_MAX; 160 } 161 }; 162 163 template <> 164 class BALL_EXPORT Limits<unsigned char> 165 { 166 public: 167 min()168 static unsigned char min() 169 { 170 return 0; 171 } 172 max()173 static unsigned char max() 174 { 175 return UCHAR_MAX; 176 } 177 }; 178 179 template <> 180 class BALL_EXPORT Limits<short> 181 { 182 public: 183 min()184 static short min() 185 { 186 return SHRT_MIN; 187 } 188 max()189 static short max() 190 { 191 return SHRT_MAX; 192 } 193 }; 194 195 template <> 196 class BALL_EXPORT Limits<unsigned short> 197 { 198 public: 199 min()200 static unsigned short min() 201 { 202 return 0; 203 } 204 max()205 static unsigned short max() 206 { 207 return USHRT_MAX; 208 } 209 }; 210 211 template <> 212 class BALL_EXPORT Limits<int> 213 { 214 public: 215 min()216 static int min() 217 { 218 return INT_MIN; 219 } 220 max()221 static int max() 222 { 223 return INT_MAX; 224 } 225 }; 226 227 template <> 228 class BALL_EXPORT Limits<unsigned int> 229 { 230 public: 231 min()232 static unsigned int min() 233 { 234 return 0; 235 } 236 max()237 static unsigned int max() 238 { 239 return UINT_MAX; 240 } 241 }; 242 243 template <> 244 class BALL_EXPORT Limits<long> 245 { 246 public: 247 min()248 static long min() 249 { 250 return LONG_MIN; 251 } 252 max()253 static long max() 254 { 255 return LONG_MAX; 256 } 257 }; 258 259 template <> 260 class BALL_EXPORT Limits<unsigned long> 261 { 262 public: 263 min()264 static unsigned long min() 265 { 266 return 0; 267 } 268 max()269 static unsigned long max() 270 { 271 return ULONG_MAX; 272 } 273 }; 274 275 #endif // BALL_HAS_NUMERIC_LIMITS 276 277 } // namespace BALL 278 279 #endif // BALL_COMMON_LIMITS_H 280