manage_new_object.qbk 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. [section boost/python/manage_new_object.hpp]
  2. [section Class `manage_new_object`]
  3. `manage_new_object` is a model of [link concepts.resultconverter.resultconvertergenerator_concept ResultConverterGenerator] which can be used to wrap C++ functions which return a pointer to an object allocated with a new-expression, and expect the caller to take responsibility for deleting that object.
  4. ``
  5. namespace boost { namespace python
  6. {
  7. struct manage_new_object
  8. {
  9. template <class T> struct apply;
  10. };
  11. }}
  12. ``
  13. [endsect]
  14. [section Class `manage_new_object` metafunctions]
  15. ``template <class T> struct apply``
  16. [variablelist
  17. [[Requires][`T` is `U*` for some `U`.]]
  18. [[Returns][`typedef to_python_indirect<T> type;`]]
  19. ]
  20. [endsect]
  21. [section Example]
  22. In C++:
  23. ``
  24. #include <boost/python/module.hpp>
  25. #include <boost/python/class.hpp>
  26. #include <boost/python/manage_new_object.hpp>
  27. #include <boost/python/return_value_policy.hpp>
  28. struct Foo {
  29. Foo(int x) : x(x){}
  30. int get_x() { return x; }
  31. int x;
  32. };
  33. Foo* make_foo(int x) { return new Foo(x); }
  34. // Wrapper code
  35. using namespace boost::python;
  36. BOOST_PYTHON_MODULE(my_module)
  37. {
  38. def("make_foo", make_foo, return_value_policy<manage_new_object>())
  39. class_<Foo>("Foo")
  40. .def("get_x", &Foo::get_x)
  41. ;
  42. }
  43. ``
  44. Python code:
  45. ``
  46. >>> from my_module import *
  47. >>> f = make_foo(3) # create a Foo object
  48. >>> f.get_x()
  49. 3
  50. ``
  51. [endsect]
  52. [endsect]