1 |
786 |
skrzyp |
// This file is part of the uSTL library, an STL implementation.
|
2 |
|
|
//
|
3 |
|
|
// Copyright (c) 2005-2009 by Mike Sharov <msharov@users.sourceforge.net>
|
4 |
|
|
// This file is free software, distributed under the MIT License.
|
5 |
|
|
|
6 |
|
|
#ifndef ULIMITS_H_1C2192EA3821E0811BBAF86B0F048364
|
7 |
|
|
#define ULIMITS_H_1C2192EA3821E0811BBAF86B0F048364
|
8 |
|
|
|
9 |
|
|
#include "utypes.h"
|
10 |
|
|
|
11 |
|
|
namespace ustl {
|
12 |
|
|
|
13 |
|
|
#define __limits_digits(T) (sizeof(T)*8)
|
14 |
|
|
#define __limits_digits10(T) (sizeof(T)*8*643/2136+1)
|
15 |
|
|
|
16 |
|
|
/// \class numeric_limits ulimits.h ustl.h
|
17 |
|
|
/// \brief Defines numeric limits for a type.
|
18 |
|
|
///
|
19 |
|
|
template <typename T>
|
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.
|
28 |
|
|
static const unsigned digits = __limits_digits(T); ///< Number of bits in T
|
29 |
|
|
static const unsigned digits10 = __limits_digits10(T); ///< Maximum number of decimal digits in printed version of T
|
30 |
|
|
};
|
31 |
|
|
|
32 |
|
|
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
33 |
|
|
|
34 |
|
|
template <typename T>
|
35 |
|
|
struct numeric_limits<T*> {
|
36 |
|
|
static inline T* min (void) { return (NULL); }
|
37 |
|
|
static inline T* max (void) { return (reinterpret_cast<T*>(UINTPTR_MAX)); }
|
38 |
|
|
static const bool is_signed = false;
|
39 |
|
|
static const bool is_integer = true;
|
40 |
|
|
static const bool is_integral = true;
|
41 |
|
|
static const unsigned digits = __limits_digits(T*);
|
42 |
|
|
static const unsigned digits10 = __limits_digits10(T*);
|
43 |
|
|
};
|
44 |
|
|
|
45 |
|
|
#define _NUMERIC_LIMITS(type, minVal, maxVal, bSigned, bInteger, bIntegral) \
|
46 |
|
|
template <> \
|
47 |
|
|
struct numeric_limits<type> { \
|
48 |
|
|
static inline type min (void) { return (minVal); } \
|
49 |
|
|
static inline type max (void) { return (maxVal); } \
|
50 |
|
|
static const bool is_signed = bSigned; \
|
51 |
|
|
static const bool is_integer = bInteger; \
|
52 |
|
|
static const bool is_integral = bIntegral; \
|
53 |
|
|
static const unsigned digits = __limits_digits(type); \
|
54 |
|
|
static const unsigned digits10 = __limits_digits10(type); \
|
55 |
|
|
}
|
56 |
|
|
|
57 |
|
|
//--------------------------------------------------------------------------------------
|
58 |
|
|
// type min max signed integer integral
|
59 |
|
|
//--------------------------------------------------------------------------------------
|
60 |
|
|
_NUMERIC_LIMITS (bool, false, true, false, true, true);
|
61 |
|
|
_NUMERIC_LIMITS (char, CHAR_MIN, CHAR_MAX, true, true, true);
|
62 |
|
|
_NUMERIC_LIMITS (int, INT_MIN, INT_MAX, true, true, true);
|
63 |
|
|
_NUMERIC_LIMITS (short, SHRT_MIN, SHRT_MAX, true, true, true);
|
64 |
|
|
_NUMERIC_LIMITS (long, LONG_MIN, LONG_MAX, true, true, true);
|
65 |
|
|
#if HAVE_THREE_CHAR_TYPES
|
66 |
|
|
_NUMERIC_LIMITS (signed char, SCHAR_MIN, SCHAR_MAX, true, true, true);
|
67 |
|
|
#endif
|
68 |
|
|
_NUMERIC_LIMITS (unsigned char, 0, UCHAR_MAX, false, true, true);
|
69 |
|
|
_NUMERIC_LIMITS (unsigned int, 0, UINT_MAX, false, true, true);
|
70 |
|
|
_NUMERIC_LIMITS (unsigned short,0, USHRT_MAX, false, true, true);
|
71 |
|
|
_NUMERIC_LIMITS (unsigned long, 0, ULONG_MAX, false, true, true);
|
72 |
|
|
_NUMERIC_LIMITS (wchar_t, 0, WCHAR_MAX, false, true, true);
|
73 |
|
|
_NUMERIC_LIMITS (float, FLT_MIN, FLT_MAX, true, false, true);
|
74 |
|
|
_NUMERIC_LIMITS (double, DBL_MIN, DBL_MAX, true, false, true);
|
75 |
|
|
_NUMERIC_LIMITS (long double, LDBL_MIN, LDBL_MAX, true, false, true);
|
76 |
|
|
#if HAVE_LONG_LONG
|
77 |
|
|
_NUMERIC_LIMITS (long long, LLONG_MIN, LLONG_MAX, true, true, true);
|
78 |
|
|
_NUMERIC_LIMITS (unsigned long long, 0, ULLONG_MAX, false, true, true);
|
79 |
|
|
#endif
|
80 |
|
|
//--------------------------------------------------------------------------------------
|
81 |
|
|
|
82 |
|
|
#endif // DOXYGEN_SHOULD_SKIP_THIS
|
83 |
|
|
|
84 |
|
|
/// Macro for defining numeric_limits specializations
|
85 |
|
|
#define NUMERIC_LIMITS(type, minVal, maxVal, bSigned, bInteger, bIntegral) \
|
86 |
|
|
namespace ustl { _NUMERIC_LIMITS (type, minVal, maxVal, bSigned, bInteger, bIntegral); }
|
87 |
|
|
|
88 |
|
|
/// \brief Returns the recommended stream alignment for type \p T. Override with ALIGNOF.
|
89 |
|
|
/// Because this is occasionally called with a null value, do not access the argument!
|
90 |
|
|
template <typename T>
|
91 |
|
|
inline size_t alignof (const T&)
|
92 |
|
|
{
|
93 |
|
|
if (numeric_limits<T>::is_integral)
|
94 |
|
|
return (__alignof__(T));
|
95 |
|
|
return (4);
|
96 |
|
|
}
|
97 |
|
|
|
98 |
|
|
#define ALIGNOF(type,grain) \
|
99 |
|
|
namespace ustl { \
|
100 |
|
|
template <> inline size_t alignof (const type&) { return (grain); } }
|
101 |
|
|
|
102 |
|
|
} // namespace ustl
|
103 |
|
|
|
104 |
|
|
#endif
|