|
|
Boost.PythonHeader <boost/python/data_members.hpp> |
make_getter() and
make_setter() are the
functions used internally by class_<>::def_readonly and
class_<>::def_readwrite to produce
Python callable objects which wrap C++ data members.
template <class C, class D> object make_getter(D C::*pm); template <class C, class D, class Policies> object make_getter(D C::*pm, Policies const& policies);
Policies is a model of CallPolicies.from_python to
C*, and returns the corresponding member D
member of the C object, converted to_python.
If policies is supplied, it will be applied to the
function as described here.template <class C, class D> object make_setter(D C::*pm); template <class C, class D, class Policies> object make_setter(D C::*pm, Policies const& policies);
Policies is a model of CallPolicies.from_python to C* and
D const&, respectively, and sets the
corresponding D member of the C object. If
policies is supplied, it will be applied to the function
as described here.The code below uses make_getter and make_setter to expose a data member as functions:
#include <boost/python/data_members.hpp>
#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
struct X
{
X(int x) : y(x) {}
int y;
};
using namespace boost::python;
BOOST_PYTHON_MODULE_INIT(data_members_example)
{
class_<X>("X", init<int>())
.def("get", make_getter(&X::y))
.def("set", make_setter(&X::y))
;
}
It can be used this way in Python:
>>> from data_members_example import * >>> x = X(1) >>> x.get() 1 >>> x.set(2) >>> x.get() 2
29 September 2002
© Copyright Dave Abrahams 2002. All Rights Reserved.