Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/python/doc/v2/enum.html @ 45

Last change on this file since 45 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 6.4 KB
Line 
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2
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) -->
6<html>
7  <head>
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">
12
13    <title>Boost.Python - &lt;boost/python/enum.hpp&gt;</title>
14  </head>
15
16  <body link="#0000ff" vlink="#800080">
17    <table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
18    "header">
19      <tr>
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>
23        </td>
24
25        <td valign="top">
26          <h1 align="center"><a href="../index.html">Boost.Python</a></h1>
27
28          <h2 align="center">Header &lt;boost/python/enum.hpp&gt;</h2>
29        </td>
30      </tr>
31    </table>
32    <hr>
33
34    <h2>Contents</h2>
35
36    <dl class="page-index">
37      <dt><a href="#introduction">Introduction</a></dt>
38
39      <dt><a href="#classes">Classes</a></dt>
40
41      <dd>
42        <dl class="page-index">
43          <dt><a href="#enum_-spec">Class template
44          <code>enum_</code></a></dt>
45
46          <dd>
47            <dl class="page-index">
48              <dt><a href="#enum_-spec-synopsis">Class template <code>enum_</code>
49              synopsis</a></dt>
50
51              <dt><a href="#enum_-spec-ctors">Class template <code>enum_</code>
52              constructors</a></dt>
53
54              <dt><a href="#enum_-spec-modifiers">Class template <code>enum_</code>
55              modifier functions</a></dt>
56            </dl>
57          </dd>
58
59        </dl>
60      </dd>
61
62      <dt><a href="#examples">Example(s)</a></dt>
63    </dl>
64    <hr>
65
66    <h2><a name="introduction"></a>Introduction</h2>
67
68    <p><code>&lt;boost/python/enum.hpp&gt;</code> defines the
69    interface through which users expose their C++ enumeration types
70    to Python. It declares the
71    <code>enum_</code> class template, which is parameterized on the
72    enumeration type being exposed. </p>
73
74
75    <h2><a name="classes"></a>Classes</h2>
76
77    <h3><a name="enum_-spec"></a>Class template
78    <code>enum_&lt;T&gt;</code></h3>
79
80    <p>Creates a Python class derived from Python's <code>int</code>
81    type which is associated with the C++ type passed as its first
82    parameter.
83
84    <h4><a name="enum_-spec-synopsis"></a>Class template <code>enum_</code>
85    synopsis</h4>
86<pre>
87namespace boost { namespace python
88{
89  template &lt;class T&gt;
90  class enum_ : public <a href="object.html#object-spec">object</a>
91  {
92    enum_(char const* name);
93    enum_&lt;T&gt;&amp; value(char const* name, T);
94    enum_&lt;T&gt;&amp; export_values();
95  };
96}}
97</pre>
98
99    <h4><a name="enum_-spec-ctors"></a>Class template <code>enum_</code>
100    constructors</h4>
101<pre>
102enum_(char const* name);
103</pre>
104
105    <dl class="function-semantics">
106      <dt><b>Requires:</b> <code>name</code> is an <a href=
107      "definitions.html#ntbs">ntbs</a> which conforms to Python's <a href=
108      "http://www.python.org/doc/current/ref/identifiers.html">identifier
109      naming rules</a>.
110
111      <dt><b>Effects:</b> Constructs an <code>enum_</code> object
112      holding a Python extension type derived from <code>int</code>
113      which is named <code>name</code>. The
114      <code>name</code>d attribute of the <a href=
115      "scope.html#introduction">current scope</a> is bound to the new
116      extension type.</dt>
117    </dl>
118
119    <h4><a name="enum_-spec-modifiers"></a>Class template
120    <code>enum_</code> modifier functions</h4>
121<pre>
122inline enum_&lt;T&gt;&amp; value(char const* name, T x);
123</pre>
124
125    <dl class="function-semantics">
126      <dt><b>Requires:</b> <code>name</code> is an <a href=
127      "definitions.html#ntbs">ntbs</a> which conforms to Python's <a
128      href=
129      "http://www.python.org/doc/current/ref/identifiers.html">identifier
130      naming rules</a>.
131
132      <dt><b>Effects:</b> adds an instance of the wrapped enumeration
133      type with value <code>x</code> to the type's dictionary as the
134      <code>name</code>d attribute</dt>.
135
136      <dt><b>Returns:</b> <code>*this</code></dt>
137
138    </dl>
139
140<pre>
141inline enum_&lt;T&gt;&amp; export_values();
142</pre>
143
144    <dl class="function-semantics">
145
146      <dt><b>Effects:</b> sets attributes in the current <a
147        href="scope.html#scope-spec"><code>scope</code></a> with the
148        same names and values as all enumeration values exposed so far
149        by calling <code>value()</code></dt>.
150
151      <dt><b>Returns:</b> <code>*this</code></dt>
152
153    </dl>
154
155    <h2><a name="examples"></a>Example(s)</h2>
156
157    <p>C++ module definition
158<pre>
159#include &lt;boost/python/enum.hpp&gt;
160#include &lt;boost/python/def.hpp&gt;
161#include &lt;boost/python/module.hpp&gt;
162
163using namespace boost::python;
164
165enum color { red = 1, green = 2, blue = 4 };
166
167color identity_(color x) { return x; }
168
169BOOST_PYTHON_MODULE(enums)
170{
171    enum_&lt;color&gt;(&quot;color&quot;)
172        .value(&quot;red&quot;, red)
173        .value(&quot;green&quot;, green)
174        .export_values()
175        .value(&quot;blue&quot;, blue)
176        ;
177   
178    def(&quot;identity&quot;, identity_);
179}
180</pre>
181    <p>Interactive Python:
182<pre>
183&gt;&gt;&gt; from enums import *
184
185&gt;&gt;&gt; identity(red)
186enums.color.red
187
188&gt;&gt;&gt; identity(color.red)
189enums.color.red
190
191&gt;&gt;&gt; identity(green)
192enums.color.green
193
194&gt;&gt;&gt; identity(color.green)
195enums.color.green
196
197&gt;&gt;&gt; identity(blue)
198Traceback (most recent call last):
199  File &quot;&lt;stdin&gt;&quot;, line 1, in ?
200NameError: name blue' is not defined
201
202&gt;&gt;&gt; identity(color.blue)
203enums.color.blue
204
205&gt;&gt;&gt; identity(color(1))
206enums.color.red
207
208&gt;&gt;&gt; identity(color(2))
209enums.color.green
210
211&gt;&gt;&gt; identity(color(3))
212enums.color(3)
213
214&gt;&gt;&gt; identity(color(4))
215enums.color.blue
216
217&gt;&gt;&gt; identity(1)
218Traceback (most recent call last):
219  File &quot;&lt;stdin&gt;&quot;, line 1, in ?
220TypeError: bad argument type for built-in operation
221</pre>
222    <hr>
223
224    Revised
225    <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
226  13 December, 2002
227  <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
228     
229
230    <p><i>&copy; Copyright <a href=
231    "../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
232  </body>
233</html>
234
Note: See TracBrowser for help on using the repository browser.