aggressive_ptr_cast.hpp 4.5 KB

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