1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>boost/python/enum.hpp</title> 5<link rel="stylesheet" href="../../boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../index.html" title="Boost.Python Reference Manual"> 8<link rel="up" href="../high_level_components.html" title="Chapter 2. High Level Components"> 9<link rel="prev" href="boost_python_docstring_options_h.html" title="boost/python/docstring_options.hpp"> 10<link rel="next" href="boost_python_errors_hpp.html" title="boost/python/errors.hpp"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="../../images/boost.png"></td></tr></table> 14<hr> 15<div class="spirit-nav"> 16<a accesskey="p" href="boost_python_docstring_options_h.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../high_level_components.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_errors_hpp.html"><img src="../../images/next.png" alt="Next"></a> 17</div> 18<div class="section"> 19<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 20<a name="high_level_components.boost_python_enum_hpp"></a><a class="link" href="boost_python_enum_hpp.html" title="boost/python/enum.hpp">boost/python/enum.hpp</a> 21</h2></div></div></div> 22<div class="toc"><dl class="toc"> 23<dt><span class="section"><a href="boost_python_enum_hpp.html#high_level_components.boost_python_enum_hpp.introduction">Introduction</a></span></dt> 24<dt><span class="section"><a href="boost_python_enum_hpp.html#high_level_components.boost_python_enum_hpp.class_template_enum">Class 25 template <code class="computeroutput"><span class="identifier">enum_</span></code></a></span></dt> 26<dt><span class="section"><a href="boost_python_enum_hpp.html#high_level_components.boost_python_enum_hpp.class_template_enum_constructors">Class 27 template <code class="computeroutput"><span class="identifier">enum_</span></code> constructors</a></span></dt> 28<dt><span class="section"><a href="boost_python_enum_hpp.html#high_level_components.boost_python_enum_hpp.class_template_enum_modifier_fun">Class 29 template <code class="computeroutput"><span class="identifier">enum_</span></code> modifier functions</a></span></dt> 30<dt><span class="section"><a href="boost_python_enum_hpp.html#high_level_components.boost_python_enum_hpp.example">Example</a></span></dt> 31</dl></div> 32<div class="section"> 33<div class="titlepage"><div><div><h3 class="title"> 34<a name="high_level_components.boost_python_enum_hpp.introduction"></a><a class="link" href="boost_python_enum_hpp.html#high_level_components.boost_python_enum_hpp.introduction" title="Introduction">Introduction</a> 35</h3></div></div></div> 36<p> 37 <boost/python/enum.hpp> defines the interface through which users 38 expose their C++ enumeration types to Python. It declares the <code class="computeroutput"><span class="identifier">enum_</span></code> class template, which is parameterized 39 on the enumeration type being exposed. 40 </p> 41</div> 42<div class="section"> 43<div class="titlepage"><div><div><h3 class="title"> 44<a name="high_level_components.boost_python_enum_hpp.class_template_enum"></a><a class="link" href="boost_python_enum_hpp.html#high_level_components.boost_python_enum_hpp.class_template_enum" title="Class template enum_">Class 45 template <code class="computeroutput"><span class="identifier">enum_</span></code></a> 46</h3></div></div></div> 47<p> 48 Creates a Python class derived from Python's <code class="computeroutput"><span class="keyword">int</span></code> 49 type which is associated with the C++ type passed as its first parameter. 50 </p> 51<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">python</span> 52<span class="special">{</span> 53 <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> 54 <span class="keyword">class</span> <span class="identifier">enum_</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">object</span> 55 <span class="special">{</span> 56 <span class="identifier">enum_</span><span class="special">(</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">name</span><span class="special">,</span> <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">doc</span> <span class="special">=</span> <span class="number">0</span><span class="special">);</span> 57 <span class="identifier">enum_</span><span class="special"><</span><span class="identifier">T</span><span class="special">>&</span> <span class="identifier">value</span><span class="special">(</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">name</span><span class="special">,</span> <span class="identifier">T</span><span class="special">);</span> 58 <span class="identifier">enum_</span><span class="special"><</span><span class="identifier">T</span><span class="special">>&</span> <span class="identifier">export_values</span><span class="special">();</span> 59 <span class="special">};</span> 60<span class="special">}}</span> 61</pre> 62</div> 63<div class="section"> 64<div class="titlepage"><div><div><h3 class="title"> 65<a name="high_level_components.boost_python_enum_hpp.class_template_enum_constructors"></a><a class="link" href="boost_python_enum_hpp.html#high_level_components.boost_python_enum_hpp.class_template_enum_constructors" title="Class template enum_ constructors">Class 66 template <code class="computeroutput"><span class="identifier">enum_</span></code> constructors</a> 67</h3></div></div></div> 68<pre class="programlisting"><span class="identifier">enum_</span><span class="special">(</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">name</span><span class="special">,</span> <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">doc</span><span class="special">=</span><span class="number">0</span><span class="special">);</span></pre> 69<div class="variablelist"> 70<p class="title"><b></b></p> 71<dl class="variablelist"> 72<dt><span class="term">Requires</span></dt> 73<dd><p> 74 name is an <a class="link" href="../glossary.html#ntbs">ntbs</a> which conforms to Python's 75 <a href="http://www.python.org/doc/current/ref/identifiers.html" target="_top">identifier 76 naming rules</a>. 77 </p></dd> 78<dt><span class="term">Effects</span></dt> 79<dd><p> 80 Constructs an <code class="computeroutput"><span class="identifier">enum_</span></code> 81 object holding a Python extension type derived from <code class="computeroutput"><span class="keyword">int</span></code> which is named <code class="computeroutput"><span class="identifier">name</span></code>. 82 The named attribute of the <a class="link" href="boost_python_scope_hpp.html" title="boost/python/scope.hpp">current 83 scope</a> is bound to the new extension type. 84 </p></dd> 85</dl> 86</div> 87</div> 88<div class="section"> 89<div class="titlepage"><div><div><h3 class="title"> 90<a name="high_level_components.boost_python_enum_hpp.class_template_enum_modifier_fun"></a><a class="link" href="boost_python_enum_hpp.html#high_level_components.boost_python_enum_hpp.class_template_enum_modifier_fun" title="Class template enum_ modifier functions">Class 91 template <code class="computeroutput"><span class="identifier">enum_</span></code> modifier functions</a> 92</h3></div></div></div> 93<pre class="programlisting"><span class="identifier">enum_</span><span class="special"><</span><span class="identifier">T</span><span class="special">>&</span> <span class="identifier">value</span><span class="special">(</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">name</span><span class="special">,</span> <span class="identifier">T</span> <span class="identifier">x</span><span class="special">);</span></pre> 94<div class="variablelist"> 95<p class="title"><b></b></p> 96<dl class="variablelist"> 97<dt><span class="term">Requires</span></dt> 98<dd><p> 99 name is an <a class="link" href="../glossary.html#ntbs">ntbs</a> which conforms to Python's 100 <a href="http://www.python.org/doc/current/ref/identifiers.html" target="_top">identifier 101 naming rules</a>. 102 </p></dd> 103<dt><span class="term">Effects</span></dt> 104<dd><p> 105 adds an instance of the wrapped enumeration type with value x to 106 the type's dictionary as the named attribute. 107 </p></dd> 108<dt><span class="term">Returns</span></dt> 109<dd><p> 110 <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> 111 </p></dd> 112</dl> 113</div> 114<pre class="programlisting"><span class="identifier">enum_</span><span class="special"><</span><span class="identifier">T</span><span class="special">>&</span> <span class="identifier">export_values</span><span class="special">();</span></pre> 115<div class="variablelist"> 116<p class="title"><b></b></p> 117<dl class="variablelist"> 118<dt><span class="term">Effects</span></dt> 119<dd><p> 120 sets attributes in the <a class="link" href="boost_python_scope_hpp.html" title="boost/python/scope.hpp">current 121 scope</a> with the same names and values as all enumeration values 122 exposed so far by calling value(). 123 </p></dd> 124<dt><span class="term">Returns</span></dt> 125<dd><p> 126 <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> 127 </p></dd> 128</dl> 129</div> 130</div> 131<div class="section"> 132<div class="titlepage"><div><div><h3 class="title"> 133<a name="high_level_components.boost_python_enum_hpp.example"></a><a class="link" href="boost_python_enum_hpp.html#high_level_components.boost_python_enum_hpp.example" title="Example">Example</a> 134</h3></div></div></div> 135<p> 136 C++ module definition 137 </p> 138<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="keyword">enum</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 139<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">def</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 140<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">module</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 141 142<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">;</span> 143 144<span class="keyword">enum</span> <span class="identifier">color</span> <span class="special">{</span> <span class="identifier">red</span> <span class="special">=</span> <span class="number">1</span><span class="special">,</span> <span class="identifier">green</span> <span class="special">=</span> <span class="number">2</span><span class="special">,</span> <span class="identifier">blue</span> <span class="special">=</span> <span class="number">4</span> <span class="special">};</span> 145 146<span class="identifier">color</span> <span class="identifier">identity_</span><span class="special">(</span><span class="identifier">color</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">x</span><span class="special">;</span> <span class="special">}</span> 147 148<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">enums</span><span class="special">)</span> 149<span class="special">{</span> 150 <span class="identifier">enum_</span><span class="special"><</span><span class="identifier">color</span><span class="special">>(</span><span class="string">"color"</span><span class="special">)</span> 151 <span class="special">.</span><span class="identifier">value</span><span class="special">(</span><span class="string">"red"</span><span class="special">,</span> <span class="identifier">red</span><span class="special">)</span> 152 <span class="special">.</span><span class="identifier">value</span><span class="special">(</span><span class="string">"green"</span><span class="special">,</span> <span class="identifier">green</span><span class="special">)</span> 153 <span class="special">.</span><span class="identifier">export_values</span><span class="special">()</span> 154 <span class="special">.</span><span class="identifier">value</span><span class="special">(</span><span class="string">"blue"</span><span class="special">,</span> <span class="identifier">blue</span><span class="special">)</span> 155 <span class="special">;</span> 156 157 <span class="identifier">def</span><span class="special">(</span><span class="string">"identity"</span><span class="special">,</span> <span class="identifier">identity_</span><span class="special">);</span> 158<span class="special">}</span> 159</pre> 160<p> 161 Interactive Python: 162 </p> 163<pre class="programlisting"><span class="special">>>></span> <span class="identifier">from</span> <span class="identifier">enums</span> <span class="identifier">import</span> <span class="special">*</span> 164 165<span class="special">>>></span> <span class="identifier">identity</span><span class="special">(</span><span class="identifier">red</span><span class="special">)</span> 166<span class="identifier">enums</span><span class="special">.</span><span class="identifier">color</span><span class="special">.</span><span class="identifier">red</span> 167 168<span class="special">>>></span> <span class="identifier">identity</span><span class="special">(</span><span class="identifier">color</span><span class="special">.</span><span class="identifier">red</span><span class="special">)</span> 169<span class="identifier">enums</span><span class="special">.</span><span class="identifier">color</span><span class="special">.</span><span class="identifier">red</span> 170 171<span class="special">>>></span> <span class="identifier">identity</span><span class="special">(</span><span class="identifier">green</span><span class="special">)</span> 172<span class="identifier">enums</span><span class="special">.</span><span class="identifier">color</span><span class="special">.</span><span class="identifier">green</span> 173 174<span class="special">>>></span> <span class="identifier">identity</span><span class="special">(</span><span class="identifier">color</span><span class="special">.</span><span class="identifier">green</span><span class="special">)</span> 175<span class="identifier">enums</span><span class="special">.</span><span class="identifier">color</span><span class="special">.</span><span class="identifier">green</span> 176 177<span class="special">>>></span> <span class="identifier">identity</span><span class="special">(</span><span class="identifier">blue</span><span class="special">)</span> 178<span class="identifier">Traceback</span> <span class="special">(</span><span class="identifier">most</span> <span class="identifier">recent</span> <span class="identifier">call</span> <span class="identifier">last</span><span class="special">):</span> 179 <span class="identifier">File</span> <span class="string">"<stdin>"</span><span class="special">,</span> <span class="identifier">line</span> <span class="number">1</span><span class="special">,</span> <span class="identifier">in</span> <span class="special">?</span> 180<span class="identifier">NameError</span><span class="special">:</span> <span class="identifier">name</span> <span class="char">'blue'</span> <span class="identifier">is</span> <span class="keyword">not</span> <span class="identifier">defined</span> 181 182<span class="special">>>></span> <span class="identifier">identity</span><span class="special">(</span><span class="identifier">color</span><span class="special">.</span><span class="identifier">blue</span><span class="special">)</span> 183<span class="identifier">enums</span><span class="special">.</span><span class="identifier">color</span><span class="special">.</span><span class="identifier">blue</span> 184 185<span class="special">>>></span> <span class="identifier">identity</span><span class="special">(</span><span class="identifier">color</span><span class="special">(</span><span class="number">1</span><span class="special">))</span> 186<span class="identifier">enums</span><span class="special">.</span><span class="identifier">color</span><span class="special">.</span><span class="identifier">red</span> 187 188<span class="special">>>></span> <span class="identifier">identity</span><span class="special">(</span><span class="identifier">color</span><span class="special">(</span><span class="number">2</span><span class="special">))</span> 189<span class="identifier">enums</span><span class="special">.</span><span class="identifier">color</span><span class="special">.</span><span class="identifier">green</span> 190 191<span class="special">>>></span> <span class="identifier">identity</span><span class="special">(</span><span class="identifier">color</span><span class="special">(</span><span class="number">3</span><span class="special">))</span> 192<span class="identifier">enums</span><span class="special">.</span><span class="identifier">color</span><span class="special">(</span><span class="number">3</span><span class="special">)</span> 193 194<span class="special">>>></span> <span class="identifier">identity</span><span class="special">(</span><span class="identifier">color</span><span class="special">(</span><span class="number">4</span><span class="special">))</span> 195<span class="identifier">enums</span><span class="special">.</span><span class="identifier">color</span><span class="special">.</span><span class="identifier">blue</span> 196 197<span class="special">>>></span> <span class="identifier">identity</span><span class="special">(</span><span class="number">1</span><span class="special">)</span> 198<span class="identifier">Traceback</span> <span class="special">(</span><span class="identifier">most</span> <span class="identifier">recent</span> <span class="identifier">call</span> <span class="identifier">last</span><span class="special">):</span> 199 <span class="identifier">File</span> <span class="string">"<stdin>"</span><span class="special">,</span> <span class="identifier">line</span> <span class="number">1</span><span class="special">,</span> <span class="identifier">in</span> <span class="special">?</span> 200<span class="identifier">TypeError</span><span class="special">:</span> <span class="identifier">bad</span> <span class="identifier">argument</span> <span class="identifier">type</span> <span class="keyword">for</span> <span class="identifier">built</span><span class="special">-</span><span class="identifier">in</span> <span class="identifier">operation</span> 201</pre> 202</div> 203</div> 204<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 205<td align="left"></td> 206<td align="right"><div class="copyright-footer">Copyright © 2002-2005, 2015 David Abrahams, Stefan Seefeld<p> 207 Distributed under the Boost Software License, Version 1.0. (See accompanying 208 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a> 209 </p> 210</div></td> 211</tr></table> 212<hr> 213<div class="spirit-nav"> 214<a accesskey="p" href="boost_python_docstring_options_h.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../high_level_components.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_errors_hpp.html"><img src="../../images/next.png" alt="Next"></a> 215</div> 216</body> 217</html> 218