aggressive_ptr_cast.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
  2. // Copyright 2015-2021 Antony Polukhin.
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt
  6. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_DLL_DETAIL_AGGRESSIVE_PTR_CAST_HPP
  8. #define BOOST_DLL_DETAIL_AGGRESSIVE_PTR_CAST_HPP
  9. #include <boost/dll/config.hpp>
  10. #ifdef BOOST_HAS_PRAGMA_ONCE
  11. # pragma once
  12. #endif
  13. #include <boost/core/addressof.hpp>
  14. #include <boost/core/enable_if.hpp>
  15. #include <boost/static_assert.hpp>
  16. #include <boost/type_traits/is_pointer.hpp>
  17. #include <boost/type_traits/is_member_pointer.hpp>
  18. #include <boost/type_traits/is_void.hpp>
  19. #include <boost/type_traits/is_reference.hpp>
  20. #include <boost/type_traits/remove_pointer.hpp>
  21. #include <boost/type_traits/remove_reference.hpp>
  22. #include <cstring> // std::memcpy
  23. #if defined(__GNUC__) && defined(__GNUC_MINOR__) && (__GNUC__ * 100 + __GNUC_MINOR__ > 301)
  24. # pragma GCC system_header
  25. #endif
  26. namespace boost { namespace dll { namespace detail {
  27. // GCC warns when reinterpret_cast between function pointer and object pointer occur.
  28. // This method suppress the warnings and ensures that such casts are safe.
  29. template <class To, class From>
  30. BOOST_FORCEINLINE typename boost::disable_if_c<boost::is_member_pointer<To>::value || boost::is_reference<To>::value || boost::is_member_pointer<From>::value, To>::type
  31. aggressive_ptr_cast(From v) BOOST_NOEXCEPT
  32. {
  33. BOOST_STATIC_ASSERT_MSG(
  34. boost::is_pointer<To>::value && boost::is_pointer<From>::value,
  35. "`agressive_ptr_cast` function must be used only for pointer casting."
  36. );
  37. BOOST_STATIC_ASSERT_MSG(
  38. boost::is_void< typename boost::remove_pointer<To>::type >::value
  39. || boost::is_void< typename boost::remove_pointer<From>::type >::value,
  40. "`agressive_ptr_cast` function must be used only for casting to or from void pointers."
  41. );
  42. BOOST_STATIC_ASSERT_MSG(
  43. sizeof(v) == sizeof(To),
  44. "Pointer to function and pointer to object differ in size on your platform."
  45. );
  46. return reinterpret_cast<To>(v);
  47. }
  48. #ifdef BOOST_MSVC
  49. # pragma warning(push)
  50. # pragma warning(disable: 4172) // "returning address of local variable or temporary" but **v is not local!
  51. #endif
  52. template <class To, class From>
  53. BOOST_FORCEINLINE typename boost::disable_if_c<!boost::is_reference<To>::value || boost::is_member_pointer<From>::value, To>::type
  54. aggressive_ptr_cast(From v) BOOST_NOEXCEPT
  55. {
  56. BOOST_STATIC_ASSERT_MSG(
  57. boost::is_pointer<From>::value,
  58. "`agressive_ptr_cast` function must be used only for pointer casting."
  59. );
  60. BOOST_STATIC_ASSERT_MSG(
  61. boost::is_void< typename boost::remove_pointer<From>::type >::value,
  62. "`agressive_ptr_cast` function must be used only for casting to or from void pointers."
  63. );
  64. BOOST_STATIC_ASSERT_MSG(
  65. sizeof(v) == sizeof(typename boost::remove_reference<To>::type*),
  66. "Pointer to function and pointer to object differ in size on your platform."
  67. );
  68. return static_cast<To>(
  69. **reinterpret_cast<typename boost::remove_reference<To>::type**>(
  70. v
  71. )
  72. );
  73. }
  74. #ifdef BOOST_MSVC
  75. # pragma warning(pop)
  76. #endif
  77. template <class To, class From>
  78. BOOST_FORCEINLINE typename boost::disable_if_c<!boost::is_member_pointer<To>::value || boost::is_member_pointer<From>::value, To>::type
  79. aggressive_ptr_cast(From v) BOOST_NOEXCEPT
  80. {
  81. BOOST_STATIC_ASSERT_MSG(
  82. boost::is_pointer<From>::value,
  83. "`agressive_ptr_cast` function must be used only for pointer casting."
  84. );
  85. BOOST_STATIC_ASSERT_MSG(
  86. boost::is_void< typename boost::remove_pointer<From>::type >::value,
  87. "`agressive_ptr_cast` function must be used only for casting to or from void pointers."
  88. );
  89. To res = 0;
  90. std::memcpy(&res, &v, sizeof(From));
  91. return res;
  92. }
  93. template <class To, class From>
  94. BOOST_FORCEINLINE typename boost::disable_if_c<boost::is_member_pointer<To>::value || !boost::is_member_pointer<From>::value, To>::type
  95. aggressive_ptr_cast(From /* v */) BOOST_NOEXCEPT
  96. {
  97. BOOST_STATIC_ASSERT_MSG(
  98. boost::is_pointer<To>::value,
  99. "`agressive_ptr_cast` function must be used only for pointer casting."
  100. );
  101. BOOST_STATIC_ASSERT_MSG(
  102. boost::is_void< typename boost::remove_pointer<To>::type >::value,
  103. "`agressive_ptr_cast` function must be used only for casting to or from void pointers."
  104. );
  105. BOOST_STATIC_ASSERT_MSG(
  106. !sizeof(From),
  107. "Casting from member pointers to void pointer is not implemnted in `agressive_ptr_cast`."
  108. );
  109. return 0;
  110. }
  111. }}} // boost::dll::detail
  112. #endif // BOOST_DLL_DETAIL_AGGRESSIVE_PTR_CAST_HPP