| 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_DETAIL_RUNTIME_CAST_IMPL_HPP |
| 10 | #define BOOST_TYPE_INDEX_RUNTIME_CAST_DETAIL_RUNTIME_CAST_IMPL_HPP |
| 11 | |
| 12 | /// \file runtime_cast_impl.hpp |
| 13 | /// \brief Contains the overload of boost::typeindex::runtime_cast for |
| 14 | /// pointer types. |
| 15 | /// |
| 16 | /// boost::typeindex::runtime_cast can be used to emulate dynamic_cast |
| 17 | /// functionality on platorms that don't provide it or should the user |
| 18 | /// desire opt in functionality instead of enabling it system wide. |
| 19 | |
| 20 | #include <boost/type_index.hpp> |
| 21 | |
| 22 | #include <type_traits> |
| 23 | |
| 24 | #ifdef BOOST_HAS_PRAGMA_ONCE |
| 25 | # pragma once |
| 26 | #endif |
| 27 | |
| 28 | namespace boost { namespace typeindex { |
| 29 | |
| 30 | namespace detail { |
| 31 | |
| 32 | template<typename T, typename U> |
| 33 | T* runtime_cast_impl(U* u, std::integral_constant<bool, true>) noexcept { |
| 34 | return u; |
| 35 | } |
| 36 | |
| 37 | template<typename T, typename U> |
| 38 | T const* runtime_cast_impl(U const* u, std::integral_constant<bool, true>) noexcept { |
| 39 | return u; |
| 40 | } |
| 41 | |
| 42 | template<typename T, typename U> |
| 43 | T* runtime_cast_impl(U* u, std::integral_constant<bool, false>) noexcept { |
| 44 | return const_cast<T*>(static_cast<T const*>( |
| 45 | u->boost_type_index_find_instance_(boost::typeindex::type_id<T>()) |
| 46 | )); |
| 47 | } |
| 48 | |
| 49 | template<typename T, typename U> |
| 50 | T const* runtime_cast_impl(U const* u, std::integral_constant<bool, false>) noexcept { |
| 51 | return static_cast<T const*>(u->boost_type_index_find_instance_(boost::typeindex::type_id<T>())); |
| 52 | } |
| 53 | |
| 54 | } // namespace detail |
| 55 | |
| 56 | }} // namespace boost::typeindex |
| 57 | |
| 58 | #endif // BOOST_TYPE_INDEX_RUNTIME_CAST_DETAIL_RUNTIME_CAST_IMPL_HPP |
| 59 | |