• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>boost/python/scope.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_operators_hpp.html" title="boost/python/operators.hpp">
10<link rel="next" href="boost_python_stl_iterator_hpp.html" title="boost/python/stl_iterator.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_operators_hpp.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_stl_iterator_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_scope_hpp"></a><a class="link" href="boost_python_scope_hpp.html" title="boost/python/scope.hpp">boost/python/scope.hpp</a>
21</h2></div></div></div>
22<div class="toc"><dl class="toc">
23<dt><span class="section"><a href="boost_python_scope_hpp.html#high_level_components.boost_python_scope_hpp.introduction">Introduction</a></span></dt>
24<dt><span class="section"><a href="boost_python_scope_hpp.html#high_level_components.boost_python_scope_hpp.class_scope">Class
25        <code class="computeroutput"><span class="identifier">scope</span></code></a></span></dt>
26<dt><span class="section"><a href="boost_python_scope_hpp.html#high_level_components.boost_python_scope_hpp.class_scope_constructors_and_des">Class
27        scope constructors and destructor</a></span></dt>
28<dt><span class="section"><a href="boost_python_scope_hpp.html#high_level_components.boost_python_scope_hpp.example">Example</a></span></dt>
29</dl></div>
30<div class="section">
31<div class="titlepage"><div><div><h3 class="title">
32<a name="high_level_components.boost_python_scope_hpp.introduction"></a><a class="link" href="boost_python_scope_hpp.html#high_level_components.boost_python_scope_hpp.introduction" title="Introduction">Introduction</a>
33</h3></div></div></div>
34<p>
35          Defines facilities for querying and controlling the Python scope (namespace)
36          which will contain new wrapped classes and functions.
37        </p>
38</div>
39<div class="section">
40<div class="titlepage"><div><div><h3 class="title">
41<a name="high_level_components.boost_python_scope_hpp.class_scope"></a><a class="link" href="boost_python_scope_hpp.html#high_level_components.boost_python_scope_hpp.class_scope" title="Class scope">Class
42        <code class="computeroutput"><span class="identifier">scope</span></code></a>
43</h3></div></div></div>
44<p>
45          The scope class has an associated global Python object which controls the
46          Python namespace in which new extension classes and wrapped functions will
47          be defined as attributes. Default-constructing a new scope object binds
48          it to the associated global Python object. Constructing a scope object
49          with an argument changes the associated global Python object to the one
50          held by the argument, until the lifetime of the scope object ends, at which
51          time the associated global Python object reverts to what it was before
52          the scope object was constructed.
53        </p>
54<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>
55<span class="special">{</span>
56  <span class="keyword">class</span> <span class="identifier">scope</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">object</span>
57  <span class="special">{</span>
58   <span class="keyword">public</span><span class="special">:</span>
59      <span class="identifier">scope</span><span class="special">(</span><span class="identifier">scope</span> <span class="keyword">const</span><span class="special">&amp;);</span>
60      <span class="identifier">scope</span><span class="special">(</span><span class="identifier">object</span> <span class="keyword">const</span><span class="special">&amp;);</span>
61      <span class="identifier">scope</span><span class="special">();</span>
62      <span class="special">~</span><span class="identifier">scope</span><span class="special">()</span>
63   <span class="keyword">private</span><span class="special">:</span>
64      <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">=(</span><span class="identifier">scope</span> <span class="keyword">const</span><span class="special">&amp;);</span>
65  <span class="special">};</span>
66<span class="special">}}</span>
67</pre>
68</div>
69<div class="section">
70<div class="titlepage"><div><div><h3 class="title">
71<a name="high_level_components.boost_python_scope_hpp.class_scope_constructors_and_des"></a><a class="link" href="boost_python_scope_hpp.html#high_level_components.boost_python_scope_hpp.class_scope_constructors_and_des" title="Class scope constructors and destructor">Class
72        scope constructors and destructor</a>
73</h3></div></div></div>
74<pre class="programlisting"><span class="keyword">explicit</span> <span class="identifier">scope</span><span class="special">(</span><span class="identifier">scope</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">x</span><span class="special">);</span>
75<span class="keyword">explicit</span> <span class="identifier">scope</span><span class="special">(</span><span class="identifier">object</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">x</span><span class="special">);</span>
76</pre>
77<p>
78          Stores a reference to the current associated scope object, and sets the
79          associated scope object to the one referred to by x.ptr(). The object base
80          class is initialized with x.
81        </p>
82<pre class="programlisting"><span class="identifier">scope</span><span class="special">();</span></pre>
83<p>
84          Stores a reference to the current associated scope object. The object base
85          class is initialized with the current associated scope object. Outside
86          any module initialization function, the current associated Python object
87          is None.
88        </p>
89<pre class="programlisting"><span class="special">~</span><span class="identifier">scope</span><span class="special">()</span></pre>
90<p>
91          Sets the current associated Python object to the stored object.
92        </p>
93</div>
94<div class="section">
95<div class="titlepage"><div><div><h3 class="title">
96<a name="high_level_components.boost_python_scope_hpp.example"></a><a class="link" href="boost_python_scope_hpp.html#high_level_components.boost_python_scope_hpp.example" title="Example">Example</a>
97</h3></div></div></div>
98<p>
99          The following example shows how scope setting can be used to define nested
100          classes.
101        </p>
102<p>
103          C++ Module definition:
104        </p>
105<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</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">&gt;</span>
106<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="keyword">class</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
107<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">scope</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
108<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>
109
110<span class="keyword">struct</span> <span class="identifier">X</span>
111<span class="special">{</span>
112  <span class="keyword">void</span> <span class="identifier">f</span><span class="special">()</span> <span class="special">{}</span>
113
114  <span class="keyword">struct</span> <span class="identifier">Y</span> <span class="special">{</span> <span class="keyword">int</span> <span class="identifier">g</span><span class="special">()</span> <span class="special">{</span> <span class="keyword">return</span> <span class="number">42</span><span class="special">;</span> <span class="special">}</span> <span class="special">};</span>
115<span class="special">};</span>
116
117<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">nested</span><span class="special">)</span>
118<span class="special">{</span>
119   <span class="comment">// add some constants to the current (module) scope</span>
120   <span class="identifier">scope</span><span class="special">().</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"yes"</span><span class="special">)</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span>
121   <span class="identifier">scope</span><span class="special">().</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"no"</span><span class="special">)</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
122
123   <span class="comment">// Change the current scope </span>
124   <span class="identifier">scope</span> <span class="identifier">outer</span>
125       <span class="special">=</span> <span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">X</span><span class="special">&gt;(</span><span class="string">"X"</span><span class="special">)</span>
126            <span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"f"</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">X</span><span class="special">::</span><span class="identifier">f</span><span class="special">)</span>
127            <span class="special">;</span>
128
129   <span class="comment">// Define a class Y in the current scope, X</span>
130   <span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">X</span><span class="special">::</span><span class="identifier">Y</span><span class="special">&gt;(</span><span class="string">"Y"</span><span class="special">)</span>
131      <span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"g"</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">X</span><span class="special">::</span><span class="identifier">Y</span><span class="special">::</span><span class="identifier">g</span><span class="special">)</span>
132      <span class="special">;</span>
133<span class="special">}</span>
134</pre>
135<p>
136          Interactive Python:
137        </p>
138<pre class="programlisting"><span class="special">&gt;&gt;&gt;</span> <span class="identifier">import</span> <span class="identifier">nested</span>
139<span class="special">&gt;&gt;&gt;</span> <span class="identifier">nested</span><span class="special">.</span><span class="identifier">yes</span>
140<span class="number">1</span>
141<span class="special">&gt;&gt;&gt;</span> <span class="identifier">y</span> <span class="special">=</span> <span class="identifier">nested</span><span class="special">.</span><span class="identifier">X</span><span class="special">.</span><span class="identifier">Y</span><span class="special">()</span>
142<span class="special">&gt;&gt;&gt;</span> <span class="identifier">y</span><span class="special">.</span><span class="identifier">g</span><span class="special">()</span>
143<span class="number">42</span>
144</pre>
145</div>
146</div>
147<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
148<td align="left"></td>
149<td align="right"><div class="copyright-footer">Copyright © 2002-2005, 2015 David Abrahams, Stefan Seefeld<p>
150        Distributed under the Boost Software License, Version 1.0. (See accompanying
151        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>
152      </p>
153</div></td>
154</tr></table>
155<hr>
156<div class="spirit-nav">
157<a accesskey="p" href="boost_python_operators_hpp.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_stl_iterator_hpp.html"><img src="../../images/next.png" alt="Next"></a>
158</div>
159</body>
160</html>
161