williamr@4
|
1 |
// boost cast.hpp header file ----------------------------------------------//
|
williamr@2
|
2 |
|
williamr@4
|
3 |
// (C) Copyright Kevlin Henney and Dave Abrahams 1999.
|
williamr@4
|
4 |
// Distributed under the Boost
|
williamr@4
|
5 |
// Software License, Version 1.0. (See accompanying file
|
williamr@4
|
6 |
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
williamr@4
|
7 |
|
williamr@4
|
8 |
// See http://www.boost.org/libs/conversion for Documentation.
|
williamr@4
|
9 |
|
williamr@4
|
10 |
// Revision History
|
williamr@4
|
11 |
// 23 JUn 05 numeric_cast removed and redirected to the new verion (Fernando Cacciola)
|
williamr@4
|
12 |
// 02 Apr 01 Removed BOOST_NO_LIMITS workarounds and included
|
williamr@4
|
13 |
// <boost/limits.hpp> instead (the workaround did not
|
williamr@4
|
14 |
// actually compile when BOOST_NO_LIMITS was defined in
|
williamr@4
|
15 |
// any case, so we loose nothing). (John Maddock)
|
williamr@4
|
16 |
// 21 Jan 01 Undid a bug I introduced yesterday. numeric_cast<> never
|
williamr@4
|
17 |
// worked with stock GCC; trying to get it to do that broke
|
williamr@4
|
18 |
// vc-stlport.
|
williamr@4
|
19 |
// 20 Jan 01 Moved BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS to config.hpp.
|
williamr@4
|
20 |
// Removed unused BOOST_EXPLICIT_TARGET macro. Moved
|
williamr@4
|
21 |
// boost::detail::type to boost/type.hpp. Made it compile with
|
williamr@4
|
22 |
// stock gcc again (Dave Abrahams)
|
williamr@4
|
23 |
// 29 Nov 00 Remove nested namespace cast, cleanup spacing before Formal
|
williamr@4
|
24 |
// Review (Beman Dawes)
|
williamr@4
|
25 |
// 19 Oct 00 Fix numeric_cast for floating-point types (Dave Abrahams)
|
williamr@4
|
26 |
// 15 Jul 00 Suppress numeric_cast warnings for GCC, Borland and MSVC
|
williamr@4
|
27 |
// (Dave Abrahams)
|
williamr@4
|
28 |
// 30 Jun 00 More MSVC6 wordarounds. See comments below. (Dave Abrahams)
|
williamr@4
|
29 |
// 28 Jun 00 Removed implicit_cast<>. See comment below. (Beman Dawes)
|
williamr@4
|
30 |
// 27 Jun 00 More MSVC6 workarounds
|
williamr@4
|
31 |
// 15 Jun 00 Add workarounds for MSVC6
|
williamr@4
|
32 |
// 2 Feb 00 Remove bad_numeric_cast ";" syntax error (Doncho Angelov)
|
williamr@4
|
33 |
// 26 Jan 00 Add missing throw() to bad_numeric_cast::what(0 (Adam Levar)
|
williamr@4
|
34 |
// 29 Dec 99 Change using declarations so usages in other namespaces work
|
williamr@4
|
35 |
// correctly (Dave Abrahams)
|
williamr@4
|
36 |
// 23 Sep 99 Change polymorphic_downcast assert to also detect M.I. errors
|
williamr@4
|
37 |
// as suggested Darin Adler and improved by Valentin Bonnard.
|
williamr@4
|
38 |
// 2 Sep 99 Remove controversial asserts, simplify, rename.
|
williamr@4
|
39 |
// 30 Aug 99 Move to cast.hpp, replace value_cast with numeric_cast,
|
williamr@4
|
40 |
// place in nested namespace.
|
williamr@4
|
41 |
// 3 Aug 99 Initial version
|
williamr@4
|
42 |
|
williamr@4
|
43 |
#ifndef BOOST_CAST_HPP
|
williamr@4
|
44 |
#define BOOST_CAST_HPP
|
williamr@4
|
45 |
|
williamr@4
|
46 |
# include <boost/config.hpp>
|
williamr@4
|
47 |
# include <boost/assert.hpp>
|
williamr@4
|
48 |
# include <typeinfo>
|
williamr@4
|
49 |
# include <boost/type.hpp>
|
williamr@4
|
50 |
# include <boost/limits.hpp>
|
williamr@4
|
51 |
# include <boost/detail/select_type.hpp>
|
williamr@4
|
52 |
|
williamr@4
|
53 |
// It has been demonstrated numerous times that MSVC 6.0 fails silently at link
|
williamr@4
|
54 |
// time if you use a template function which has template parameters that don't
|
williamr@4
|
55 |
// appear in the function's argument list.
|
williamr@2
|
56 |
//
|
williamr@4
|
57 |
// TODO: Add this to config.hpp?
|
williamr@4
|
58 |
# if defined(BOOST_MSVC) && BOOST_MSVC < 1300
|
williamr@4
|
59 |
# define BOOST_EXPLICIT_DEFAULT_TARGET , ::boost::type<Target>* = 0
|
williamr@4
|
60 |
# else
|
williamr@4
|
61 |
# define BOOST_EXPLICIT_DEFAULT_TARGET
|
williamr@4
|
62 |
# endif
|
williamr@2
|
63 |
|
williamr@2
|
64 |
namespace boost
|
williamr@2
|
65 |
{
|
williamr@4
|
66 |
// See the documentation for descriptions of how to choose between
|
williamr@4
|
67 |
// static_cast<>, dynamic_cast<>, polymorphic_cast<> and polymorphic_downcast<>
|
williamr@2
|
68 |
|
williamr@4
|
69 |
// polymorphic_cast --------------------------------------------------------//
|
williamr@4
|
70 |
|
williamr@4
|
71 |
// Runtime checked polymorphic downcasts and crosscasts.
|
williamr@4
|
72 |
// Suggested in The C++ Programming Language, 3rd Ed, Bjarne Stroustrup,
|
williamr@4
|
73 |
// section 15.8 exercise 1, page 425.
|
williamr@4
|
74 |
|
williamr@4
|
75 |
template <class Target, class Source>
|
williamr@4
|
76 |
inline Target polymorphic_cast(Source* x BOOST_EXPLICIT_DEFAULT_TARGET)
|
williamr@4
|
77 |
{
|
williamr@4
|
78 |
Target tmp = dynamic_cast<Target>(x);
|
williamr@4
|
79 |
if ( tmp == 0 ) throw std::bad_cast();
|
williamr@4
|
80 |
return tmp;
|
williamr@4
|
81 |
}
|
williamr@4
|
82 |
|
williamr@4
|
83 |
// polymorphic_downcast ----------------------------------------------------//
|
williamr@4
|
84 |
|
williamr@4
|
85 |
// BOOST_ASSERT() checked polymorphic downcast. Crosscasts prohibited.
|
williamr@4
|
86 |
|
williamr@4
|
87 |
// WARNING: Because this cast uses BOOST_ASSERT(), it violates
|
williamr@4
|
88 |
// the One Definition Rule if used in multiple translation units
|
williamr@4
|
89 |
// where BOOST_DISABLE_ASSERTS, BOOST_ENABLE_ASSERT_HANDLER
|
williamr@4
|
90 |
// NDEBUG are defined inconsistently.
|
williamr@4
|
91 |
|
williamr@4
|
92 |
// Contributed by Dave Abrahams
|
williamr@4
|
93 |
|
williamr@4
|
94 |
template <class Target, class Source>
|
williamr@4
|
95 |
inline Target polymorphic_downcast(Source* x BOOST_EXPLICIT_DEFAULT_TARGET)
|
williamr@4
|
96 |
{
|
williamr@4
|
97 |
BOOST_ASSERT( dynamic_cast<Target>(x) == x ); // detect logic error
|
williamr@4
|
98 |
return static_cast<Target>(x);
|
williamr@4
|
99 |
}
|
williamr@4
|
100 |
|
williamr@4
|
101 |
# undef BOOST_EXPLICIT_DEFAULT_TARGET
|
williamr@2
|
102 |
|
williamr@2
|
103 |
} // namespace boost
|
williamr@2
|
104 |
|
williamr@4
|
105 |
# include <boost/numeric/conversion/cast.hpp>
|
williamr@2
|
106 |
|
williamr@4
|
107 |
#endif // BOOST_CAST_HPP
|