<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <!-- Copyright David Abrahams 2006. Distributed under the Boost --> <!-- Software License, Version 1.0. (See accompanying --> <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) --> <html> <head> <meta name="generator" content= "HTML Tidy for Cygwin (vers 1st April 2002), see www.w3.org"> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <link rel="stylesheet" type="text/css" href="../boost.css"> <title>Boost.Python - <boost/python/data_members.hpp></title> </head> <body> <table border="0" cellpadding="7" cellspacing="0" width="100%" summary= "header"> <tr> <td valign="top" width="300"> <h3><a href="../../../../index.htm"><img height="86" width="277" alt="C++ Boost" src="../../../../boost.png" border="0"></a></h3> </td> <td valign="top"> <h1 align="center"><a href="../index.html">Boost.Python</a></h1> <h2 align="center">Header <boost/python/data_members.hpp></h2> </td> </tr> </table> <hr> <h2>Contents</h2> <dl class="page-index"> <dt><a href="#introduction">Introduction</a></dt> <dt><a href="#functions">Functions</a></dt> <dd> <dl class="page-index"> <dt><a href="#make_getter-spec">make_getter</a></dt> <dt><a href="#make_setter-spec">make_setter</a></dt> </dl> </dd> <dt><a href="#examples">Example</a></dt> </dl> <hr> <h2><a name="introduction"></a>Introduction</h2> <p><code><a href="#make_getter-spec">make_getter</a>()</code> and <code><a href="#make_setter-spec">make_setter</a>()</code> are the functions used internally by <code>class_<>::<a href= "class.html#class_-spec-modifiers">def_readonly</a></code> and <code>class_<>::<a href= "class.html#class_-spec-modifiers">def_readwrite</a></code> to produce Python callable objects which wrap C++ data members.</p> <h2><a name="functions"></a>Functions</h2> <pre> <a name="make_getter-spec">template <class C, class D></a> <a href="object.html#object-spec">object</a> make_getter(D C::*pm); template <class C, class D, class Policies> <a href= "object.html#object-spec">object</a> make_getter(D C::*pm, Policies const& policies); </pre> <dl class="function-semantics"> <dt><b>Requires:</b> <code>Policies</code> is a model of <a href= "CallPolicies.html">CallPolicies</a>.</dt> <dt><b>Effects:</b> Creates a Python callable object which accepts a single argument that can be converted <code>from_python</code> to <code>C*</code>, and returns the corresponding member <code>D</code> member of the <code>C</code> object, converted <code>to_python</code>. If <code>policies</code> is supplied, it will be applied to the function as described <a href="CallPolicies.html">here</a>. Otherwise, the library attempts to determine whether <code>D</code> is a user-defined class type, and if so uses <code><a href= "return_internal_reference.html#return_internal_reference-spec">return_internal_reference</a><></code></dt> <dt>for <code>Policies</code>. Note that this test may inappropriately choose <code>return_internal_reference<></code> in some cases when <code>D</code> is a smart pointer type. This is a known defect.</dt> <dt><b>Returns:</b> An instance of <a href= "object.html#object-spec">object</a> which holds the new Python callable object.</dt> </dl> <pre> template <class D> <a href="object.html#object-spec">object</a> make_getter(D const& d); template <class D, class Policies> <a href= "object.html#object-spec">object</a> make_getter(D const& d, Policies const& policies); template <class D> <a href="object.html#object-spec">object</a> make_getter(D const* p); template <class D, class Policies> <a href= "object.html#object-spec">object</a> make_getter(D const* p, Policies const& policies); </pre> <dl class="function-semantics"> <dt><b>Requires:</b> <code>Policies</code> is a model of <a href= "CallPolicies.html">CallPolicies</a>.</dt> <dt><b>Effects:</b> Creates a Python callable object which accepts no arguments and returns <code>d</code> or <code>*p</code>, converted <code>to_python</code> on demand. If <code>policies</code> is supplied, it will be applied to the function as described <a href= "CallPolicies.html">here</a>. Otherwise, the library attempts to determine whether <code>D</code> is a user-defined class type, and if so uses <code><a href= "reference_existing_object.html#reference_existing_object-spec">reference_existing_object</a></code></dt> <dt>for <code>Policies</code>.</dt> <dt><b>Returns:</b> An instance of <a href= "object.html#object-spec">object</a> which holds the new Python callable object.</dt> </dl> <pre> <a name="make_setter-spec">template <class C, class D></a> <a href="object.html#object-spec">object</a> make_setter(D C::*pm); template <class C, class D, class Policies> <a href= "object.html#object-spec">object</a> make_setter(D C::*pm, Policies const& policies); </pre> <dl class="function*-semantics"> <dt><b>Requires:</b> <code>Policies</code> is a model of <a href= "CallPolicies.html">CallPolicies</a>.</dt> <dt><b>Effects:</b> Creates a Python callable object which, when called from Python, expects two arguments which can be converted <code>from_python</code> to <code>C*</code> and <code>D const&</code>, respectively, and sets the corresponding <code>D</code> member of the <code>C</code> object. If <code>policies</code> is supplied, it will be applied to the function as described <a href="CallPolicies.html">here</a>.</dt> <dt><b>Returns:</b> An instance of <a href= "object.html#object-spec">object</a> which holds the new Python callable object.</dt> </dl> <pre> template <class D> <a href="object.html#object-spec">object</a> make_setter(D& d); template <class D, class Policies> <a href= "object.html#object-spec">object</a> make_setter(D& d, Policies const& policies); template <class D> <a href="object.html#object-spec">object</a> make_setter(D* p); template <class D, class Policies> <a href= "object.html#object-spec">object</a> make_setter(D* p, Policies const& policies); </pre> <dl class="function-semantics"> <dt><b>Requires:</b> <code>Policies</code> is a model of <a href= "CallPolicies.html">CallPolicies</a>.</dt> <dt><b>Effects:</b> Creates a Python callable object which accepts one argument, which is converted from Python to <code>D const&</code> and written into <code>d</code> or <code>*p</code>, respectively. If <code>policies</code> is supplied, it will be applied to the function as described <a href="CallPolicies.html">here</a>.</dt> <dt><b>Returns:</b> An instance of <a href= "object.html#object-spec">object</a> which holds the new Python callable object.</dt> </dl> <h2><a name="examples"></a>Example</h2> <p>The code below uses make_getter and make_setter to expose a data member as functions:</p> <pre> #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)) ; } </pre> It can be used this way in Python: <pre> >>> from data_members_example import * >>> x = X(1) >>> x.get() 1 >>> x.set(2) >>> x.get() 2 </pre> <p> <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan --> 5 August, 2003 <!--webbot bot="Timestamp" endspan i-checksum="39359" --> </p> <p><i>© Copyright <a href= "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> </body> </html>