| 1 | // |
| 2 | // Copyright (c) Chris Glover, 2016. |
| 3 | // |
| 4 | // |
| 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) |
| 7 | // |
| 8 | |
| 9 | #ifndef BOOST_TYPE_INDEX_RUNTIME_CAST_POINTER_CAST_HPP |
| 10 | #define BOOST_TYPE_INDEX_RUNTIME_CAST_POINTER_CAST_HPP |
| 11 | |
| 12 | /// \file pointer_class.hpp |
| 13 | /// \brief Contains the function overloads of boost::typeindex::runtime_cast for |
| 14 | /// pointer types. |
| 15 | #include <boost/type_index.hpp> |
| 16 | #include <boost/type_index/runtime_cast/detail/runtime_cast_impl.hpp> |
| 17 | |
| 18 | |
| 19 | #ifdef BOOST_HAS_PRAGMA_ONCE |
| 20 | # pragma once |
| 21 | #endif |
| 22 | |
| 23 | namespace boost { namespace typeindex { |
| 24 | |
| 25 | /// \brief Safely converts pointers to classes up, down, and sideways along the inheritance hierarchy. |
| 26 | /// \tparam T The desired target type. Like dynamic_cast, must be a pointer to complete class type. |
| 27 | /// \tparam U A complete class type of the source instance, u. |
| 28 | /// \return If there exists a valid conversion from U* to T, returns a T that points to |
| 29 | /// an address suitably offset from u. If no such conversion exists, returns nullptr. |
| 30 | template<typename T, typename U> |
| 31 | T runtime_cast(U* u) noexcept { |
| 32 | typedef typename std::remove_pointer<T>::type impl_type; |
| 33 | return detail::runtime_cast_impl<impl_type>(u, std::is_base_of<T, U>()); |
| 34 | } |
| 35 | |
| 36 | /// \brief Safely converts pointers to classes up, down, and sideways along the inheritance hierarchy. |
| 37 | /// \tparam T The desired target type. Like dynamic_cast, must be a pointer to complete class type. |
| 38 | /// \tparam U A complete class type of the source instance, u. |
| 39 | /// \return If there exists a valid conversion from U* to T, returns a T that points to |
| 40 | /// an address suitably offset from u. If no such conversion exists, returns nullptr. |
| 41 | template<typename T, typename U> |
| 42 | T runtime_cast(U const* u) noexcept { |
| 43 | typedef typename std::remove_pointer<T>::type impl_type; |
| 44 | return detail::runtime_cast_impl<impl_type>(u, std::is_base_of<T, U>()); |
| 45 | } |
| 46 | |
| 47 | /// \brief Safely converts pointers to classes up, down, and sideways along the inheritance |
| 48 | /// hierarchy. |
| 49 | /// \tparam T The desired target type to return a pointer to. |
| 50 | /// \tparam U A complete class type of the source instance, u. |
| 51 | /// \return If there exists a valid conversion from U const* to T*, returns a T* |
| 52 | /// that points to an address suitably offset from u. |
| 53 | /// If no such conversion exists, returns nullptr. |
| 54 | template<typename T, typename U> |
| 55 | T* runtime_pointer_cast(U* u) noexcept { |
| 56 | return detail::runtime_cast_impl<T>(u, std::is_base_of<T, U>()); |
| 57 | } |
| 58 | |
| 59 | /// \brief Safely converts pointers to classes up, down, and sideways along the inheritance |
| 60 | /// hierarchy. |
| 61 | /// \tparam T The desired target type to return a pointer to. |
| 62 | /// \tparam U A complete class type of the source instance, u. |
| 63 | /// \return If there exists a valid conversion from U const* to T const*, returns a T const* |
| 64 | /// that points to an address suitably offset from u. |
| 65 | /// If no such conversion exists, returns nullptr. |
| 66 | template<typename T, typename U> |
| 67 | T const* runtime_pointer_cast(U const* u) noexcept { |
| 68 | return detail::runtime_cast_impl<T>(u, std::is_base_of<T, U>()); |
| 69 | } |
| 70 | |
| 71 | }} // namespace boost::typeindex |
| 72 | |
| 73 | #endif // BOOST_TYPE_INDEX_RUNTIME_CAST_POINTER_CAST_HPP |
| 74 | |