register_ptr_to_python.qbk 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. [section boost/python/register_ptr_to_python.hpp]
  2. [section Introduction]
  3. <boost/python/register_ptr_to_python.hpp> supplies `register_ptr_to_python`, a function template which registers a conversion for smart pointers to Python. The resulting Python object holds a copy of the converted smart pointer, but behaves as though it were a wrapped copy of the pointee. If the pointee type has virtual functions and the class representing its dynamic (most-derived) type has been wrapped, the Python object will be an instance of the wrapper for the most-derived type. More than one smart pointer type for a pointee's class can be registered.
  4. Note that in order to convert a Python `X` object to a `smart_ptr<X>&` (non-const reference), the embedded C++ object must be held by `smart_ptr<X>`, and that when wrapped objects are created by calling the constructor from Python, how they are held is determined by the HeldType parameter to `class_<...>` instances.
  5. [endsect]
  6. [section Function `register_ptr_to_python`]
  7. ``
  8. template <class P>
  9. void register_ptr_to_python()
  10. ``
  11. [variablelist
  12. [[Requires][`P` is [link concepts.dereferenceable Dereferenceable].]]
  13. [[Effects][Allows conversions to-python of P instances. ]]
  14. ]
  15. [endsect]
  16. [section Example]
  17. Here is an example of a module that contains a class A with virtual functions and some functions that work with boost::shared_ptr<A>.
  18. In C++:
  19. ``
  20. struct A
  21. {
  22. virtual int f() { return 0; }
  23. };
  24. shared_ptr<A> New() { return shared_ptr<A>( new A() ); }
  25. int Ok( const shared_ptr<A>& a ) { return a->f(); }
  26. int Fail( shared_ptr<A>& a ) { return a->f(); }
  27. struct A_Wrapper: A
  28. {
  29. A_Wrapper(PyObject* self_): self(self_) {}
  30. int f() { return call_method<int>(self, "f"); }
  31. int default_f() { return A::f(); }
  32. PyObject* self;
  33. };
  34. BOOST_PYTHON_MODULE(register_ptr)
  35. {
  36. class_<A, A_Wrapper>("A")
  37. .def("f", &A::f, &A_Wrapper::default_f)
  38. ;
  39. def("New", &New);
  40. def("Ok", &Call);
  41. def("Fail", &Fail);
  42. register_ptr_to_python< shared_ptr<A> >();
  43. }
  44. ``
  45. In Python:
  46. ``
  47. >>> from register_ptr import *
  48. >>> a = A()
  49. >>> Ok(a) # ok, passed as shared_ptr<A>
  50. 0
  51. >>> Fail(a) # passed as shared_ptr<A>&, and was created in Python!
  52. Traceback (most recent call last):
  53. File "<stdin>", line 1, in ?
  54. TypeError: bad argument type for built-in operation
  55. >>>
  56. >>> na = New() # now "na" is actually a shared_ptr<A>
  57. >>> Ok(a)
  58. 0
  59. >>> Fail(a)
  60. 0
  61. >>>
  62. ``
  63. If shared_ptr<A> is registered as follows:
  64. ``
  65. class_<A, A_Wrapper, shared_ptr<A> >("A")
  66. .def("f", &A::f, &A_Wrapper::default_f)
  67. ;
  68. ``
  69. There will be an error when trying to convert shared_ptr<A> to shared_ptr<A_Wrapper>:
  70. ``
  71. >>> a = New()
  72. Traceback (most recent call last):
  73. File "<stdin>", line 1, in ?
  74. TypeError: No to_python (by-value) converter found for C++ type: class boost::shared_ptr<struct A>
  75. >>>
  76. ``
  77. [endsect]