From 1d160762b55804ff87b84c5b859ed713ebcf88bc Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Wed, 8 May 2002 22:13:30 +0000 Subject: [PATCH] initial commit [SVN r13762] --- doc/v2/data_members.html | 150 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 doc/v2/data_members.html diff --git a/doc/v2/data_members.html b/doc/v2/data_members.html new file mode 100644 index 00000000..820bb1d4 --- /dev/null +++ b/doc/v2/data_members.html @@ -0,0 +1,150 @@ + + + + + + Boost.Python - <boost/python/data_members.hpp> + + + +
+

+

+ +
+

Boost.Python

+ +

Header <boost/python/data_members.hpp>

+
+
+ +

Contents

+ +
+
Introduction + +
Functions + +
+
+
make_getter + +
make_setter +
+ +
Example +
+
+ +

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>
+objects::function* make_getter(D C::*pm);
+
+template <class C, class D, class Policies>
+objects::function* make_getter(D C::*pm, Policies const& policies);
+
+ +
+
Requires: Policies is a model of CallPolicies. + +
Effects: Creates a Python callable object which + accepts a single argument that can be converted + 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. + +
Returns: A pointer convertible to 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);
+
+ +
+
Requires: Policies is a model of CallPolicies. + +
Effects: Creates a Python callable object which, when + called from Python, expects two arguments which can be converted + 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. + +
Returns: A pointer convertible to + PyObject* which refers to 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)
+{
+    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. +