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