1 // This file is part of the ustl library, an STL implementation.
3 // Copyright (C) 2005 by Mike Sharov <msharov@users.sourceforge.net>
4 // This file is free software, distributed under the MIT License.
9 #ifndef ULIMITS_H_1C2192EA3821E0811BBAF86B0F048364
10 #define ULIMITS_H_1C2192EA3821E0811BBAF86B0F048364
16 /// \class numeric_limits ulimits.h ustl.h
17 /// \brief Defines numeric limits for a type.
20 struct numeric_limits
{
21 /// Returns the minimum value for type T.
22 static inline T
min (void) { return (T(0)); }
23 /// Returns the minimum value for type T.
24 static inline T
max (void) { return (T(0)); }
25 static const bool is_signed
= false; ///< True if the type is signed.
26 static const bool is_integer
= false; ///< True if stores an exact value.
27 static const bool is_integral
= false; ///< True if fixed size and cast-copyable.
30 #ifndef DOXYGEN_SHOULD_SKIP_THIS
33 struct numeric_limits
<T
*> {
34 static inline T
* min (void) { return (NULL
); }
35 static inline T
* max (void) { return (reinterpret_cast<T
*>(UINTPTR_MAX
)); }
36 static const bool is_signed
= false;
37 static const bool is_integer
= true;
38 static const bool is_integral
= true;
41 #define _NUMERIC_LIMITS(type, minVal, maxVal, bSigned, bInteger, bIntegral) \
43 struct numeric_limits<type> { \
44 static inline type min (void) { return (minVal); } \
45 static inline type max (void) { return (maxVal); } \
46 static const bool is_signed = bSigned; \
47 static const bool is_integer = bInteger; \
48 static const bool is_integral = bIntegral; \
51 //--------------------------------------------------------------------------------------
52 // type min max signed integer integral
53 //--------------------------------------------------------------------------------------
54 _NUMERIC_LIMITS (bool, false, true, false, true, true);
55 _NUMERIC_LIMITS (char, CHAR_MIN
, CHAR_MAX
, true, true, true);
56 _NUMERIC_LIMITS (int, INT_MIN
, INT_MAX
, true, true, true);
57 _NUMERIC_LIMITS (short, SHRT_MIN
, SHRT_MAX
, true, true, true);
58 _NUMERIC_LIMITS (long, LONG_MIN
, LONG_MAX
, true, true, true);
59 #if HAVE_THREE_CHAR_TYPES
60 _NUMERIC_LIMITS (signed char, SCHAR_MIN
, SCHAR_MAX
, true, true, true);
62 _NUMERIC_LIMITS (unsigned char, 0, UCHAR_MAX
, false, true, true);
63 _NUMERIC_LIMITS (unsigned int, 0, UINT_MAX
, false, true, true);
64 _NUMERIC_LIMITS (unsigned short,0, USHRT_MAX
, false, true, true);
65 _NUMERIC_LIMITS (unsigned long, 0, ULONG_MAX
, false, true, true);
66 _NUMERIC_LIMITS (wchar_t, 0, WCHAR_MAX
, false, true, true);
67 _NUMERIC_LIMITS (float, FLT_MIN
, FLT_MAX
, true, false, true);
68 _NUMERIC_LIMITS (double, DBL_MIN
, DBL_MAX
, true, false, true);
69 _NUMERIC_LIMITS (long double, LDBL_MIN
, LDBL_MAX
, true, false, true);
71 _NUMERIC_LIMITS (long long, LLONG_MIN
, LLONG_MAX
, true, true, true);
72 _NUMERIC_LIMITS (unsigned long long, 0, ULLONG_MAX
, false, true, true);
74 //--------------------------------------------------------------------------------------
76 #endif // DOXYGEN_SHOULD_SKIP_THIS
78 /// Macro for defining numeric_limits specializations
79 #define NUMERIC_LIMITS(type, minVal, maxVal, bSigned, bInteger, bIntegral) \
80 namespace ustl { _NUMERIC_LIMITS (type, minVal, maxVal, bSigned, bInteger, bIntegral); }
82 /// \brief Returns the recommended stream alignment for type \p T. Override with ALIGNOF.
83 /// Because this is occasionally called with a null value, do not access the argument!
85 inline size_t alignof (const T
&)
87 if (numeric_limits
<T
>::is_integral
)
88 return (__alignof__(T
));
92 #define ALIGNOF(type,grain) \
94 template <> inline size_t alignof (const type&) { return (grain); } }