pointer_cast.hpp 3.3 KB

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