boost/python/return_arg.hpp
はじめに
return_arg
および return_self
のインスタンスは、ラップする(メンバ)関数の指定した引数(大抵は *this)を返す CallPolicies モデルである。
クラス
return_arg
クラステンプレート
-
template<size_t arg_pos = 1, class Base = default_call_policies>
struct return_arg : Base - テンプレートパラメータ
arg_pos --
返す引数の位置。1
- 要件
std::size_t
型の正のコンパイル時定数。
Base --
ポリシーの合成に使用。提供する
result_converter
はreturn_arg
によりオーバーライドされるが、そのprecall
およびpostcall
ポリシーは CallPolicies の項に示すとおり合成される。- 要件
CallPolicies のモデル
- 既定
return_arg
クラステンプレートの概要
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>{};
};
}}
return_arg
クラステンプレートの静的関数
-
PyObject *postcall(PyObject *args, PyObject *result)
- 要件
PyTuple_Check(args) != 0 かつ PyTuple_Size(args) != 0
- 戻り値
PyTuple_GetItem(args, arg_pos - 1)
return_self
クラステンプレート
-
template<class Base = default_call_policies>
struct return_self : return_arg<1, Base>
return_self
クラステンプレートの概要
namespace boost { namespace python
{
template <class Base = default_call_policies>
struct return_self
: return_arg<1,Base>
{};
}}
例
#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<>())
;
}
>>> from return_self_ext import *
>>> l1 = Label().label("foo").sensitive(false)
>>> l2 = Label().sensitive(false).label("foo")
- 1
訳注
arg_pos
テンプレート引数に 0 を指定することはできません。