1 <!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN">
3 <!-- Copyright David Abrahams 2006. Distributed under the Boost -->
4 <!-- Software License, Version 1.0. (See accompanying -->
5 <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
8 <meta name=
"generator" content=
9 "HTML Tidy for Windows (vers 1st August 2002), see www.w3.org">
10 <meta http-equiv=
"Content-Type" content=
"text/html; charset=iso-8859-1">
11 <link rel=
"stylesheet" type=
"text/css" href=
"../boost.css">
13 <title>Boost.Python -
<boost/python/lvalue_from_python.hpp
></title>
17 <table border=
"0" cellpadding=
"7" cellspacing=
"0" width=
"100%" summary=
20 <td valign=
"top" width=
"300">
21 <h3><a href=
"../../../../index.htm"><img height=
"86" width=
"277"
22 alt=
"C++ Boost" src=
"../../../../boost.png" border=
"0"></a></h3>
26 <h1 align=
"center"><a href=
"../index.html">Boost.Python
</a></h1>
28 <h2 align=
"center">Header
29 <boost/python/lvalue_from_pytype.hpp
></h2>
37 <dl class=
"page-index">
38 <dt><a href=
"#introduction">Introduction
</a></dt>
40 <dt><a href=
"#classes">Classes
</a></dt>
43 <dl class=
"page-index">
44 <dt><a href=
"#lvalue_from_pytype-spec">Class Template
45 <code>lvalue_from_pytype
</code></a></dt>
48 <dl class=
"page-index">
49 <dt><a href=
"#lvalue_from_pytype-spec-synopsis">Class Template
50 <code>lvalue_from_pytype
</code> synopsis
</a></dt>
52 <dt><a href=
"#lvalue_from_pytype-spec-ctors">Class Template
53 <code>lvalue_from_pytype
</code> constructor
</a></dt>
58 <dl class=
"page-index">
59 <dt><a href=
"#extract_identity-spec">Class Template
60 <code>extract_identity
</code></a></dt>
63 <dl class=
"page-index">
64 <dt><a href=
"#extract_identity-spec-synopsis">Class Template
65 <code>extract_identity
</code> synopsis
</a></dt>
67 <dt><a href=
"#extract_identity-spec-statics">Class Template
68 <code>extract_identity
</code> static functions
</a></dt>
72 <dt><a href=
"#extract_member-spec">Class Template
73 <code>extract_member
</code></a></dt>
76 <dl class=
"page-index">
77 <dt><a href=
"#extract_member-spec-synopsis">Class Template
78 <code>extract_member
</code> synopsis
</a></dt>
80 <dt><a href=
"#extract_member-spec-statics">Class Template
81 <code>extract_member
</code> static functions
</a></dt>
87 <dt><a href=
"#examples">Example
</a></dt>
91 <h2><a name=
"introduction"></a>Introduction
</h2>
92 <code><boost/python/lvalue_from_pytype.hpp
></code> supplies a
93 facility for extracting C++ objects from within Python instances of a
94 given type. This is typically useful for dealing with
"traditional"
95 Python extension types.
97 <h2><a name=
"classes"></a>Classes
</h2>
99 <h3><a name=
"lvalue_from_pytype-spec"></a>Class template
100 <code>lvalue_from_pytype
</code></h3>
102 <p>Class template
<code>lvalue_from_pytype
</code> will register
103 from_python converters which, given an object of the given Python type,
104 can extract references and pointers to a particular C++ type. Its
105 template arguments are:
</p>
107 <table border=
"1" summary=
"lvalue_from_pytype template parameters">
109 <b><code>lvalue_from_pytype
</code> Requirements
</b><br>
110 In the table below,
<b><code>x
</code></b> denotes an object of type
111 <code>PythonObject
&</code>
117 <th>Requirements
</th>
123 <td><code>Extractor
</code></td>
125 <td>a model of
<a href=
126 "Extractor.html#Extractor-concept">Extractor
</a> whose execute
127 function returns a reference type.
</td>
129 <td>Extracts the lvalue from the Python object once its type has been
134 <td><code>python_type
</code></td>
136 <td>A compile-time constant
<code><a href=
137 "http://www.python.org/doc/2.2/ext/dnt-type-methods.html">PyTypeObject
</a>*
</code></td>
139 <td>The Python type of instances convertible by this converter.
140 Python subtypes are also convertible.
</td>
144 <h4><a name=
"lvalue_from_pytype-spec-synopsis"></a>Class template
145 <code>lvalue_from_pytype
</code> synopsis
</h4>
147 namespace boost { namespace python
149 template
<class Extractor, PyTypeObject const* python_type
>
150 struct lvalue_from_pytype
152 lvalue_from_pytype();
157 <h4><a name=
"lvalue_from_pytype-spec-ctors"></a>Class template
158 <code>lvalue_from_pytype
</code> constructor
</h4>
160 lvalue_from_pytype();
163 <dl class=
"function-semantics">
164 <dt><b>Effects:
</b> Registers converters which can convert Python
165 objects of the given type to lvalues of the type returned by
166 <code>Extractor::execute
</code>.
</dt>
169 <h3><a name=
"extract_identity-spec"></a>Class template
170 <code>extract_identity
</code></h3>
172 <p><code>extract_identity
</code> is a model of
<a href=
173 "Extractor.html#Extractor-concept">Extractor
</a> which can be used in the
174 common case where the C++ type to be extracted is the same as the Python
177 <h4><a name=
"extract_identity-spec-synopsis"></a>Class template
178 <code>extract_identity
</code> synopsis
</h4>
180 namespace boost { namespace python
182 template
<class InstanceType
>
183 struct extract_identity
185 static InstanceType
& execute(InstanceType
& c);
190 <h4><a name=
"extract_identity-spec-statics"></a>Class template
191 <code>extract_identity
</code> static functions
</h4>
193 InstanceType
& execute(InstanceType
& c);
196 <dl class=
"function-semantics">
197 <dt><b>Returns:
</b> <code>c
</code></dt>
200 <h3><a name=
"extract_member-spec"></a>Class template
201 <code>extract_member
</code></h3>
203 <p><code>extract_member
</code> is a model of
<a href=
204 "Extractor.html#Extractor-concept">Extractor
</a> which can be used in the
205 common case in the common case where the C++ type to be extracted is a
206 member of the Python object.
</p>
208 <h4><a name=
"extract_member-spec-synopsis"></a>Class template
209 <code>extract_member
</code> synopsis
</h4>
211 namespace boost { namespace python
213 template
<class InstanceType, class MemberType, MemberType (InstanceType::*member)
>
214 struct extract_member
216 static MemberType
& execute(InstanceType
& c);
221 <h4><a name=
"extract_member-spec-statics"></a>Class template
222 <code>extract_member
</code> static functions
</h4>
224 static MemberType
& execute(InstanceType
& c);
227 <dl class=
"function-semantics">
228 <dt><b>Returns:
</b> <code>c.*member
</code></dt>
231 <h2><a name=
"examples"></a>Example
</h2>
232 This example presumes that someone has implemented the standard
<a href=
233 "http://www.python.org/doc/2.2/ext/dnt-basics.html">noddy example
234 module
</a> from the Python documentation, and we want to build a module
235 which manipulates
<code>Noddy
</code>s. Since
236 <code>noddy_NoddyObject
</code> is so simple that it carries no
237 interesting information, the example is a bit contrived: it assumes you
238 want to keep track of one particular object for some reason. This module
239 would have to be dynamically linked to the module which defines
240 <code>noddy_NoddyType
</code>.
242 <h3>C++ module definition
</h3>
244 #include
<boost/python/module.hpp
>
245 #include
<boost/python/handle.hpp
>
246 #include
<boost/python/borrowed.hpp
>
247 #include
<boost/python/lvalue_from_pytype.hpp
>
249 // definition lifted from the Python docs
254 using namespace boost::python;
255 static handle
<noddy_NoddyObject
> cache;
257 bool is_cached(noddy_NoddyObject* x)
259 return x == cache.get();
262 void set_cache(noddy_NoddyObject* x)
264 cache = handle
<noddy_NoddyObject
>(borrowed(x));
267 BOOST_PYTHON_MODULE(noddy_cache)
269 def(
"is_cached", is_cached);
270 def(
"set_cache", set_cache);
272 // register Noddy lvalue converter
273 lvalue_from_pytype
<extract_identity
<noddy_NoddyObject
>,
&noddy_NoddyType
>();
279 >>> import noddy
280 >>> n = noddy.new_noddy()
281 >>> import noddy_cache
282 >>> noddy_cache.is_cached(n)
284 >>> noddy_cache.set_cache(n)
285 >>> noddy_cache.is_cached(n)
287 >>> noddy_cache.is_cached(noddy.new_noddy())
292 <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
294 <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
297 <p><i>© Copyright
<a href=
298 "../../../../people/dave_abrahams.htm">Dave Abrahams
</a> 2002.
</i></p>