type_info.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2016 Klemens Morgenstern, Antony Polukhin
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. // For more information, see http://www.boost.org
  7. #ifndef BOOST_DLL_DETAIL_TYPE_INFO_HPP_
  8. #define BOOST_DLL_DETAIL_TYPE_INFO_HPP_
  9. #include <typeinfo>
  10. #include <cstring>
  11. #include <boost/dll/config.hpp>
  12. #if defined(BOOST_MSVC) || defined(BOOST_MSVC_VER)
  13. #include <boost/winapi/basic_types.hpp>
  14. #endif
  15. namespace boost { namespace dll { namespace detail {
  16. #if defined(BOOST_MSVC) || defined(BOOST_MSVC_VER)
  17. #if defined ( _WIN64 )
  18. template<typename Class, typename Lib, typename Storage>
  19. const std::type_info& load_type_info(Lib & lib, Storage & storage)
  20. {
  21. struct RTTICompleteObjectLocator
  22. {
  23. boost::winapi::DWORD_ signature; //always zero ?
  24. boost::winapi::DWORD_ offset; //offset of this vtable in the complete class
  25. boost::winapi::DWORD_ cdOffset; //constructor displacement offset
  26. boost::winapi::DWORD_ pTypeDescriptorOffset; //TypeDescriptor of the complete class
  27. boost::winapi::DWORD_ pClassDescriptorOffset; //describes inheritance hierarchy (ignored)
  28. };
  29. RTTICompleteObjectLocator** vtable_p = &lib.template get<RTTICompleteObjectLocator*>(storage.template get_vtable<Class>());
  30. vtable_p--;
  31. auto vtable = *vtable_p;
  32. auto nat = reinterpret_cast<const char*>(lib.native());
  33. nat += vtable->pTypeDescriptorOffset;
  34. return *reinterpret_cast<const std::type_info*>(nat);
  35. }
  36. #else
  37. template<typename Class, typename Lib, typename Storage>
  38. const std::type_info& load_type_info(Lib & lib, Storage & storage)
  39. {
  40. struct RTTICompleteObjectLocator
  41. {
  42. boost::winapi::DWORD_ signature; //always zero ?
  43. boost::winapi::DWORD_ offset; //offset of this vtable in the complete class
  44. boost::winapi::DWORD_ cdOffset; //constructor displacement offset
  45. const std::type_info* pTypeDescriptor; //TypeDescriptor of the complete class
  46. void* pClassDescriptor; //describes inheritance hierarchy (ignored)
  47. };
  48. RTTICompleteObjectLocator** vtable_p = &lib.template get<RTTICompleteObjectLocator*>(storage.template get_vtable<Class>());
  49. vtable_p--;
  50. auto vtable = *vtable_p;
  51. return *vtable->pTypeDescriptor;
  52. }
  53. #endif //_WIN64
  54. #else
  55. template<typename Class, typename Lib, typename Storage>
  56. const std::type_info& load_type_info(Lib & lib, Storage & storage)
  57. {
  58. return lib.template get<const std::type_info>(storage.template get_type_info<Class>());
  59. }
  60. #endif
  61. }}}
  62. #endif /* BOOST_DLL_DETAIL_TYPE_INFO_HPP_ */