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 - <boost/python/enum.hpp></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 <boost/python/enum.hpp></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><boost/python/enum.hpp></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_<T></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> |
---|
87 | namespace boost { namespace python |
---|
88 | { |
---|
89 | template <class T> |
---|
90 | class enum_ : public <a href="object.html#object-spec">object</a> |
---|
91 | { |
---|
92 | enum_(char const* name); |
---|
93 | enum_<T>& value(char const* name, T); |
---|
94 | enum_<T>& 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> |
---|
102 | enum_(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> |
---|
122 | inline enum_<T>& 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> |
---|
141 | inline enum_<T>& 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 <boost/python/enum.hpp> |
---|
160 | #include <boost/python/def.hpp> |
---|
161 | #include <boost/python/module.hpp> |
---|
162 | |
---|
163 | using namespace boost::python; |
---|
164 | |
---|
165 | enum color { red = 1, green = 2, blue = 4 }; |
---|
166 | |
---|
167 | color identity_(color x) { return x; } |
---|
168 | |
---|
169 | BOOST_PYTHON_MODULE(enums) |
---|
170 | { |
---|
171 | enum_<color>("color") |
---|
172 | .value("red", red) |
---|
173 | .value("green", green) |
---|
174 | .export_values() |
---|
175 | .value("blue", blue) |
---|
176 | ; |
---|
177 | |
---|
178 | def("identity", identity_); |
---|
179 | } |
---|
180 | </pre> |
---|
181 | <p>Interactive Python: |
---|
182 | <pre> |
---|
183 | >>> from enums import * |
---|
184 | |
---|
185 | >>> identity(red) |
---|
186 | enums.color.red |
---|
187 | |
---|
188 | >>> identity(color.red) |
---|
189 | enums.color.red |
---|
190 | |
---|
191 | >>> identity(green) |
---|
192 | enums.color.green |
---|
193 | |
---|
194 | >>> identity(color.green) |
---|
195 | enums.color.green |
---|
196 | |
---|
197 | >>> identity(blue) |
---|
198 | Traceback (most recent call last): |
---|
199 | File "<stdin>", line 1, in ? |
---|
200 | NameError: name blue' is not defined |
---|
201 | |
---|
202 | >>> identity(color.blue) |
---|
203 | enums.color.blue |
---|
204 | |
---|
205 | >>> identity(color(1)) |
---|
206 | enums.color.red |
---|
207 | |
---|
208 | >>> identity(color(2)) |
---|
209 | enums.color.green |
---|
210 | |
---|
211 | >>> identity(color(3)) |
---|
212 | enums.color(3) |
---|
213 | |
---|
214 | >>> identity(color(4)) |
---|
215 | enums.color.blue |
---|
216 | |
---|
217 | >>> identity(1) |
---|
218 | Traceback (most recent call last): |
---|
219 | File "<stdin>", line 1, in ? |
---|
220 | TypeError: 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>© Copyright <a href= |
---|
231 | "../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> |
---|
232 | </body> |
---|
233 | </html> |
---|
234 | |
---|