2 // Copyright (c) Chris Glover, 2016.
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 #ifndef BOOST_TYPE_INDEX_RUNTIME_CAST_REFERENCE_CAST_HPP
10 #define BOOST_TYPE_INDEX_RUNTIME_CAST_REFERENCE_CAST_HPP
12 /// \file reference_cast.hpp
13 /// \brief Contains the overload of boost::typeindex::runtime_cast for
16 #include <boost/core/addressof.hpp>
17 #include <boost/type_index/runtime_cast/detail/runtime_cast_impl.hpp>
18 #include <boost/throw_exception.hpp>
19 #include <boost/type_traits/add_reference.hpp>
20 #include <boost/type_traits/remove_reference.hpp>
21 #include <boost/type_traits/is_base_and_derived.hpp>
23 #ifdef BOOST_HAS_PRAGMA_ONCE
27 namespace boost { namespace typeindex {
29 /// \brief Indicates that runtime_cast was unable to perform the desired cast operation
30 /// because the source instance was not also an instance of the target type.
31 struct bad_runtime_cast : std::exception
34 /// \brief Safely converts references to classes up, down, and sideways along the inheritance hierarchy.
35 /// \tparam T The desired target type. Like dynamic_cast, must be a pointer to complete class type.
36 /// \tparam U A complete class type of the source instance, u.
37 /// \return If there exists a valid conversion from U& to T, returns a T that references an address
38 /// suitably offset from u. If no such conversion exists, throws boost::typeindex::bad_runtime_cast.
39 template<typename T, typename U>
40 typename boost::add_reference<T>::type runtime_cast(U& u) {
41 typedef typename boost::remove_reference<T>::type impl_type;
42 impl_type* value = detail::runtime_cast_impl<impl_type>(
43 boost::addressof(u), boost::is_base_and_derived<T, U>());
45 BOOST_THROW_EXCEPTION(bad_runtime_cast());
49 /// \brief Safely converts references to classes up, down, and sideways along the inheritance hierarchy.
50 /// \tparam T The desired target type. Like dynamic_cast, must be a pointer to complete class type.
51 /// \tparam U A complete class type of the source instance, u.
52 /// \return If there exists a valid conversion from U const& to T const, returns a T const that references an address
53 /// suitably offset from u. If no such conversion exists, throws boost::typeindex::bad_runtime_cast.
54 template<typename T, typename U>
55 typename boost::add_reference<const T>::type runtime_cast(U const& u) {
56 typedef typename boost::remove_reference<T>::type impl_type;
57 impl_type* value = detail::runtime_cast_impl<impl_type>(
58 boost::addressof(u), boost::is_base_and_derived<T, U>());
60 BOOST_THROW_EXCEPTION(bad_runtime_cast());
64 }} // namespace boost::typeindex
66 #endif // BOOST_TYPE_INDEX_RUNTIME_CAST_REFERENCE_CAST_HPP