Boost.Python
Header <boost/python/data_members.hpp>
Contents
Introduction
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.
Functions
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);
- Requires:
Policiesis a model of CallPolicies. - Effects: Creates a Python callable object which accepts a
single argument that can be converted
from_pythontoC*, and returns the corresponding memberDmember of theCobject, convertedto_python. Ifpoliciesis supplied, it will be applied to the function as described here. Otherwise, the library attempts to determine whetherDis a user-defined class type, and if so usesreturn_internal_reference<> - for
Policies. Note that this test may inappropriately choosereturn_internal_reference<>in some cases whenDis a smart pointer type. This is a known defect. - Returns: An instance of object which holds the new Python callable object.
template <class D> object make_getter(D const& d); template <class D, class Policies> object make_getter(D const& d, Policies const& policies); template <class D> object make_getter(D const* p); template <class D, class Policies> object make_getter(D const* p, Policies const& policies);
- Requires:
Policiesis a model of CallPolicies. - Effects: Creates a Python callable object which accepts no
arguments and returns
dor*p, convertedto_pythonon demand. Ifpoliciesis supplied, it will be applied to the function as described here. Otherwise, the library attempts to determine whetherDis a user-defined class type, and if so usesreference_existing_object - for
Policies. - Returns: An instance of object which holds the new Python callable object.
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);
- Requires:
Policiesis a model of CallPolicies. - Effects: Creates a Python callable object which, when called
from Python, expects two arguments which can be converted
from_pythontoC*andD const&, respectively, and sets the correspondingDmember of theCobject. Ifpoliciesis supplied, it will be applied to the function as described here. - Returns: An instance of object which holds the new Python callable object.
template <class D> object make_setter(D& d); template <class D, class Policies> object make_setter(D& d, Policies const& policies); template <class D> object make_setter(D* p); template <class D, class Policies> object make_setter(D* p, Policies const& policies);
- Requires:
Policiesis a model of CallPolicies. - Effects: Creates a Python callable object which accepts one
argument, which is converted from Python to
D const&and written intodor*p, respectively. Ifpoliciesis supplied, it will be applied to the function as described here. - Returns: An instance of object which holds the new Python callable object.
Example
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
5 August, 2003
© Copyright Dave Abrahams 2002.
