void_ptr_cast.hpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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_STACKTRACE_DETAIL_VOID_PTR_CAST_HPP
  8. #define BOOST_STACKTRACE_DETAIL_VOID_PTR_CAST_HPP
  9. #include <boost/config.hpp>
  10. #ifdef BOOST_HAS_PRAGMA_ONCE
  11. # pragma once
  12. #endif
  13. #include <boost/static_assert.hpp>
  14. #include <boost/type_traits/is_pointer.hpp>
  15. #if defined(__GNUC__) && defined(__GNUC_MINOR__) && (__GNUC__ * 100 + __GNUC_MINOR__ > 301)
  16. # pragma GCC system_header
  17. #endif
  18. namespace boost { namespace stacktrace { namespace detail {
  19. // GCC warns when reinterpret_cast between function pointer and object pointer occur.
  20. // This functionsuppress the warnings and ensures that such casts are safe.
  21. template <class To, class From>
  22. To void_ptr_cast(From* v) BOOST_NOEXCEPT {
  23. BOOST_STATIC_ASSERT_MSG(
  24. boost::is_pointer<To>::value,
  25. "`void_ptr_cast` function must be used only for casting to or from void pointers."
  26. );
  27. BOOST_STATIC_ASSERT_MSG(
  28. sizeof(From*) == sizeof(To),
  29. "Pointer to function and pointer to object differ in size on your platform."
  30. );
  31. return reinterpret_cast<To>(v);
  32. }
  33. }}} // boost::stacktrace::detail
  34. #endif // BOOST_STACKTRACE_DETAIL_VOID_PTR_CAST_HPP