Skip to content

Commit 67b3cdc

Browse files
committed
lvalue_from_pytype + documentation
[SVN r14030]
1 parent 051994b commit 67b3cdc

File tree

4 files changed

+410
-31
lines changed

4 files changed

+410
-31
lines changed

doc/v2/Extractor.html

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<html>
2+
<head>
3+
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
4+
<link rel="stylesheet" type="text/css" href="../../../boost.css">
5+
<title>Boost.Python - Extractor Concept</title>
6+
</head>
7+
<body link="#0000ff" vlink="#800080">
8+
<table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
9+
"header">
10+
<tr>
11+
<td valign="top" width="300">
12+
<h3><a href="../../../../index.htm"><img height="86" width="277" alt="C++ Boost" src="../../../../c++boost.gif" border="0"></a></h3>
13+
</td>
14+
<td valign="top">
15+
<h1 align="center">Boost.Python</h1>
16+
<h2 align="center">Extractor Concept</h2>
17+
</td>
18+
</tr>
19+
</table>
20+
<hr>
21+
<dl class="page-index">
22+
<dt><a href="#introduction">Introduction</a></dt>
23+
<dt><a href="#concept-requirements">Concept Requirements</a></dt>
24+
<dl class="page-index">
25+
<dt><a href="#Extractor-concept">Extractor Concept</a></dt>
26+
</dl>
27+
<dt><a href="#notes">Notes</a></dt>
28+
</dl>
29+
30+
<h2><a name="introduction"></a>Introduction</h2>
31+
32+
<p>An Extractor is a class which Boost.Python can use to extract C++
33+
objects from Python objects, and is typically used by facilities that
34+
define <code>from_python</code> conversions for
35+
&quot;traditional&quot; Python extension types.
36+
37+
<h2><a name="concept-requirements"></a>Concept Requirements</h2>
38+
<h3><a name="Extractor-concept"></a>Extractor Concept</h3>
39+
40+
<p>In the table below, <code><b>X</b></code> denotes a model of
41+
Extractor and <code><b>a</b></code> denotes an instance of a Python
42+
object type.
43+
44+
<table summary="Extractor expressions" border="1" cellpadding="5">
45+
46+
<tr>
47+
<td><b>Expression</b></td>
48+
<td><b>Type</b></td>
49+
<td><b>Semantics</b></td>
50+
</tr>
51+
52+
<tr>
53+
<td valign="top"><code>X::execute(a)</code></td>
54+
<td>non-void
55+
<td>Returns the C++ object being extracted. The
56+
<code>execute</code> function must not be overloaded.
57+
</tr>
58+
59+
<tr>
60+
<td valign="top"><code>&amp;a.ob_type</code>
61+
<td><code><a
62+
href="http://www.python.org/doc/2.2/ext/dnt-type-methods.html">PyTypeObject</a>**</code>
63+
<td>Points to the <code>ob_type</code> field of an object which is
64+
layout-compatible with <code>PyObject</code>
65+
</tr>
66+
67+
</tr>
68+
69+
</table>
70+
71+
<h2><a name="notes"></a>Notes</h2>
72+
73+
Informally, an Extractor's <code>execute</code> member must be a
74+
non-overloaded static function whose single argument is a Python
75+
object type. Acceptable Python object types include those publicly (and
76+
unambiguously) derived from <code>PyObject</code>, and POD types which
77+
are layout-compatible with PyObject.
78+
79+
<hr>
80+
<p>Revised
81+
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
82+
22 May, 2002
83+
<!--webbot bot="Timestamp" endspan i-checksum="39359" -->
84+
</p>
85+
<p><i>&copy; Copyright <a href="../../../../people/dave_abrahams.htm">Dave
86+
Abrahams</a> 2002. All Rights Reserved.</i>
87+
88+
<p>Permission to copy, use, modify, sell
89+
and distribute this software is granted provided this copyright notice appears
90+
in all copies. This software is provided "as is" without express or implied
91+
warranty, and with no claim as to its suitability for any purpose.
92+
</body>
93+
</html>

doc/v2/lvalue_from_pytype.html

