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