ACE中对于不同类型数据的最大值最小值管理ACE_Numeric_Limits

对于不同类型的变量,这里定义了其最大最小值来提供给ACE_Utils等使用

  1 template <typename T> struct ACE_Numeric_Limits;
  2 
  3 
  4 // ------------------------------------------
  5 // Special cases.
  6 template<>
  7 struct ACE_Export ACE_Numeric_Limits<char>
  8 {
  9   static char min (void) { return CHAR_MIN; }
 10   static char max (void) { return CHAR_MAX; }
 11 };
 12 
 13 // ------------------------------------------
 14 // Signed integers.
 15 
 16 template<>
 17 struct ACE_Export ACE_Numeric_Limits<signed char>
 18 {
 19   static signed char min (void) { return SCHAR_MIN; }
 20   static signed char max (void) { return SCHAR_MAX; }
 21 };
 22 
 23 template<>
 24 struct ACE_Export ACE_Numeric_Limits<signed short>
 25 {
 26   static signed short min (void) { return SHRT_MIN; }
 27   static signed short max (void) { return SHRT_MAX; }
 28 };
 29 
 30 template<>
 31 struct ACE_Export ACE_Numeric_Limits<signed int>
 32 {
 33   static signed int min (void) { return INT_MIN; }
 34   static signed int max (void) { return INT_MAX; }
 35 };
 36 
 37 template<>
 38 struct ACE_Export ACE_Numeric_Limits<signed long>
 39 {
 40   static signed long min (void) { return LONG_MIN; }
 41   static signed long max (void) { return LONG_MAX; }
 42 };
 43 
 44 template<>
 45 struct ACE_Export ACE_Numeric_Limits<signed long long>
 46 {
 47 #if defined (LLONG_MIN)
 48 #  define ACE_LLONG_MIN LLONG_MIN
 49 #elif defined (LONG_LONG_MIN)
 50 #  define ACE_LLONG_MIN LONG_LONG_MIN
 51 #elif defined (LONGLONG_MIN)
 52 #  define ACE_LLONG_MIN LONGLONG_MIN
 53 #else
 54 #  error Unable to determine minimum signed long long value.
 55 #endif  /* LLONG_MIN */
 56 
 57 #if defined (LLONG_MAX)
 58 #  define ACE_LLONG_MAX LLONG_MAX
 59 #elif defined (LONG_LONG_MAX)
 60 #  define ACE_LLONG_MAX LONG_LONG_MAX
 61 #elif defined (LONGLONG_MAX)
 62 #  define ACE_LLONG_MAX LONGLONG_MAX
 63 #else
 64 #  error Unable to determine maximum signed long long value.
 65 #endif  /* LLONG_MAX */
 66 
 67   static signed long long min (void) { return ACE_LLONG_MIN; }
 68   static signed long long max (void) { return ACE_LLONG_MAX; }
 69 };
 70 
 71 // ------------------------------------------
 72 // Unsigned integers
 73 template<>
 74 struct ACE_Export ACE_Numeric_Limits<unsigned char>
 75 {
 76   static unsigned char min (void) { return 0; }
 77   static unsigned char max (void) { return UCHAR_MAX; }
 78 };
 79 
 80 template<>
 81 struct ACE_Export ACE_Numeric_Limits<unsigned short>
 82 {
 83   static unsigned short min (void) { return 0; }
 84   static unsigned short max (void) { return USHRT_MAX; }
 85 };
 86 
 87 template<>
 88 struct ACE_Export ACE_Numeric_Limits<unsigned int>
 89 {
 90   static unsigned int min (void) { return 0; }
 91   static unsigned int max (void) { return UINT_MAX; }
 92 };
 93 
 94 template<>
 95 struct ACE_Export ACE_Numeric_Limits<unsigned long>
 96 {
 97   static unsigned long min (void) { return 0; }
 98   static unsigned long max (void) { return ULONG_MAX; }
 99 };
100 
101 template<>
102 struct ACE_Export ACE_Numeric_Limits<unsigned long long>
103 {
104   static unsigned long long min (void) { return 0; }
105   static unsigned long long max (void)
106   {
107 # if defined (ULLONG_MAX)
108     return ULLONG_MAX;
109 # elif defined (ULONGLONG_MAX)
110     return ULONGLONG_MAX;
111 # else
112 #  error Unable to determine maximum unsigned long long value.
113 # endif  /* ULLONG_MAX */
114   }
115 };
116 
117 // ------------------------------------------
118 // Floating point types
119 
120 template<>
121 struct ACE_Export ACE_Numeric_Limits<float>
122 {
123   static float min (void) { return FLT_MIN; }
124   static float max (void) { return FLT_MAX; }
125 };
126 
127 template<>
128 struct ACE_Export ACE_Numeric_Limits<double>
129 {
130   static double min (void) { return DBL_MIN; }
131   static double max (void) { return DBL_MAX; }
132 };
133 
134 template<>
135 struct ACE_Export ACE_Numeric_Limits<long double>
136 {
137   static long double min (void) { return LDBL_MIN; }
138   static long double max (void) { return LDBL_MAX; }
139 };
140 
141 #else
142 
143 // std::numeric_limits<> has all of the necessary specializations.
144 // Just wrap it.
145 
146 template <typename T>
147 struct ACE_Numeric_Limits
148 {
149   static T min (void) { return std::numeric_limits<T>::min (); }
150   static T max (void) { return std::numeric_limits<T>::max (); }
151 };
152 
153 # if (defined (ACE_WIN64) && defined (_MSC_VER) && _MSC_VER <= 1310) \
154     || defined (ACE_LACKS_NUMERIC_LIMITS_64_BIT_TYPES)
155 // The Microsoft Platform SDK does not provide std::numeric_limits<>
156 // specializations for 64 bit integers so we need to explicitly provide
157 // ACE_Numeric_Limits<> specializations to compensate for this
158 // deficiency.
159 //
160 // Unfortunately there is no way to tell if the platform SDK is being
161 // used so we specialize for the ACE_WIN64 + MSVC++ 7.1 case, which is
162 // the configuration that exhibits this problem.  It also happens to
163 // be a fairly isolated configuration since 64-bit support in MSVC++
164 // 7.1 was not very good to begin with.
165 template<>
166 struct ACE_Numeric_Limits<LONGLONG>
167 {
168   static LONGLONG min (void) { return _I64_MIN; }
169   static LONGLONG max (void) { return _I64_MAX; }
170 };
171 
172 template<>
173 struct ACE_Numeric_Limits<ULONGLONG>
174 {
175   static ULONGLONG min (void) { return 0; }
176   static ULONGLONG max (void) { return _UI64_MAX; }
177 };
178 # endif  /* ACE_WIN64 && _MSC_VER <= 1310 */

猜你喜欢

转载自www.cnblogs.com/zl1991/p/9342492.html
今日推荐