reference_cast.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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_REFERENCE_CAST_HPP
  9. #define BOOST_TYPE_INDEX_RUNTIME_CAST_REFERENCE_CAST_HPP
  10. /// \file reference_cast.hpp
  11. /// \brief Contains the overload of boost::typeindex::runtime_cast for
  12. /// reference types.
  13. #include <boost/core/addressof.hpp>
  14. #include <boost/type_index/runtime_cast/detail/runtime_cast_impl.hpp>
  15. #include <boost/throw_exception.hpp>
  16. #include <boost/type_traits/add_reference.hpp>
  17. #include <boost/type_traits/remove_reference.hpp>
  18. #include <boost/type_traits/is_base_and_derived.hpp>
  19. #ifdef BOOST_HAS_PRAGMA_ONCE
  20. # pragma once
  21. #endif
  22. namespace boost { namespace typeindex {
  23. /// \brief Indicates that runtime_cast was unable to perform the desired cast operation
  24. /// because the source instance was not also an instance of the target type.
  25. struct bad_runtime_cast : std::exception
  26. {};
  27. /// \brief Safely converts references to classes up, down, and sideways along the inheritance hierarchy.
  28. /// \tparam T The desired target type. Like dynamic_cast, must be a pointer to complete class type.
  29. /// \tparam U A complete class type of the source instance, u.
  30. /// \return If there exists a valid conversion from U& to T, returns a T that references an address
  31. /// suitably offset from u. If no such conversion exists, throws boost::typeindex::bad_runtime_cast.
  32. template<typename T, typename U>
  33. typename boost::add_reference<T>::type runtime_cast(U& u) {
  34. typedef typename boost::remove_reference<T>::type impl_type;
  35. impl_type* value = detail::runtime_cast_impl<impl_type>(
  36. boost::addressof(u), boost::is_base_and_derived<T, U>());
  37. if(!value)
  38. BOOST_THROW_EXCEPTION(bad_runtime_cast());
  39. return *value;
  40. }
  41. /// \brief Safely converts references to classes up, down, and sideways along the inheritance hierarchy.
  42. /// \tparam T The desired target type. Like dynamic_cast, must be a pointer to complete class type.
  43. /// \tparam U A complete class type of the source instance, u.
  44. /// \return If there exists a valid conversion from U const& to T const, returns a T const that references an address
  45. /// suitably offset from u. If no such conversion exists, throws boost::typeindex::bad_runtime_cast.
  46. template<typename T, typename U>
  47. typename boost::add_reference<const T>::type runtime_cast(U const& u) {
  48. typedef typename boost::remove_reference<T>::type impl_type;
  49. impl_type* value = detail::runtime_cast_impl<impl_type>(
  50. boost::addressof(u), boost::is_base_and_derived<T, U>());
  51. if(!value)
  52. BOOST_THROW_EXCEPTION(bad_runtime_cast());
  53. return *value;
  54. }
  55. }} // namespace boost::typeindex
  56. #endif // BOOST_TYPE_INDEX_RUNTIME_CAST_REFERENCE_CAST_HPP