registered.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright David Abrahams 2002.
  2. // Copyright Stefan Seefeld 2016.
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef boost_python_converter_registered_hpp_
  7. #define boost_python_converter_registered_hpp_
  8. #include <boost/python/type_id.hpp>
  9. #include <boost/python/converter/registry.hpp>
  10. #include <boost/python/converter/registrations.hpp>
  11. #include <boost/python/detail/type_traits.hpp>
  12. #include <boost/detail/workaround.hpp>
  13. #include <boost/type.hpp>
  14. #include <memory>
  15. #if defined(BOOST_PYTHON_TRACE_REGISTRY) \
  16. || defined(BOOST_PYTHON_CONVERTER_REGISTRY_APPLE_MACH_WORKAROUND)
  17. # include <iostream>
  18. #endif
  19. namespace boost {
  20. // You'll see shared_ptr mentioned in this header because we need to
  21. // note which types are shared_ptrs in their registrations, to
  22. // implement special shared_ptr handling for rvalue conversions.
  23. template <class T> class shared_ptr;
  24. namespace python { namespace converter {
  25. struct registration;
  26. namespace detail
  27. {
  28. template <class T>
  29. struct registered_base
  30. {
  31. static registration const& converters;
  32. };
  33. }
  34. template <class T>
  35. struct registered
  36. : detail::registered_base<
  37. typename boost::python::detail::add_lvalue_reference<
  38. typename boost::python::detail::add_cv<T>::type
  39. >::type
  40. >
  41. {
  42. };
  43. # if !BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1310))
  44. // collapses a few more types to the same static instance. MSVC7.1
  45. // fails to strip cv-qualification from array types in typeid. For
  46. // some reason we can't use this collapse there or array converters
  47. // will not be found.
  48. template <class T>
  49. struct registered<T&>
  50. : registered<T> {};
  51. # endif
  52. //
  53. // implementations
  54. //
  55. namespace detail
  56. {
  57. inline void
  58. register_shared_ptr0(...)
  59. {
  60. }
  61. template <class T>
  62. inline void
  63. register_shared_ptr0(shared_ptr<T>*)
  64. {
  65. registry::lookup_shared_ptr(type_id<shared_ptr<T> >());
  66. }
  67. #if !defined(BOOST_NO_CXX11_SMART_PTR)
  68. template <class T>
  69. inline void
  70. register_shared_ptr0(std::shared_ptr<T>*)
  71. {
  72. registry::lookup_shared_ptr(type_id<std::shared_ptr<T> >());
  73. }
  74. #endif
  75. template <class T>
  76. inline void
  77. register_shared_ptr1(T const volatile*)
  78. {
  79. detail::register_shared_ptr0((T*)0);
  80. }
  81. template <class T>
  82. inline registration const&
  83. registry_lookup2(T&(*)())
  84. {
  85. detail::register_shared_ptr1((T*)0);
  86. return registry::lookup(type_id<T&>());
  87. }
  88. template <class T>
  89. inline registration const&
  90. registry_lookup1(type<T>)
  91. {
  92. return registry_lookup2((T(*)())0);
  93. }
  94. inline registration const&
  95. registry_lookup1(type<const volatile void>)
  96. {
  97. detail::register_shared_ptr1((void*)0);
  98. return registry::lookup(type_id<void>());
  99. }
  100. template <class T>
  101. registration const& registered_base<T>::converters = detail::registry_lookup1(type<T>());
  102. }
  103. }}} // namespace boost::python::converter
  104. #endif