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/init.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">Headers <boost/python/init.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= |
---|
40 | "#init-expressions"><em>init-expressions</em></a></dt> |
---|
41 | |
---|
42 | <dt><a href="#classes">Classes</a></dt> |
---|
43 | |
---|
44 | <dd> |
---|
45 | <dl class="page-index"> |
---|
46 | <dt><a href="#init-spec">Class template <code>init</code></a></dt> |
---|
47 | |
---|
48 | <dd> |
---|
49 | <dl class="page-index"> |
---|
50 | <dt><a href="#init-spec-synopsis">Class template |
---|
51 | <code>init</code> synopsis</a></dt> |
---|
52 | |
---|
53 | <dt><a href="#init-spec-ctors">Class <code>init</code> |
---|
54 | constructors</a></dt> |
---|
55 | |
---|
56 | </dl> |
---|
57 | </dd> |
---|
58 | |
---|
59 | <dt><a href="#optional-spec">Class template |
---|
60 | <code>optional</code></a></dt> |
---|
61 | |
---|
62 | <dd> |
---|
63 | <dl class="page-index"> |
---|
64 | <dt><a href="#optional-spec-synopsis">Class template |
---|
65 | <code>optional</code> synopsis</a></dt> |
---|
66 | </dl> |
---|
67 | </dd> |
---|
68 | </dl> |
---|
69 | </dd> |
---|
70 | |
---|
71 | <dt><a href="#examples">Example(s)</a></dt> |
---|
72 | </dl> |
---|
73 | <hr> |
---|
74 | |
---|
75 | <h2><a name="introduction"></a>Introduction</h2> |
---|
76 | |
---|
77 | <p><code><boost/python/init.hpp></code> defines the interface for |
---|
78 | exposing C++ constructors to Python as extension class |
---|
79 | <code>__init__</code> functions.</p> |
---|
80 | |
---|
81 | <h2><a name="init-expressions"><em>init-expressions</em></a></h2> |
---|
82 | An <em>init-expression</em> is used to describe a family of |
---|
83 | <code>__init__</code> methods to be generated for an extension class, and |
---|
84 | the result has the following properties: |
---|
85 | |
---|
86 | <blockquote> |
---|
87 | <dl class="properties"> |
---|
88 | <dt><b>docstring:</b> An <a href="definitions.html#ntbs">ntbs</a> |
---|
89 | whose value will bound to the method's <code>__doc__</code> |
---|
90 | attribute</dt> |
---|
91 | |
---|
92 | <dt><b>keywords:</b> A <a href= |
---|
93 | "args.html#keyword-expression">keyword-expression</a> which will be |
---|
94 | used to name (a trailing subsequence of) the arguments to the |
---|
95 | generated <code>__init__</code> function(s).</dt> |
---|
96 | |
---|
97 | <dt><b>call policies:</b> An instance of a model of <a href= |
---|
98 | "CallPolicies.html">CallPolicies</a>.</dt> |
---|
99 | |
---|
100 | <dt><b>argument types:</b> An MPL sequence of C++ argument types |
---|
101 | which will be used to construct the wrapped C++ object. An init |
---|
102 | expression has one or more |
---|
103 | <b>valid prefixes</b> which are given by a sequence of |
---|
104 | prefixes of its argument types.</dt> |
---|
105 | </dl> |
---|
106 | </blockquote> |
---|
107 | |
---|
108 | <h2><a name="classes"></a>Classes</h2> |
---|
109 | |
---|
110 | <h3><a name="init-spec"></a>Class template <code>init<T1 =</code> |
---|
111 | <i>unspecified</i><code>, T2 =</code> |
---|
112 | <i>unspecified</i><code>,</code>...<code>Tn</code> = |
---|
113 | <i>unspecified</i><code>></code></h3> |
---|
114 | |
---|
115 | <p>A <a href="../../../mpl/doc/refmanual/forward-sequence.html">MPL sequence</a> which |
---|
116 | can be used to specify a family of one or more <code>__init__</code> |
---|
117 | functions. Only the last <code>T</code><i><small>i</small></i> supplied |
---|
118 | may be an instantiation of <a href= |
---|
119 | "#optional-spec"><code>optional</code></a><code><</code>...<code>></code>.</p> |
---|
120 | |
---|
121 | <h4><a name="init-spec-synopsis"></a>Class template <code>init</code> |
---|
122 | synopsis</h4> |
---|
123 | <pre> |
---|
124 | namespace boost { namespace python |
---|
125 | { |
---|
126 | template <T1 = <i>unspecified</i>,...T<i>n</i> = <i>unspecified</i>> |
---|
127 | struct init |
---|
128 | { |
---|
129 | init(char const* doc = 0); |
---|
130 | template <class Keywords> init(Keywords const& kw, char const* doc = 0); |
---|
131 | template <class Keywords> init(char const* doc, Keywords const& kw); |
---|
132 | |
---|
133 | template <class CallPolicies> |
---|
134 | <em>unspecified</em> operator[](CallPolicies const& policies) const |
---|
135 | }; |
---|
136 | }} |
---|
137 | </pre> |
---|
138 | |
---|
139 | <h4><a name="init-spec-ctors"></a>Class template <code>init</code> |
---|
140 | constructors</h4> |
---|
141 | <pre> |
---|
142 | init(char const* doc = 0); |
---|
143 | template <class Keywords> init(Keywords const& kw, char const* doc = 0); |
---|
144 | template <class Keywords> init(char const* doc, Keywords const& kw); |
---|
145 | </pre> |
---|
146 | |
---|
147 | <dl class="function-semantics"> |
---|
148 | <dt><b>Requires:</b> If supplied, <code>doc</code> is an <a href= |
---|
149 | "definitions.html#ntbs">ntbs</a>. If supplied, <code>kw</code> is the |
---|
150 | result of a <a href="args.html#keyword-expression"></a></dt> |
---|
151 | |
---|
152 | <dt><b>Effects:</b> The result is an <em>init-expression</em> whose |
---|
153 | <em>docstring</em> is <code>doc</code> and whose <em>keywords</em> are |
---|
154 | a reference to <code>kw</code>. If the first form is used, the |
---|
155 | resulting expression's <em>keywords</em> are empty. The expression's |
---|
156 | <em>call policies</em> are an instance of <a href= |
---|
157 | "default_call_policies.html#default_call_policies-spec">default_call_policies</a>. |
---|
158 | If <code>T</code><i><small>n</small></i> is <a href= |
---|
159 | "#optional-spec"><code>optional</code></a><code><U1, U2,</code>... |
---|
160 | <code>U</code><small><i>m</i></small><code>></code>, the |
---|
161 | expression's <em>valid prefixes</em> are given by:</dt> |
---|
162 | |
---|
163 | <dd> |
---|
164 | <blockquote> |
---|
165 | (<code>T1, T2,</code>...<code>T</code><i><small>n-1</small></i>), |
---|
166 | (<code>T1, T2,</code>...<code>T</code><i><small>n-1</small></i> |
---|
167 | <code>, U1</code>), |
---|
168 | (<code>T1, T2,</code>...<code>T</code><i><small>n-1</small></i> |
---|
169 | <code>, U1, U2</code>), |
---|
170 | ...(<code>T1, T2,</code>...<code>T</code><i><small>n-1</small></i> |
---|
171 | <code>, U1, U2,</code>...<code>U</code><i><small>m</small></i>). |
---|
172 | </blockquote> |
---|
173 | Otherwise, the expression has one <em>valid prefix</em> given by the |
---|
174 | the template arguments the user specified. |
---|
175 | </dd> |
---|
176 | </dl> |
---|
177 | |
---|
178 | <h4><a name="init-spec-observers"></a>Class template <code>init</code> |
---|
179 | observer functions</h4> |
---|
180 | <pre> |
---|
181 | template <class Policies> |
---|
182 | <em>unspecified</em> operator[](Policies const& policies) const |
---|
183 | </pre> |
---|
184 | |
---|
185 | <dl class="function-semantics"> |
---|
186 | <dt><b>Requires:</b> Policies is a model of <a href= |
---|
187 | "CallPolicies.html">CallPolicies</a>.</dt> |
---|
188 | |
---|
189 | <dt><b>Effects:</b> Returns a new <a href= |
---|
190 | "#init-expressions"><em>init-expression</em></a> with all the same |
---|
191 | properties as the <code>init</code> object except that its <em>call |
---|
192 | policies</em> are replaced by a reference to |
---|
193 | <code>policies</code>.</dt> |
---|
194 | </dl> |
---|
195 | |
---|
196 | <h3><a name="optional-spec"></a>Class template <code>optional<T1 |
---|
197 | =</code> <i>unspecified</i><code>, T2 =</code> |
---|
198 | <i>unspecified</i><code>,</code>...<code>Tn</code> = |
---|
199 | <i>unspecified</i><code>></code></h3> |
---|
200 | |
---|
201 | <p>A <a href="../../../mpl/doc/refmanual/forward-sequence.html">MPL sequence</a> which |
---|
202 | can be used to specify the optional arguments to an <code>__init__</code> |
---|
203 | function.</p> |
---|
204 | |
---|
205 | <h4><a name="optional-spec-synopsis"></a>Class template |
---|
206 | <code>optional</code> synopsis</h4> |
---|
207 | <pre> |
---|
208 | namespace boost { namespace python |
---|
209 | { |
---|
210 | template <T1 = <i>unspecified</i>,...T<i>n</i> = <i>unspecified</i>> |
---|
211 | struct optional {}; |
---|
212 | }} |
---|
213 | </pre> |
---|
214 | |
---|
215 | <h2><a name="examples"></a>Example(s)</h2> |
---|
216 | |
---|
217 | <p>Given the C++ declarations:</p> |
---|
218 | <pre> |
---|
219 | class Y; |
---|
220 | class X |
---|
221 | { |
---|
222 | public: |
---|
223 | X(int x, Y* y) : m_y(y) {} |
---|
224 | X(double); |
---|
225 | private: |
---|
226 | Y* m_y; |
---|
227 | }; |
---|
228 | </pre> |
---|
229 | A corresponding Boost.Python extension class can be created with: |
---|
230 | <pre> |
---|
231 | using namespace boost::python; |
---|
232 | |
---|
233 | class_<X>("X", "This is X's docstring.", |
---|
234 | init<int,char const*>(args("x","y"), "X.__init__'s docstring")[ |
---|
235 | with_custodian_and_ward<1,3>()] |
---|
236 | ) |
---|
237 | .def(init<double>()) |
---|
238 | ; |
---|
239 | </pre> |
---|
240 | <hr> |
---|
241 | Revised |
---|
242 | <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan --> |
---|
243 | 13 November, 2002 |
---|
244 | <!--webbot bot="Timestamp" endspan i-checksum="39359" --> |
---|
245 | |
---|
246 | |
---|
247 | <p><i>© Copyright <a href= |
---|
248 | "../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> |
---|
249 | </body> |
---|
250 | </html> |
---|
251 | |
---|