|
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> objects::function* make_getter(D C::*pm); template <class C, class D, class Policies> objects::function* 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.
PyObject*
which
refers to the new Python callable object.
template <class C, class D> objects::function* make_setter(D C::*pm); template <class C, class D, class Policies> objects::function* 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.
PyObject*
which refers to the new Python callable
object.
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) { module("data_members_example") .add( class_<X>("X") .def_init(args<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
8 May 2002
© Copyright Dave Abrahams 2002. All Rights Reserved.