|
| 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 - <boost/python/lvalue_from_python.hpp></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 <boost/python/lvalue_from_pytype.hpp></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><boost/python/lvalue_from_pytype.hpp></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 | + "traditional" 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&</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 <class Extractor, PyTypeObject const* python_type> |
| 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 <class InstanceType> |
| 163 | + struct extract_identity |
| 164 | + { |
| 165 | + static InstanceType& execute(InstanceType& 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& execute(InstanceType& 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 <class InstanceType, class MemberType, MemberType (InstanceType::*member)> |
| 194 | + struct extract_member |
| 195 | + { |
| 196 | + static MemberType& execute(InstanceType& 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& execute(InstanceType& 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 <boost/python/reference.hpp> |
| 228 | +#include <boost/python/module.hpp> |
| 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<PyObject> 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("noddy_cache") |
| 251 | + .def("is_cached", is_cached) |
| 252 | + .def("set_cache", set_cache) |
| 253 | + ; |
| 254 | + |
| 255 | + // register Noddy lvalue converter |
| 256 | + lvalue_from_pytype<extract_identity<noddy_NoddyObject>,&noddy_NoddyType>(); |
| 257 | +} |
| 258 | +</pre> |
| 259 | + |
| 260 | +<h3>Python code</h3> |
| 261 | + |
| 262 | +<pre> |
| 263 | +>>> import noddy |
| 264 | +>>> n = noddy.new_noddy() |
| 265 | +>>> import noddy_cache |
| 266 | +>>> noddy_cache.is_cached(n) |
| 267 | +0 |
| 268 | +>>> noddy_cache.set_cache(n) |
| 269 | +>>> noddy_cache.is_cached(n) |
| 270 | +1 |
| 271 | +>>> 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>© Copyright <a href="../../../../people/dave_abrahams.htm">Dave |
| 282 | + Abrahams</a> 2002. All Rights Reserved.</i> |
| 283 | + |
0 commit comments