1 // This file is part of the ustl library, an STL implementation. 2 // 3 // Copyright (C) 2005 by Mike Sharov <msharov (at) users.sourceforge.net> 4 // This file is free software, distributed under the MIT License. 5 // 6 // ulimits.h 7 // 8 9 #ifndef ULIMITS_H_1C2192EA3821E0811BBAF86B0F048364 10 #define ULIMITS_H_1C2192EA3821E0811BBAF86B0F048364 11 12 #include "utypes.h" 13 #include <stdint.h> 14 15 namespace ustl { 16 17 // Android 18 #ifndef UINTPTR_MAX 19 #define UINTPTR_MAX UINT32_MAX 20 #endif 21 22 #ifndef UINT32_MAX 23 #define UINT32_MAX (0xffffffff) 24 #endif 25 26 /// \class numeric_limits ulimits.h ustl.h 27 /// \brief Defines numeric limits for a type. 28 /// 29 template <typename T> 30 struct numeric_limits { 31 /// Returns the minimum value for type T. 32 static inline T min (void) { return (T(0)); } 33 /// Returns the minimum value for type T. 34 static inline T max (void) { return (T(0)); } 35 static const bool is_signed = false; ///< True if the type is signed. 36 static const bool is_integer = false; ///< True if stores an exact value. 37 static const bool is_integral = false; ///< True if fixed size and cast-copyable. 38 }; 39 40 #ifndef DOXYGEN_SHOULD_SKIP_THIS 41 42 template <typename T> 43 struct numeric_limits<T*> { 44 static inline T* min (void) { return (NULL); } 45 static inline T* max (void) { return (UINTPTR_MAX); } 46 static const bool is_signed = false; 47 static const bool is_integer = true; 48 static const bool is_integral = true; 49 }; 50 51 #define _NUMERIC_LIMITS(type, minVal, maxVal, bSigned, bInteger, bIntegral) \ 52 template <> \ 53 struct numeric_limits<type> { \ 54 static inline type min (void) { return (minVal); } \ 55 static inline type max (void) { return (maxVal); } \ 56 static const bool is_signed = bSigned; \ 57 static const bool is_integer = bInteger; \ 58 static const bool is_integral = bIntegral; \ 59 } 60 61 //-------------------------------------------------------------------------------------- 62 // type min max signed integer integral 63 //-------------------------------------------------------------------------------------- 64 _NUMERIC_LIMITS (bool, false, true, false, true, true); 65 _NUMERIC_LIMITS (char, SCHAR_MIN, SCHAR_MAX, true, true, true); 66 _NUMERIC_LIMITS (int, INT_MIN, INT_MAX, true, true, true); 67 _NUMERIC_LIMITS (short, SHRT_MIN, SHRT_MAX, true, true, true); 68 _NUMERIC_LIMITS (long, LONG_MIN, LONG_MAX, true, true, true); 69 #if HAVE_THREE_CHAR_TYPES 70 _NUMERIC_LIMITS (signed char, SCHAR_MIN, SCHAR_MAX, true, true, true); 71 #endif 72 _NUMERIC_LIMITS (unsigned char, 0, UCHAR_MAX, false, true, true); 73 _NUMERIC_LIMITS (unsigned int, 0, UINT_MAX, false, true, true); 74 _NUMERIC_LIMITS (unsigned short,0, USHRT_MAX, false, true, true); 75 _NUMERIC_LIMITS (unsigned long, 0, ULONG_MAX, false, true, true); 76 _NUMERIC_LIMITS (wchar_t, 0, WCHAR_MAX, false, true, true); 77 _NUMERIC_LIMITS (float, FLT_MIN, FLT_MAX, true, false, true); 78 _NUMERIC_LIMITS (double, DBL_MIN, DBL_MAX, true, false, true); 79 _NUMERIC_LIMITS (long double, LDBL_MIN, LDBL_MAX, true, false, true); 80 #ifdef HAVE_LONG_LONG 81 _NUMERIC_LIMITS (long long, LLONG_MIN, LLONG_MAX, true, true, true); 82 _NUMERIC_LIMITS (unsigned long long, 0, ULLONG_MAX, false, true, true); 83 #endif 84 //-------------------------------------------------------------------------------------- 85 86 #endif // DOXYGEN_SHOULD_SKIP_THIS 87 88 /// Macro for defining numeric_limits specializations 89 #define NUMERIC_LIMITS(type, minVal, maxVal, bSigned, bInteger, bIntegral) \ 90 namespace ustl { _NUMERIC_LIMITS (type, minVal, maxVal, bSigned, bInteger, bIntegral); } 91 92 /// Returns the recommended stream alignment for type \p T. Override with ALIGNOF. 93 template <typename T> 94 inline size_t alignof (const T&) 95 { 96 if (numeric_limits<T>::is_integral) 97 return (__alignof__(T)); 98 return (4); 99 } 100 101 #define ALIGNOF(type,grain) \ 102 namespace ustl { \ 103 template <> inline size_t alignof (const type&) { return (grain); } } 104 105 } // namespace ustl 106 107 #endif 108 109