wrapper_base.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/type_traits/is_polymorphic.hpp>
  8. # include <boost/mpl/bool.hpp>
  9. namespace boost { namespace python {
  10. class override;
  11. namespace detail
  12. {
  13. class BOOST_PYTHON_DECL_FORWARD wrapper_base;
  14. namespace wrapper_base_ // ADL disabler
  15. {
  16. inline PyObject* get_owner(wrapper_base const volatile& w);
  17. inline PyObject*
  18. owner_impl(void const volatile* /*x*/, mpl::false_)
  19. {
  20. return 0;
  21. }
  22. template <class T>
  23. inline PyObject*
  24. owner_impl(T const volatile* x, mpl::true_);
  25. template <class T>
  26. inline PyObject*
  27. owner(T const volatile* x)
  28. {
  29. return wrapper_base_::owner_impl(x,is_polymorphic<T>());
  30. }
  31. }
  32. class BOOST_PYTHON_DECL wrapper_base
  33. {
  34. friend void initialize_wrapper(PyObject* self, wrapper_base* w);
  35. friend PyObject* wrapper_base_::get_owner(wrapper_base const volatile& w);
  36. protected:
  37. wrapper_base() : m_self(0) {}
  38. override get_override(
  39. char const* name, PyTypeObject* class_object) const;
  40. private:
  41. void detach();
  42. private:
  43. PyObject* m_self;
  44. };
  45. namespace wrapper_base_ // ADL disabler
  46. {
  47. template <class T>
  48. inline PyObject*
  49. owner_impl(T const volatile* x, mpl::true_)
  50. {
  51. if (wrapper_base const volatile* w = dynamic_cast<wrapper_base const volatile*>(x))
  52. {
  53. return wrapper_base_::get_owner(*w);
  54. }
  55. return 0;
  56. }
  57. inline PyObject* get_owner(wrapper_base const volatile& w)
  58. {
  59. return w.m_self;
  60. }
  61. }
  62. inline void initialize_wrapper(PyObject* self, wrapper_base* w)
  63. {
  64. w->m_self = self;
  65. }
  66. inline void initialize_wrapper(PyObject* /*self*/, ...) {}
  67. } // namespace detail
  68. }} // namespace boost::python
  69. #endif // WRAPPER_BASE_DWA2004722_HPP