runtime_cast_impl.hpp 1.6 KB

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