NCBI C++ ToolKit
ncbi_limits.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef NCBI_LIMITS__HPP
2 #define NCBI_LIMITS__HPP
3 
4 /* $Id: ncbi_limits.hpp 89002 2020-02-11 15:01:11Z ucko $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Author: Denis Vakatov
30  *
31  * File Description:
32  *
33  * Temporary replacement for "numeric_limits<>".
34  * Extremely incomplete implementation,
35  *
36  * only min() and max() methods for:
37  *
38  * numeric_limits<char>
39  * numeric_limits<signed char>
40  * numeric_limits<unsigned char>
41  *
42  * numeric_limits<signed short>
43  * numeric_limits<unsigned short>
44  * numeric_limits<signed int>
45  * numeric_limits<unsigned int>
46  * numeric_limits<signed long>
47  * numeric_limits<unsigned long>
48  *
49  * numeric_limits<float>
50  * numeric_limits<double>
51  *
52  * (platform-specific)
53  * numeric_limits<signed long long>
54  * numeric_limits<unsigned long long>
55  * numeric_limits<signed __int64>
56  * numeric_limits<unsigned __int64>
57  *
58  */
59 
60 #include <corelib/ncbi_limits.h>
61 #include <corelib/ncbistl.hpp>
62 
63 
64 /** @addtogroup Portability
65  *
66  * @{
67  */
68 
69 
70 // Always get rid of the old non-conformant min/max macros
71 #ifdef min
72 # undef min
73 #endif
74 #ifdef max
75 # undef max
76 #endif
77 
78 #if defined(HAVE_LIMITS) && ( !defined(NCBI_COMPILER_WORKSHOP) || NCBI_COMPILER_VERSION >= 550)
79 // Ideally, we would use std::numeric_limits<> whenever available.
80 // However, certain compiler versions leave out support for extensions such
81 // as long long, so we still have to use our implementation with them.
82 # include <limits>
83 #else
84 
86 
87 ///
88 /// Pre-declaration of the "numeric_limits<>" template
89 /// Forcibly overrides (using preprocessor) the original "numeric_limits<>"!
90 ///
91 
92 # define numeric_limits ncbi_numeric_limits
93 template <class T> class numeric_limits;
94 
95 
96 ///
97 /// Auxiliary macro to implement (a limited edition of) the
98 /// "numeric_limits<>" template
99 ///
100 
101 # define NCBI_NUMERIC_LIMITS(type, alias) \
102  template <> \
103  class numeric_limits<type> \
104  { \
105  public: \
106  static inline type min() THROWS_NONE { return kMin_##alias; } \
107  static inline type max() THROWS_NONE { return kMax_##alias; } \
108  }
109 
110 # define NCBI_NUMERIC_LIMITS_UNSIGNED(type, alias) \
111  template <> \
112  class numeric_limits<type> \
113  { \
114  public: \
115  static inline type min() THROWS_NONE { return 0; } \
116  static inline type max() THROWS_NONE { return kMax_##alias; } \
117  }
118 
119 
120 //
121 // Implement (a limited edition of) the "numeric_limits<>" template
122 // for various built-in types
123 //
124 
126 NCBI_NUMERIC_LIMITS (signed char, SChar);
128 
129 #if defined(HAVE_WCHAR_H) && defined(WCHAR_MIN)
130 NCBI_NUMERIC_LIMITS (wchar_t, WChar);
131 #endif
132 
133 NCBI_NUMERIC_LIMITS (signed short, Short);
134 NCBI_NUMERIC_LIMITS_UNSIGNED (unsigned short, UShort);
135 
136 NCBI_NUMERIC_LIMITS (signed int, Int);
137 NCBI_NUMERIC_LIMITS_UNSIGNED (unsigned int, UInt);
138 
139 NCBI_NUMERIC_LIMITS (signed long, Long);
140 NCBI_NUMERIC_LIMITS_UNSIGNED (unsigned long, ULong);
141 
142 NCBI_NUMERIC_LIMITS (float, Float);
143 NCBI_NUMERIC_LIMITS (double, Double);
144 
145 # if (SIZEOF_LONG_LONG > 0)
146 NCBI_NUMERIC_LIMITS (signed long long, LongLong);
147 NCBI_NUMERIC_LIMITS_UNSIGNED (unsigned long long, ULongLong);
148 # endif
149 
150 # if defined(NCBI_INT8_IS_INT64)
151 NCBI_NUMERIC_LIMITS (signed __int64, Int64);
153 # endif
154 
155 
157 
158 #endif // !HAVE_LIMITS || NCBI_COMPILER_WORKSHOP
159 
160 
162 
163 // The formal is_signed parameter helps avoid warnings under MSVC.
164 template <typename T, bool is_signed = numeric_limits<T>::is_signed>
166 {
167  typedef numeric_limits<T> TStdLim;
168  static T max(void) { return TStdLim::max(); }
169 
170  /// For non-integral types, yield a huge negative value rather than a
171  /// tiny positive one.
172  static T min(void)
173  {
174 #if defined(NCBI_HAVE_CXX11) && !defined(NCBI_COMPILER_ICC)
175  // Be conservative with ICC, which could wind up using a libstdc++
176  // lacking lowest() despite running in C++ '11 mode itself.
177  return TStdLim::lowest();
178 #else
179  return TStdLim::is_integer ? TStdLim::min() : T(-TStdLim::max());
180 #endif
181  }
182 };
183 
184 template <typename T>
186 {
187  typedef numeric_limits<T> TStdLim;
188  static T max(void) { return TStdLim::max(); }
189  static T min(void) { return 0; }
190 };
191 
192 struct SAutoMax
193 {
194  template <typename T>
195  operator T(void) const { return SAutoMinMaxLimits<T>::max(); }
196 };
197 
198 /// Generic stand-in for type-specific kMax_* constants from ncbi_limits.h,
199 /// useful in any context with exactly one preferred type.
200 static const SAutoMax kMax_Auto = {};
201 
202 struct SAutoMin
203 {
204  template <typename T>
205  operator T(void) const { return SAutoMinMaxLimits<T>::min(); }
206 };
207 
208 /// Generic stand-in for type-specific kMin_* constants from ncbi_limits.h,
209 /// useful in any context with exactly one preferred type. NB: For
210 /// non-integral types, yields a huge negative value rather than a tiny
211 /// positive one.
212 static const SAutoMin kMin_Auto = {};
213 
214 
215 # define NCBI_FORBID_AUTOMINMAX_OPERATION(op, T1, T2) \
216 template <typename T> \
217 inline bool operator op(const T1&, const T2&) { \
218  typename T::T1##T2##OperationNotSupported tmp; \
219  return false; \
220 }
221 
222 # define NCBI_FORBID_AUTOMINMAX_OPERATIONS(op) \
223 NCBI_FORBID_AUTOMINMAX_OPERATION(op, SAutoMax, T) \
224 NCBI_FORBID_AUTOMINMAX_OPERATION(op, SAutoMin, T) \
225 NCBI_FORBID_AUTOMINMAX_OPERATION(op, T, SAutoMax) \
226 NCBI_FORBID_AUTOMINMAX_OPERATION(op, T, SAutoMin)
227 
234 
235 
236 /// Generic template to get STD limits by a variable.
237 /// Typical use:
238 /// <pre>
239 /// int a = 10;
240 /// get_limits(a).max();
241 /// </pre>
242 /// @note
243 /// Causes a compile-time failure if used
244 /// instead of the specialized implementations.
245 template<typename T>
247 {
248  typename T::TypeIsNotSupported tmp;
249  return numeric_limits<T>();
250 }
251 
252 /// Macro to declare specialized get_limits
253 # define NCBI_GET_NUMERIC_LIMITS(type) \
254  EMPTY_TEMPLATE \
255  inline numeric_limits<type> get_limits(const type&) \
256  { return numeric_limits<type>(); }
257 
261 
263 NCBI_GET_NUMERIC_LIMITS(unsigned short)
264 
267 
270 
273 
274 # if (SIZEOF_LONG_LONG > 0)
275 NCBI_GET_NUMERIC_LIMITS(signed long long)
276 NCBI_GET_NUMERIC_LIMITS(unsigned long long)
277 # endif
278 
279 # if defined(NCBI_INT8_IS_INT64)
282 # endif
283 
284 
286 
287 /* @} */
288 
289 #endif /* NCBI_LIMITS__HPP */
#define false
Definition: bool.h:36
unsigned char UChar
Definition: bzip2.c:163
#define T(s)
Definition: common.h:230
#define NCBI_NUMERIC_LIMITS(type, alias)
Auxiliary macro to implement (a limited edition of) the "numeric_limits<>" template.
static const SAutoMin kMin_Auto
Generic stand-in for type-specific kMin_* constants from ncbi_limits.h, useful in any context with ex...
ncbi_numeric_limits< T > TStdLim
#define numeric_limits
Pre-declaration of the "numeric_limits<>" template Forcibly overrides (using preprocessor) the origin...
Definition: ncbi_limits.hpp:92
#define NCBI_NUMERIC_LIMITS_UNSIGNED(type, alias)
#define NCBI_GET_NUMERIC_LIMITS(type)
Macro to declare specialized get_limits.
char Char
Alias for char.
Definition: ncbitype.h:93
static const SAutoMax kMax_Auto
Generic stand-in for type-specific kMax_* constants from ncbi_limits.h, useful in any context with ex...
static T max(void)
ncbi_numeric_limits< T > TStdLim
#define NCBI_FORBID_AUTOMINMAX_OPERATIONS(op)
static T min(void)
For non-integral types, yield a huge negative value rather than a tiny positive one.
ncbi_numeric_limits< T > get_limits(const T &)
Generic template to get STD limits by a variable.
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
The NCBI C++/STL use hints.
T max(T x_, T y_)
T min(T x_, T y_)
static char tmp[2048]
Definition: utf8.c:42
#define __int64
Definition: sse2neon.h:208
Definition: bzip2.c:233
#define const
Definition: zconf.h:230
Modified on Sat Dec 02 09:23:45 2023 by modify_doxy.py rev. 669887