blob: 4fc4cab4ba25dc2a51f6221be9394e6ae02125ac [file] [log] [blame]
Jeff Thompsonef2d5a42013-08-22 19:09:24 -07001// boost cast.hpp header file ----------------------------------------------//
2
3// (C) Copyright Kevlin Henney and Dave Abrahams 1999.
4// Distributed under the Boost
5// Software License, Version 1.0. (See accompanying file
6// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7
8// See http://www.boost.org/libs/conversion for Documentation.
9
10// Revision History
11// 23 JUN 05 Code extracted from /boost/cast.hpp into this new header.
12// Keeps this legacy version of numeric_cast<> for old compilers
13// wich can't compile the new version in /boost/numeric/conversion/cast.hpp
14// (Fernando Cacciola)
Jeff Thompson3d613fd2013-10-15 15:39:04 -070015// 02 Apr 01 Removed NDNBOOST_NO_LIMITS workarounds and included
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070016// <ndnboost/limits.hpp> instead (the workaround did not
Jeff Thompson3d613fd2013-10-15 15:39:04 -070017// actually compile when NDNBOOST_NO_LIMITS was defined in
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070018// any case, so we loose nothing). (John Maddock)
19// 21 Jan 01 Undid a bug I introduced yesterday. numeric_cast<> never
20// worked with stock GCC; trying to get it to do that broke
21// vc-stlport.
Jeff Thompson3d613fd2013-10-15 15:39:04 -070022// 20 Jan 01 Moved NDNBOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS to config.hpp.
23// Removed unused NDNBOOST_EXPLICIT_TARGET macro. Moved
Jeff Thompson9939dcd2013-10-15 15:12:24 -070024// ndnboost::detail::type to ndnboost/type.hpp. Made it compile with
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070025// stock gcc again (Dave Abrahams)
26// 29 Nov 00 Remove nested namespace cast, cleanup spacing before Formal
27// Review (Beman Dawes)
28// 19 Oct 00 Fix numeric_cast for floating-point types (Dave Abrahams)
29// 15 Jul 00 Suppress numeric_cast warnings for GCC, Borland and MSVC
30// (Dave Abrahams)
31// 30 Jun 00 More MSVC6 wordarounds. See comments below. (Dave Abrahams)
32// 28 Jun 00 Removed implicit_cast<>. See comment below. (Beman Dawes)
33// 27 Jun 00 More MSVC6 workarounds
34// 15 Jun 00 Add workarounds for MSVC6
35// 2 Feb 00 Remove bad_numeric_cast ";" syntax error (Doncho Angelov)
36// 26 Jan 00 Add missing throw() to bad_numeric_cast::what(0 (Adam Levar)
37// 29 Dec 99 Change using declarations so usages in other namespaces work
38// correctly (Dave Abrahams)
39// 23 Sep 99 Change polymorphic_downcast assert to also detect M.I. errors
40// as suggested Darin Adler and improved by Valentin Bonnard.
41// 2 Sep 99 Remove controversial asserts, simplify, rename.
42// 30 Aug 99 Move to cast.hpp, replace value_cast with numeric_cast,
43// place in nested namespace.
44// 3 Aug 99 Initial version
45
Jeff Thompson3d613fd2013-10-15 15:39:04 -070046#ifndef NDNBOOST_OLD_NUMERIC_CAST_HPP
47#define NDNBOOST_OLD_NUMERIC_CAST_HPP
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070048
49# include <ndnboost/config.hpp>
50# include <cassert>
51# include <typeinfo>
52# include <ndnboost/type.hpp>
53# include <ndnboost/limits.hpp>
54# include <ndnboost/numeric/conversion/converter_policies.hpp>
55
56// It has been demonstrated numerous times that MSVC 6.0 fails silently at link
57// time if you use a template function which has template parameters that don't
58// appear in the function's argument list.
59//
60// TODO: Add this to config.hpp?
Jeff Thompson9939dcd2013-10-15 15:12:24 -070061// FLC: This macro is repeated in ndnboost/cast.hpp but only locally (is undefined at the bottom)
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070062// so is OK to reproduce it here.
Jeff Thompson3d613fd2013-10-15 15:39:04 -070063# if defined(NDNBOOST_MSVC) && NDNBOOST_MSVC < 1300
64# define NDNBOOST_EXPLICIT_DEFAULT_TARGET , ::ndnboost::type<Target>* = 0
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070065# else
Jeff Thompson3d613fd2013-10-15 15:39:04 -070066# define NDNBOOST_EXPLICIT_DEFAULT_TARGET
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070067# endif
68
69namespace ndnboost
70{
71 using numeric::bad_numeric_cast;
72
73// LEGACY numeric_cast [only for some old broken compilers] --------------------------------------//
74
75// Contributed by Kevlin Henney
76
77// numeric_cast ------------------------------------------------------------//
78
Jeff Thompson3d613fd2013-10-15 15:39:04 -070079#if !defined(NDNBOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) || defined(NDNBOOST_SGI_CPP_LIMITS)
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070080
81 namespace detail
82 {
83 template <class T>
84 struct signed_numeric_limits : std::numeric_limits<T>
85 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -070086 static inline T min NDNBOOST_PREVENT_MACRO_SUBSTITUTION ()
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070087 {
88 return (std::numeric_limits<T>::min)() >= 0
89 // unary minus causes integral promotion, thus the static_cast<>
90 ? static_cast<T>(-(std::numeric_limits<T>::max)())
91 : (std::numeric_limits<T>::min)();
92 };
93 };
94
95 // Move to namespace ndnboost in utility.hpp?
96 template <class T, bool specialized>
97 struct fixed_numeric_limits_base
98 : public if_true< std::numeric_limits<T>::is_signed >
Jeff Thompson3d613fd2013-10-15 15:39:04 -070099 ::NDNBOOST_NESTED_TEMPLATE then< signed_numeric_limits<T>,
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700100 std::numeric_limits<T>
101 >::type
102 {};
103
104 template <class T>
105 struct fixed_numeric_limits
106 : fixed_numeric_limits_base<T,(std::numeric_limits<T>::is_specialized)>
107 {};
108
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700109# ifdef NDNBOOST_HAS_LONG_LONG
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700110 // cover implementations which supply no specialization for long
111 // long / unsigned long long. Not intended to be full
112 // numeric_limits replacements, but good enough for numeric_cast<>
113 template <>
114 struct fixed_numeric_limits_base< ::ndnboost::long_long_type, false>
115 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700116 NDNBOOST_STATIC_CONSTANT(bool, is_specialized = true);
117 NDNBOOST_STATIC_CONSTANT(bool, is_signed = true);
118 static ::ndnboost::long_long_type max NDNBOOST_PREVENT_MACRO_SUBSTITUTION ()
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700119 {
120# ifdef LONGLONG_MAX
121 return LONGLONG_MAX;
122# else
123 return 9223372036854775807LL; // hope this is portable
124# endif
125 }
126
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700127 static ::ndnboost::long_long_type min NDNBOOST_PREVENT_MACRO_SUBSTITUTION ()
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700128 {
129# ifdef LONGLONG_MIN
130 return LONGLONG_MIN;
131# else
132 return -( 9223372036854775807LL )-1; // hope this is portable
133# endif
134 }
135 };
136
137 template <>
138 struct fixed_numeric_limits_base< ::ndnboost::ulong_long_type, false>
139 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700140 NDNBOOST_STATIC_CONSTANT(bool, is_specialized = true);
141 NDNBOOST_STATIC_CONSTANT(bool, is_signed = false);
142 static ::ndnboost::ulong_long_type max NDNBOOST_PREVENT_MACRO_SUBSTITUTION ()
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700143 {
144# ifdef ULONGLONG_MAX
145 return ULONGLONG_MAX;
146# else
147 return 0xffffffffffffffffULL; // hope this is portable
148# endif
149 }
150
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700151 static ::ndnboost::ulong_long_type min NDNBOOST_PREVENT_MACRO_SUBSTITUTION () { return 0; }
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700152 };
153# endif
154 } // namespace detail
155
156// less_than_type_min -
157 // x_is_signed should be numeric_limits<X>::is_signed
158 // y_is_signed should be numeric_limits<Y>::is_signed
159 // y_min should be numeric_limits<Y>::min()
160 //
161 // check(x, y_min) returns true iff x < y_min without invoking comparisons
162 // between signed and unsigned values.
163 //
164 // "poor man's partial specialization" is in use here.
165 template <bool x_is_signed, bool y_is_signed>
166 struct less_than_type_min
167 {
168 template <class X, class Y>
169 static bool check(X x, Y y_min)
170 { return x < y_min; }
171 };
172
173 template <>
174 struct less_than_type_min<false, true>
175 {
176 template <class X, class Y>
177 static bool check(X, Y)
178 { return false; }
179 };
180
181 template <>
182 struct less_than_type_min<true, false>
183 {
184 template <class X, class Y>
185 static bool check(X x, Y)
186 { return x < 0; }
187 };
188
189 // greater_than_type_max -
190 // same_sign should be:
191 // numeric_limits<X>::is_signed == numeric_limits<Y>::is_signed
192 // y_max should be numeric_limits<Y>::max()
193 //
194 // check(x, y_max) returns true iff x > y_max without invoking comparisons
195 // between signed and unsigned values.
196 //
197 // "poor man's partial specialization" is in use here.
198 template <bool same_sign, bool x_is_signed>
199 struct greater_than_type_max;
200
201 template<>
202 struct greater_than_type_max<true, true>
203 {
204 template <class X, class Y>
205 static inline bool check(X x, Y y_max)
206 { return x > y_max; }
207 };
208
209 template <>
210 struct greater_than_type_max<false, true>
211 {
212 // What does the standard say about this? I think it's right, and it
213 // will work with every compiler I know of.
214 template <class X, class Y>
215 static inline bool check(X x, Y)
216 { return x >= 0 && static_cast<X>(static_cast<Y>(x)) != x; }
217
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700218# if defined(NDNBOOST_MSVC) && NDNBOOST_MSVC < 1300
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700219 // MSVC6 can't static_cast unsigned __int64 -> floating types
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700220# define NDNBOOST_UINT64_CAST(src_type) \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700221 static inline bool check(src_type x, unsigned __int64) \
222 { \
223 if (x < 0) return false; \
224 unsigned __int64 y = static_cast<unsigned __int64>(x); \
225 bool odd = y & 0x1; \
226 __int64 div2 = static_cast<__int64>(y >> 1); \
227 return ((static_cast<src_type>(div2) * 2.0) + odd) != x; \
228 }
229
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700230 NDNBOOST_UINT64_CAST(long double);
231 NDNBOOST_UINT64_CAST(double);
232 NDNBOOST_UINT64_CAST(float);
233# undef NDNBOOST_UINT64_CAST
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700234# endif
235 };
236
237 template<>
238 struct greater_than_type_max<true, false>
239 {
240 template <class X, class Y>
241 static inline bool check(X x, Y y_max)
242 { return x > y_max; }
243 };
244
245 template <>
246 struct greater_than_type_max<false, false>
247 {
248 // What does the standard say about this? I think it's right, and it
249 // will work with every compiler I know of.
250 template <class X, class Y>
251 static inline bool check(X x, Y)
252 { return static_cast<X>(static_cast<Y>(x)) != x; }
253 };
254
255#else // use #pragma hacks if available
256
257 namespace detail
258 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700259# if NDNBOOST_MSVC
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700260# pragma warning(push)
261# pragma warning(disable : 4018)
262# pragma warning(disable : 4146)
263#elif defined(__BORLANDC__)
264# pragma option push -w-8041
265# endif
266
267 // Move to namespace ndnboost in utility.hpp?
268 template <class T>
269 struct fixed_numeric_limits : public std::numeric_limits<T>
270 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700271 static inline T min NDNBOOST_PREVENT_MACRO_SUBSTITUTION ()
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700272 {
273 return std::numeric_limits<T>::is_signed && (std::numeric_limits<T>::min)() >= 0
274 ? T(-(std::numeric_limits<T>::max)()) : (std::numeric_limits<T>::min)();
275 }
276 };
277
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700278# if NDNBOOST_MSVC
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700279# pragma warning(pop)
280#elif defined(__BORLANDC__)
281# pragma option pop
282# endif
283 } // namespace detail
284
285#endif
286
287 template<typename Target, typename Source>
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700288 inline Target numeric_cast(Source arg NDNBOOST_EXPLICIT_DEFAULT_TARGET)
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700289 {
290 // typedefs abbreviating respective trait classes
291 typedef detail::fixed_numeric_limits<Source> arg_traits;
292 typedef detail::fixed_numeric_limits<Target> result_traits;
293
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700294#if defined(NDNBOOST_STRICT_CONFIG) \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700295 || (!defined(__HP_aCC) || __HP_aCC > 33900) \
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700296 && (!defined(NDNBOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) \
297 || defined(NDNBOOST_SGI_CPP_LIMITS))
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700298 // typedefs that act as compile time assertions
299 // (to be replaced by boost compile time assertions
300 // as and when they become available and are stable)
301 typedef bool argument_must_be_numeric[arg_traits::is_specialized];
302 typedef bool result_must_be_numeric[result_traits::is_specialized];
303
304 const bool arg_is_signed = arg_traits::is_signed;
305 const bool result_is_signed = result_traits::is_signed;
306 const bool same_sign = arg_is_signed == result_is_signed;
307
308 if (less_than_type_min<arg_is_signed, result_is_signed>::check(arg, (result_traits::min)())
309 || greater_than_type_max<same_sign, arg_is_signed>::check(arg, (result_traits::max)())
310 )
311
312#else // We need to use #pragma hacks if available
313
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700314# if NDNBOOST_MSVC
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700315# pragma warning(push)
316# pragma warning(disable : 4018)
317#elif defined(__BORLANDC__)
318#pragma option push -w-8012
319# endif
320 if ((arg < 0 && !result_traits::is_signed) // loss of negative range
321 || (arg_traits::is_signed && arg < (result_traits::min)()) // underflow
322 || arg > (result_traits::max)()) // overflow
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700323# if NDNBOOST_MSVC
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700324# pragma warning(pop)
325#elif defined(__BORLANDC__)
326#pragma option pop
327# endif
328#endif
329 {
330 throw bad_numeric_cast();
331 }
332 return static_cast<Target>(arg);
333 } // numeric_cast
334
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700335# undef NDNBOOST_EXPLICIT_DEFAULT_TARGET
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700336
337} // namespace ndnboost
338
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700339#endif // NDNBOOST_OLD_NUMERIC_CAST_HPP