Lines changed: 283 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,283 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2+
<meta name="generator" content="HTML Tidy, see www.w3.org">
3+
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
4+
<link rel="stylesheet" type="text/css" href="../boost.css">
5+
6+
<title>Boost.Python - &lt;boost/python/lvalue_from_python.hpp&gt;</title>
7+
8+
<table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
9+
"header">
10+
<tr>
11+
<td valign="top" width="300">
12+
<h3><a href="../../../../index.htm"><img height="86" width="277" alt=
13+
"C++ Boost" src="../../../../c++boost.gif" border="0"></a></h3>
14+
15+
<td valign="top">
16+
<h1 align="center">Boost.Python</h1>
17+
18+
<h2 align="center">Header &lt;boost/python/lvalue_from_pytype.hpp&gt;</h2>
19+
</table>
20+
<hr>
21+
22+
<h2>Contents</h2>
23+
24+
<dl class="page-index">
25+
<dt><a href="#introduction">Introduction</a>
26+
27+
28+
<dt><a href="#classes">Classes</a>
29+
30+
<dd>
31+
<dl class="page-index">
32+
<dt><a href="#lvalue_from_pytype-spec">Class Template <code>lvalue_from_pytype</code></a>
33+
34+
<dd>
35+
<dl class="page-index">
36+
37+
<dt><a href="#lvalue_from_pytype-spec-synopsis">Class Template
38+
<code>lvalue_from_pytype</code> synopsis</a>
39+
40+
<dt><a href="#lvalue_from_pytype-spec-ctors">Class Template
41+
<code>lvalue_from_pytype</code> constructor</a>
42+
</dl>
43+
</dl>
44+
45+
<dl class="page-index">
46+
<dt><a href="#extract_identity-spec">Class Template <code>extract_identity</code></a>
47+
<dd>
48+
<dl class="page-index">
49+
50+
<dt><a href="#extract_identity-spec-synopsis">Class Template
51+
<code>extract_identity</code> synopsis</a>
52+
53+
<dt><a href="#extract_identity-spec-statics">Class Template
54+
<code>extract_identity</code> static functions</a>
55+
</dl>
56+
<dt><a href="#extract_member-spec">Class Template <code>extract_member</code></a>
57+
<dd>
58+
<dl class="page-index">
59+
60+
<dt><a href="#extract_member-spec-synopsis">Class Template
61+
<code>extract_member</code> synopsis</a>
62+
63+
<dt><a href="#extract_member-spec-statics">Class Template
64+
<code>extract_member</code> static functions</a>
65+
</dl>
66+
</dl>
67+
68+
<dt><a href="#examples">Example</a>
69+
</dl>
70+
<hr>
71+
72+
<h2><a name="introduction"></a>Introduction</h2>
73+
74+
<code>&lt;boost/python/lvalue_from_pytype.hpp&gt;</code> supplies
75+
a facility for extracting C++ objects from within Python instances
76+
of a given type. This is typically useful for dealing with
77+
&quot;traditional&quot; Python extension types.
78+
79+
<h2><a name="classes"></a>Classes</h2>
80+
81+
<h3><a name="lvalue_from_pytype-spec"></a>Class template <code>lvalue_from_pytype</code></h3>
82+
83+
<p>Class template <code>lvalue_from_pytype</code> will register
84+
from_python converters which, given an object of the given Python
85+
type, can extract references and pointers to a particular C++
86+
type. Its template arguments are:
87+
88+
<p>
89+
90+
91+
<table border="1" summary="lvalue_from_pytype template parameters">
92+
<caption>
93+
<b><code>lvalue_from_pytype</code> Requirements</b><br>
94+
95+
In the table below, <b><code>x</code></b> denotes an object of type <code>PythonObject&amp;</code>
96+
97+
</caption>
98+
<tr>
99+
<th>Parameter
100+
101+
<th>Requirements
102+
103+
<th>Semantics
104+
105+
<tr>
106+
<td><code>Extractor</code>
107+
108+
<td>a model of <a
109+
href="Extractor.html#Extractor-concept">Extractor</a> whose
110+
execute function returns a reference type.
111+
112+
<td>Extracts the lvalue from the Python object once its type has been confirmed
113+
114+
<tr>
115+
<td><code>python_type</code>
116+
117+
<td>A compile-time constant <code><a
118+
href="http://www.python.org/doc/2.2/ext/dnt-type-methods.html">PyTypeObject</a>*</code>
119+
120+
<td>The Python type of instances convertible by this
121+
converter. Python subtypes are also convertible.
122+
123+
</table>
124+
125+
<h4><a name="lvalue_from_pytype-spec-synopsis"></a>Class template <code>lvalue_from_pytype</code> synopsis</h4>
126+
<pre>
127+
namespace boost { namespace python
128+
{
129+
template &lt;class Extractor, PyTypeObject const* python_type&gt;
130+
struct lvalue_from_pytype
131+
{
132+
lvalue_from_pytype();
133+
};
134+
}}
135+
</pre>
136+
137+
<h4><a name="lvalue_from_pytype-spec-ctors"></a>Class template <code>lvalue_from_pytype</code> constructor</h4>
138+
<pre>
139+
lvalue_from_pytype();
140+
</pre>
141+
142+
<dl class="function-semantics">
143+
144+
<dt><b>Effects:</b> Registers converters which can convert
145+
Python objects of the given type to lvalues of the type returned
146+
by <code>Extractor::execute</code>.
147+
148+
</dl>
149+
150+
<h3><a name="extract_identity-spec"></a>Class template <code>extract_identity</code></h3>
151+
152+
<p><code>extract_identity</code> is a model of <a
153+
href="Extractor.html#Extractor-concept">Extractor</a> which can be
154+
used in the common case where the C++ type to be extracted is the
155+
same as the Python object type.
156+
157+
<h4><a name="extract_identity-spec-synopsis"></a>Class template
158+
<code>extract_identity</code> synopsis</h4>
159+
<pre>
160+
namespace boost { namespace python
161+
{
162+
template &lt;class InstanceType&gt;
163+
struct extract_identity
164+
{
165+
static InstanceType&amp; execute(InstanceType&amp; c);
166+
};
167+
}}
168+
</pre>
169+
170+
<h4><a name="extract_identity-spec-statics"></a>Class template <code>extract_identity</code> static functions</h4>
171+
<pre>
172+
InstanceType&amp; execute(InstanceType&amp; c);
173+
</pre>
174+
175+
<dl class="function-semantics">
176+
177+
<dt><b>Returns:</b> <code>c</code>
178+
179+
</dl>
180+
181+
182+
<h3><a name="extract_member-spec"></a>Class template <code>extract_member</code></h3>
183+
184+
<p><code>extract_member</code> is a model of <a
185+
href="Extractor.html#Extractor-concept">Extractor</a> which can be
186+
used in the common case in the common case where the C++
187+
type to be extracted is a member of the Python object.
188+
189+
<h4><a name="extract_member-spec-synopsis"></a>Class template <code>extract_member</code> synopsis</h4>
190+
<pre>
191+
namespace boost { namespace python
192+
{
193+
template &lt;class InstanceType, class MemberType, MemberType (InstanceType::*member)&gt;
194+
struct extract_member
195+
{
196+
static MemberType&amp; execute(InstanceType&amp; c);
197+
};
198+
}}
199+
</pre>
200+
201+
<h4><a name="extract_member-spec-statics"></a>Class template <code>extract_member</code> static functions</h4>
202+
<pre>
203+
static MemberType&amp; execute(InstanceType&amp; c);
204+
</pre>
205+
206+
<dl class="function-semantics">
207+
208+
<dt><b>Returns:</b> <code>c.*member</code>
209+
210+
</dl>
211+
212+
<h2><a name="examples"></a>Example</h2>
213+
214+
This example presumes that someone has implemented the standard <a
215+
href="http://www.python.org/doc/2.2/ext/dnt-basics.html">noddy
216+
example module</a> from the Python documentation, and we want to build
217+
a module which manipulates <code>Noddy</code>s. Since
218+
<code>noddy_NoddyObject</code> is so simple that it carries no
219+
interesting information, the example is a bit contrived: it assumes
220+
you want to keep track of one particular object for some reason. This
221+
module would have to be dynamically linked to the module which defines
222+
<code>noddy_NoddyType</code>.
223+
224+
<h3>C++ module definition</h3>
225+
226+
<pre>
227+
#include &lt;boost/python/reference.hpp&gt;
228+
#include &lt;boost/python/module.hpp&gt;
229+
230+
// definition lifted from the Python docs
231+
typedef struct {
232+
PyObject_HEAD
233+
} noddy_NoddyObject;
234+
235+
using namespace boost::python;
236+
static reference&lt;PyObject&gt; cache;
237+
238+
bool is_cached(noddy_NoddyObject* x)
239+
{
240+
return x == cache.get();
241+
}
242+
243+
void set_cache(noddy_NoddyObject* x)
244+
{
245+
cache.reset((PyObject*)x, ref::increment_count);
246+
}
247+
248+
BOOST_PYTHON_MODULE_INIT(noddy_cache)
249+
{
250+
module noddy_cache(&quot;noddy_cache&quot;)
251+
.def(&quot;is_cached&quot;, is_cached)
252+
.def(&quot;set_cache&quot;, set_cache)
253+
;
254+
255+
// register Noddy lvalue converter
256+
lvalue_from_pytype&lt;extract_identity&lt;noddy_NoddyObject&gt;,&amp;noddy_NoddyType&gt;();
257+
}
258+
</pre>
259+
260+
<h3>Python code</h3>
261+
262+
<pre>
263+
&gt;&gt;&gt; import noddy
264+
&gt;&gt;&gt; n = noddy.new_noddy()
265+
&gt;&gt;&gt; import noddy_cache
266+
&gt;&gt;&gt; noddy_cache.is_cached(n)
267+
0
268+
&gt;&gt;&gt; noddy_cache.set_cache(n)
269+
&gt;&gt;&gt; noddy_cache.is_cached(n)
270+
1
271+
&gt;&gt;&gt; noddy_cache.is_cached(noddy.new_noddy())
272+
0
273+
</pre>
274+
275+
<p>Revised
276+
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
277+
05 November, 2001
278+
<!--webbot bot="Timestamp" endspan i-checksum="39359" -->
279+
280+
281+
<p><i>&copy; Copyright <a href="../../../../people/dave_abrahams.htm">Dave
282+
Abrahams</a> 2002. All Rights Reserved.</i>
283+

0 commit comments

Comments
 (0)