return_arg.qbk 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. [section boost/python/return_arg.hpp]
  2. [section Introduction]
  3. `return_arg` and `return_self` instantiations are models of [link concepts.callpolicies `CallPolicies`] which return the specified argument parameter (usually `*this`) of a wrapped (member) function.
  4. [endsect]
  5. [section Class `return_arg`]
  6. [table
  7. [[Parameter][Requirements][Description][Default]]
  8. [[arg_pos][A positive compile-time constant of type `std::size_t`.][the position of the argument to be returned.][1]]
  9. [[Base][A model of [link concepts.callpolicies `CallPolicies`]][Used for policy composition. Any `result_converter` it supplies will be overridden by `return_arg`, but its `precall` and `postcall` policies are composed as described here [link concepts.callpolicies `CallPolicies`].][default_call_policies]]
  10. ]
  11. ``
  12. namespace boost { namespace python
  13. {
  14. template <size_t arg_pos=1, class Base = default_call_policies>
  15. struct return_arg : Base
  16. {
  17. static PyObject* postcall(PyObject*, PyObject* result);
  18. struct result_converter{ template <class T> struct apply; };
  19. template <class Sig> struct extract_return_type : mpl::at_c<Sig, arg_pos>{};
  20. };
  21. }}
  22. ``
  23. [endsect]
  24. [section Class `return_arg` static functions]
  25. ``PyObject* postcall(PyObject* args, PyObject* result);``
  26. [variablelist
  27. [[Requires][`PyTuple_Check(args) != 0` and `PyTuple_Size(args) != 0`]]
  28. [[Returns][PyTuple_GetItem(args,arg_pos-1)]]
  29. ]
  30. [endsect]
  31. [section Class template `return_self`]
  32. ``
  33. namespace boost { namespace python
  34. {
  35. template <class Base = default_call_policies>
  36. struct return_self
  37. : return_arg<1,Base>
  38. {};
  39. }}
  40. ``
  41. [endsect]
  42. [section Example]
  43. C++ module definition:
  44. ``
  45. #include <boost/python/module.hpp>
  46. #include <boost/python/class.hpp>
  47. #include <boost/python/return_arg.hpp>
  48. struct Widget
  49. {
  50. Widget() :sensitive_(true){}
  51. bool get_sensitive() const { return sensitive_; }
  52. void set_sensitive(bool s) { this->sensitive_ = s; }
  53. private:
  54. bool sensitive_;
  55. };
  56. struct Label : Widget
  57. {
  58. Label() {}
  59. std::string get_label() const { return label_; }
  60. void set_label(const std::string &l){ label_ = l; }
  61. private:
  62. std::string label_;
  63. };
  64. using namespace boost::python;
  65. BOOST_PYTHON_MODULE(return_self_ext)
  66. {
  67. class_<widget>("Widget")
  68. .def("sensitive", &Widget::get_sensitive)
  69. .def("sensitive", &Widget::set_sensitive, return_self<>())
  70. ;
  71. class_<Label, bases<Widget> >("Label")
  72. .def("label", &Label::get_label)
  73. .def("label", &Label::set_label, return_self<>())
  74. ;
  75. }
  76. ``
  77. Python code:
  78. ``
  79. >>> from return_self_ext import *
  80. >>> l1 = Label().label("foo").sensitive(false)
  81. >>> l2 = Label().sensitive(false).label("foo")
  82. ``
  83. [endsect]
  84. [endsect]