mirror of
https://github.com/boostorg/python.git
synced 2026-01-19 16:32:16 +00:00
92 lines
2.6 KiB
Plaintext
92 lines
2.6 KiB
Plaintext
[section boost/python/return_arg.hpp]
|
|
[section Introduction]
|
|
`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.
|
|
[endsect]
|
|
[section Class `return_arg`]
|
|
[table
|
|
[[Parameter][Requirements][Description][Default]]
|
|
[[arg_pos][A positive compile-time constant of type `std::size_t`.][the position of the argument to be returned.][1]]
|
|
[[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]]
|
|
]
|
|
``
|
|
namespace boost { namespace python
|
|
{
|
|
template <size_t arg_pos=1, class Base = default_call_policies>
|
|
struct return_arg : Base
|
|
{
|
|
static PyObject* postcall(PyObject*, PyObject* result);
|
|
struct result_converter{ template <class T> struct apply; };
|
|
template <class Sig> struct extract_return_type : mpl::at_c<Sig, arg_pos>{};
|
|
|
|
};
|
|
}}
|
|
``
|
|
[endsect]
|
|
[section Class `return_arg` static functions]
|
|
``PyObject* postcall(PyObject* args, PyObject* result);``
|
|
[variablelist
|
|
[[Requires][`PyTuple_Check(args) != 0` and `PyTuple_Size(args) != 0`]]
|
|
[[Returns][PyTuple_GetItem(args,arg_pos-1)]]
|
|
]
|
|
[endsect]
|
|
[section Class template `return_self`]
|
|
``
|
|
namespace boost { namespace python
|
|
{
|
|
template <class Base = default_call_policies>
|
|
struct return_self
|
|
: return_arg<1,Base>
|
|
{};
|
|
}}
|
|
``
|
|
[endsect]
|
|
[section Example]
|
|
C++ module definition:
|
|
``
|
|
#include <boost/python/module.hpp>
|
|
#include <boost/python/class.hpp>
|
|
#include <boost/python/return_arg.hpp>
|
|
|
|
struct Widget
|
|
{
|
|
Widget() :sensitive_(true){}
|
|
bool get_sensitive() const { return sensitive_; }
|
|
void set_sensitive(bool s) { this->sensitive_ = s; }
|
|
private:
|
|
bool sensitive_;
|
|
};
|
|
|
|
struct Label : Widget
|
|
{
|
|
Label() {}
|
|
|
|
std::string get_label() const { return label_; }
|
|
void set_label(const std::string &l){ label_ = l; }
|
|
|
|
private:
|
|
std::string label_;
|
|
};
|
|
|
|
using namespace boost::python;
|
|
BOOST_PYTHON_MODULE(return_self_ext)
|
|
{
|
|
class_<widget>("Widget")
|
|
.def("sensitive", &Widget::get_sensitive)
|
|
.def("sensitive", &Widget::set_sensitive, return_self<>())
|
|
;
|
|
|
|
class_<Label, bases<Widget> >("Label")
|
|
.def("label", &Label::get_label)
|
|
.def("label", &Label::set_label, return_self<>())
|
|
;
|
|
}
|
|
``
|
|
Python code:
|
|
``
|
|
>>> from return_self_ext import *
|
|
>>> l1 = Label().label("foo").sensitive(false)
|
|
>>> l2 = Label().sensitive(false).label("foo")
|
|
``
|
|
[endsect]
|
|
[endsect]
|