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/scope.hpp></title> |
---|
14 | </head> |
---|
15 | |
---|
16 | <body> |
---|
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/scope.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="#scope-spec">Class <code>scope</code></a></dt> |
---|
44 | |
---|
45 | <dd> |
---|
46 | <dl class="page-index"> |
---|
47 | <dt><a href="#scope-spec-synopsis">Class <code>scope</code> |
---|
48 | synopsis</a></dt> |
---|
49 | |
---|
50 | <dt><a href="#scope-spec-ctors">Class <code>scope</code> |
---|
51 | constructors and destructor</a></dt> |
---|
52 | </dl> |
---|
53 | </dd> |
---|
54 | </dl> |
---|
55 | </dd> |
---|
56 | |
---|
57 | <dt><a href="#examples">Example</a></dt> |
---|
58 | </dl> |
---|
59 | <hr> |
---|
60 | |
---|
61 | <h2><a name="introduction"></a>Introduction</h2> |
---|
62 | |
---|
63 | <p>Defines facilities for querying and controlling the Python scope |
---|
64 | (namespace) which will contain new wrapped classes and functions.</p> |
---|
65 | |
---|
66 | <h2><a name="classes"></a>Classes</h2> |
---|
67 | |
---|
68 | <h3><a name="scope-spec"></a>Class <code>scope</code></h3> |
---|
69 | |
---|
70 | <p>The <code>scope</code> class has an associated global Python |
---|
71 | object which controls the Python namespace in which new extension |
---|
72 | classes and wrapped functions will be defined as |
---|
73 | attributes. Default-constructing a new <code>scope</code> object |
---|
74 | binds it to the associated global Python object. Constructing a |
---|
75 | <code>scope</code> object with an argument changes the associated |
---|
76 | global Python object to the one held by the argument, until the |
---|
77 | lifetime of the <code>scope</code> object ends, at which time the |
---|
78 | associated global Python object reverts to what it was before the |
---|
79 | <code>scope</code> object was constructed.</p> |
---|
80 | |
---|
81 | <h4><a name="scope-spec-synopsis"></a>Class <code>scope</code> |
---|
82 | synopsis</h4> |
---|
83 | <pre> |
---|
84 | namespace boost { namespace python |
---|
85 | { |
---|
86 | class scope : public <a href= |
---|
87 | "object.html#object-spec">object</a> |
---|
88 | { |
---|
89 | public: |
---|
90 | scope(scope const&); |
---|
91 | scope(object const&); |
---|
92 | scope(); |
---|
93 | ~scope() |
---|
94 | private: |
---|
95 | void operator=(scope const&); |
---|
96 | }; |
---|
97 | }} |
---|
98 | </pre> |
---|
99 | |
---|
100 | <h4><a name="scope-spec-ctors"></a>Class <code>scope</code> constructors |
---|
101 | and destructor</h4> |
---|
102 | <pre> |
---|
103 | explicit scope(scope const& x); |
---|
104 | explicit scope(object const& x); |
---|
105 | </pre> |
---|
106 | Stores a reference to the current associated scope object, and sets the |
---|
107 | associated scope object to the one referred to by <code>x.ptr()</code>. |
---|
108 | The <code>object</code> base class is initialized with <code>x</code>. |
---|
109 | <pre> |
---|
110 | scope(); |
---|
111 | </pre> |
---|
112 | Stores a reference to the current associated scope object. The |
---|
113 | <code>object</code> base class is initialized with the current associated |
---|
114 | scope object. Outside any module initialization function, the current |
---|
115 | associated Python object is <code>None</code>. |
---|
116 | <pre> |
---|
117 | ~scope() |
---|
118 | </pre> |
---|
119 | Sets the current associated Python object to the stored object. |
---|
120 | |
---|
121 | <h2><a name="examples"></a>Example</h2> |
---|
122 | The following example shows how scope setting can be used to define |
---|
123 | nested classes. |
---|
124 | |
---|
125 | <p>C++ Module definition:</p> |
---|
126 | <pre> |
---|
127 | #include <boost/python/module.hpp> |
---|
128 | #include <boost/python/class.hpp> |
---|
129 | #include <boost/python/scope.hpp> |
---|
130 | using namespace boost::python; |
---|
131 | |
---|
132 | struct X |
---|
133 | { |
---|
134 | void f() {} |
---|
135 | |
---|
136 | struct Y { int g() { return 42; } }; |
---|
137 | }; |
---|
138 | |
---|
139 | BOOST_PYTHON_MODULE(nested) |
---|
140 | { |
---|
141 | // add some constants to the current (module) scope |
---|
142 | scope().attr("yes") = 1; |
---|
143 | scope().attr("no") = 0; |
---|
144 | |
---|
145 | // Change the current scope |
---|
146 | scope outer |
---|
147 | = class_<X>("X") |
---|
148 | .def("f", &X::f) |
---|
149 | ; |
---|
150 | |
---|
151 | // Define a class Y in the current scope, X |
---|
152 | class_<X::Y>("Y") |
---|
153 | .def("g", &X::Y::g) |
---|
154 | ; |
---|
155 | } |
---|
156 | </pre> |
---|
157 | Interactive Python: |
---|
158 | <pre> |
---|
159 | >>> import nested |
---|
160 | >>> nested.yes |
---|
161 | 1 |
---|
162 | >>> y = nested.X.Y() |
---|
163 | >>> y.g() |
---|
164 | 42 |
---|
165 | </pre> |
---|
166 | |
---|
167 | <p>Revised 09 October, 2002</p> |
---|
168 | |
---|
169 | <p><i>© Copyright <a href= |
---|
170 | "../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> |
---|
171 | </body> |
---|
172 | </html> |
---|
173 | |
---|