File: to_python_converter.html

package info (click to toggle)
boost 1.33.1-10
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 100,948 kB
  • ctags: 145,103
  • sloc: cpp: 573,492; xml: 49,055; python: 15,626; ansic: 13,588; sh: 2,099; yacc: 858; makefile: 660; perl: 427; lex: 111; csh: 6
file content (203 lines) | stat: -rw-r--r-- 5,878 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
  <head>
    <meta name="generator" content=
    "HTML Tidy for Windows (vers 1st August 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 -
    &lt;boost/python/to_python_converter.hpp&gt;</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
          &lt;boost/python/to_python_converter.hpp&gt;</h2>
        </td>
      </tr>
    </table>
    <hr>

    <h2>Contents</h2>

    <dl class="page-index">
      <dt><a href="#introduction">Introduction</a></dt>

      <dt><a href="#classes">Classes</a></dt>

      <dd>
        <dl class="page-index">
          <dt><a href="#to_python_converter-spec">Class Template
          <code>to_python_converter</code></a></dt>

          <dd>
            <dl class="page-index">
              <dt><a href="#to_python_converter-spec-synopsis">Class Template
              <code>to_python_converter</code> synopsis</a></dt>

              <dt><a href="#to_python_converter-spec-ctors">Class Template
              <code>to_python_converter</code> constructor</a></dt>
            </dl>
          </dd>
        </dl>
      </dd>

      <dt><a href="#examples">Example</a></dt>
    </dl>
    <hr>

    <h2><a name="introduction"></a>Introduction</h2>
    <code>to_python_converter</code> registers a conversion from objects of a
    given C++ type into a Python object. 

    <h2><a name="classes"></a>Classes</h2>

    <h3><a name="to_python_converter-spec"></a>Class template
    <code>to_python_converter</code></h3>
    <code>to_python_converter</code> adds a wrapper around a static member
    function of its second template parameter, handling low-level details
    such as insertion into the converter registry. 

    <table border="1" summary="to_python_converter template parameters">
      <caption>
        <b><code>to_python_converter</code> template parameters</b><br>
        In the table below, <b><code>x</code></b> denotes an object of type
        <code>T</code>
      </caption>

      <tr>
        <th>Parameter</th>

        <th>Requirements</th>

        <th>Description</th>
      </tr>

      <tr>
        <td><code>T</code></td>

        <td>
        </td>

        <td>The C++ type of the source object in the conversion</td>
      </tr>

      <tr>
        <td><code>Conversion</code></td>

        <td>
        <code>PyObject*&nbsp;p&nbsp;=&nbsp;Conversion::convert(x)</code>,<br>
         if <code>p&nbsp;==&nbsp;0</code>, <code><a href=
        "http://www.python.org/doc/2.2/api/exceptionHandling.html#l2h-71">PyErr_Occurred</a>()&nbsp;!=&nbsp;0</code>.</td>

        <td>A class type whose static member function <code>convert</code>
        does the real work of the conversion.</td>
      </tr>
    </table>

    <h4><a name="to_python_converter-spec-synopsis"></a>Class template
    <code>to_python_converter</code> synopsis</h4>
<pre>
namespace boost { namespace python
{
  template &lt;class T, class Conversion&gt;
  struct to_python_converter
  {
      to_python_converter();
  };
}}
</pre>

    <h4><a name="to_python_converter-spec-ctors"></a>Class template
    <code>to_python_converter</code> constructor</h4>
<pre>
to_python_converter();
</pre>

    <dl class="function-semantics">
      <dt><b>Effects:</b> Registers a to_python converter which uses
      <code>Conversion::convert()</code> to do its work.</dt>
    </dl>

    <h2><a name="examples"></a>Example</h2>
    This example presumes that someone has implemented the standard <a href=
    "http://www.python.org/doc/2.2/ext/dnt-basics.html">noddy example
    module</a> from the Python documentation, and placed the corresponding
    declarations in <code>"noddy.h"</code>. Because
    <code>noddy_NoddyObject</code> is the ultimate trivial extension type,
    the example is a bit contrived: it wraps a function for which all
    information is contained in the <i>type</i> of its return value. 

    <h3>C++ module definition</h3>
<pre>
#include &lt;boost/python/reference.hpp&gt;
#include &lt;boost/python/module.hpp&gt;
#include "noddy.h"

struct tag {};
tag make_tag() { return tag(); }

using namespace boost::python;

struct tag_to_noddy
{
    static PyObject* convert(tag const&amp; x)
    {
        return PyObject_New(noddy_NoddyObject, &amp;noddy_NoddyType);
    }
};

BOOST_PYTHON_MODULE(to_python_converter)
{
    def("make_tag", make_tag);
    to_python_converter&lt;tag, tag_to_noddy&gt;();
}
</pre>

    <h3>Python code</h3>
<pre>
&gt;&gt;&gt; import to_python_converter
&gt;&gt;&gt; def always_none():
...     return None
...
&gt;&gt;&gt; def choose_function(x):
...     if (x % 2 != 0):
...         return to_python_converter.make_tag
...     else:
...         return always_none
...
&gt;&gt;&gt; a = [ choose_function(x) for x in range(5) ]
&gt;&gt;&gt; b = [ f() for f in a ]
&gt;&gt;&gt; type(b[0])
&lt;type 'NoneType'&gt;
&gt;&gt;&gt; type(b[1])
&lt;type 'Noddy'&gt;
&gt;&gt;&gt; type(b[2])
&lt;type 'NoneType'&gt;
&gt;&gt;&gt; type(b[3])
&lt;type 'Noddy'&gt;
</pre>

    <p>Revised 
    <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
  13 November, 2002
  <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
    </p>

    <p><i>&copy; Copyright <a href=
    "../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
  </body>
</html>