copy_const_reference.qbk 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. [section boost/python/copy_const_reference.hpp]
  2. [section Class `copy_const_reference`]
  3. `copy_const_reference` is a model of [link concepts.resultconverter.resultconvertergenerator_concept ResultConverterGenerator] which can be used to wrap C++ functions returning a reference-to-const type such that the referenced value is copied into a new Python object.
  4. ``
  5. namespace boost { namespace python
  6. {
  7. struct copy_const_reference
  8. {
  9. template <class T> struct apply;
  10. };
  11. }}
  12. ``
  13. [endsect]
  14. [section Class `copy_const_reference` metafunctions]
  15. ``template <class T> struct apply``
  16. [variablelist
  17. [[Requires][`T` is `U const&` for some `U`.]]
  18. [[Returns][`typedef to_python_value<T> type;`]]
  19. ]
  20. [endsect]
  21. [section Example]
  22. C++ module definition:
  23. ``
  24. #include <boost/python/module.hpp>
  25. #include <boost/python/class.hpp>
  26. #include <boost/python/copy_const_reference.hpp>
  27. #include <boost/python/return_value_policy.hpp>
  28. // classes to wrap
  29. struct Bar { int x; }
  30. struct Foo {
  31. Foo(int x) : { b.x = x; }
  32. Bar const& get_bar() const { return b; }
  33. private:
  34. Bar b;
  35. };
  36. // Wrapper code
  37. using namespace boost::python;
  38. BOOST_PYTHON_MODULE(my_module)
  39. {
  40. class_<Bar>("Bar");
  41. class_<Foo>("Foo", init<int>())
  42. .def("get_bar", &Foo::get_bar
  43. , return_value_policy<copy_const_reference>())
  44. ;
  45. }
  46. ``
  47. Python code:
  48. ``
  49. >>> from my_module import *
  50. >>> f = Foo(3) # create a Foo object
  51. >>> b = f.get_bar() # make a copy of the internal Bar object
  52. ``
  53. [endsect]
  54. [endsect]