wrapper_base.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright David Abrahams 2004. Distributed under the Boost
  2. // Software License, Version 1.0. (See accompanying
  3. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #ifndef WRAPPER_BASE_DWA2004722_HPP
  5. # define WRAPPER_BASE_DWA2004722_HPP
  6. # include <boost/python/detail/prefix.hpp>
  7. # include <boost/python/detail/type_traits.hpp>
  8. namespace boost { namespace python {
  9. class override;
  10. namespace detail
  11. {
  12. class wrapper_base;
  13. namespace wrapper_base_ // ADL disabler
  14. {
  15. inline PyObject* get_owner(wrapper_base const volatile& w);
  16. inline PyObject*
  17. owner_impl(void const volatile* /*x*/, detail::false_)
  18. {
  19. return 0;
  20. }
  21. template <class T>
  22. inline PyObject*
  23. owner_impl(T const volatile* x, detail::true_);
  24. template <class T>
  25. inline PyObject*
  26. owner(T const volatile* x)
  27. {
  28. return wrapper_base_::owner_impl(x,is_polymorphic<T>());
  29. }
  30. }
  31. class BOOST_PYTHON_DECL wrapper_base
  32. {
  33. friend void initialize_wrapper(PyObject* self, wrapper_base* w);
  34. friend PyObject* wrapper_base_::get_owner(wrapper_base const volatile& w);
  35. protected:
  36. wrapper_base() : m_self(0) {}
  37. override get_override(
  38. char const* name, PyTypeObject* class_object) const;
  39. private:
  40. void detach();
  41. private:
  42. PyObject* m_self;
  43. };
  44. namespace wrapper_base_ // ADL disabler
  45. {
  46. template <class T>
  47. inline PyObject*
  48. owner_impl(T const volatile* x, detail::true_)
  49. {
  50. if (wrapper_base const volatile* w = dynamic_cast<wrapper_base const volatile*>(x))
  51. {
  52. return wrapper_base_::get_owner(*w);
  53. }
  54. return 0;
  55. }
  56. inline PyObject* get_owner(wrapper_base const volatile& w)
  57. {
  58. return w.m_self;
  59. }
  60. }
  61. inline void initialize_wrapper(PyObject* self, wrapper_base* w)
  62. {
  63. w->m_self = self;
  64. }
  65. inline void initialize_wrapper(PyObject* /*self*/, ...) {}
  66. } // namespace detail
  67. }} // namespace boost::python
  68. #endif // WRAPPER_BASE_DWA2004722_HPP