mirror of
https://github.com/boostorg/python.git
synced 2026-01-21 05:02:17 +00:00
151 lines
4.8 KiB
HTML
151 lines
4.8 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<meta name="generator" content="HTML Tidy, 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>
|
|
|
|
<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="../../../../c++boost.gif" border="0"></a></h3>
|
|
|
|
<td valign="top">
|
|
<h1 align="center">Boost.Python</h1>
|
|
|
|
<h2 align="center">Header <boost/python/data_members.hpp></h2>
|
|
</table>
|
|
<hr>
|
|
|
|
<h2>Contents</h2>
|
|
|
|
<dl class="page-index">
|
|
<dt><a href="#introduction">Introduction</a>
|
|
|
|
<dt><a href="#functions">Functions</a>
|
|
|
|
<dd>
|
|
<dl class="page-index">
|
|
<dt><a href="#make_getter-spec">make_getter</a>
|
|
|
|
<dt><a href="#make_setter-spec">make_setter</a>
|
|
</dl>
|
|
|
|
<dt><a href="#examples">Example</a>
|
|
</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.
|
|
|
|
<h2><a name="functions"></a>Functions</h2>
|
|
|
|
<pre>
|
|
<a name="make_getter-spec">template <class C, class D></a>
|
|
objects::function* make_getter(D C::*pm);
|
|
|
|
template <class C, class D, class Policies>
|
|
objects::function* 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><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>.
|
|
|
|
<dt><b>Returns:</b> A pointer convertible to <code>PyObject*</code> which
|
|
refers to the new Python callable object.
|
|
</dl>
|
|
<pre>
|
|
<a name="make_setter-spec">template <class C, class D></a>
|
|
objects::function* make_setter(D C::*pm);
|
|
|
|
template <class C, class D, class Policies>
|
|
objects::function* 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><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><b>Returns:</b> A pointer convertible to
|
|
<code>PyObject*</code> which refers to the new Python callable
|
|
object.
|
|
</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:
|
|
|
|
<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)
|
|
{
|
|
module("data_members_example")
|
|
.add(
|
|
class_<X>("X")
|
|
.def_init(args<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 -->
|
|
8 May 2002 <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
|
|
|
|
|
|
<p><i>© Copyright <a href="../../../../people/dave_abrahams.htm">Dave
|
|
Abrahams</a> 2002. All Rights Reserved.</i>
|
|
|