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/operators.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/operators.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="#self_t-spec">Class |
---|
44 | <code>self_ns::self_t</code></a></dt> |
---|
45 | |
---|
46 | <dd> |
---|
47 | <dl class="page-index"> |
---|
48 | <dt><a href="#self_t-spec-synopsis">Class <code>self_t</code> |
---|
49 | synopsis</a></dt> |
---|
50 | |
---|
51 | <dt><a href="#self_t-spec-inplace">Class <code>self_t</code> |
---|
52 | inplace operators</a></dt> |
---|
53 | |
---|
54 | <dt><a href="#self_t-spec-comparisons">Class |
---|
55 | <code>self_t</code> comparison functions</a></dt> |
---|
56 | |
---|
57 | <dt><a href="#self_t-spec-ops">Class <code>self_t</code> |
---|
58 | non-member operations</a></dt> |
---|
59 | |
---|
60 | <dt><a href="#self_t-spec-value-unary-ops">Class |
---|
61 | <code>self_t</code> unary operations</a></dt> |
---|
62 | |
---|
63 | <dt><a href="#self_t-spec-value-ops">Class |
---|
64 | <code>self_t</code> value operations</a></dt> |
---|
65 | </dl> |
---|
66 | </dd> |
---|
67 | |
---|
68 | <dt><a href="#other-spec">Class template |
---|
69 | <code>other</code></a></dt> |
---|
70 | |
---|
71 | <dd> |
---|
72 | <dl class="page-index"> |
---|
73 | <dt><a href="#other-spec-synopsis">Class <code>other</code> |
---|
74 | synopsis</a></dt> |
---|
75 | </dl> |
---|
76 | </dd> |
---|
77 | |
---|
78 | <dt><a href="#operator_-spec">Class template |
---|
79 | <code>operator_</code></a></dt> |
---|
80 | |
---|
81 | <dd> |
---|
82 | <dl class="page-index"> |
---|
83 | <dt><a href="#operator_-spec-synopsis">Class |
---|
84 | <code>operator_</code> synopsis</a></dt> |
---|
85 | </dl> |
---|
86 | </dd> |
---|
87 | </dl> |
---|
88 | </dd> |
---|
89 | |
---|
90 | <dt><a href="#objects">Objects</a></dt> |
---|
91 | |
---|
92 | <dd> |
---|
93 | <dl class="page-index"> |
---|
94 | <dt><a href="#self-spec">self</a></dt> |
---|
95 | </dl> |
---|
96 | </dd> |
---|
97 | |
---|
98 | <dt><a href="#examples">Examples</a></dt> |
---|
99 | </dl> |
---|
100 | <hr> |
---|
101 | |
---|
102 | <h2><a name="introduction"></a>Introduction</h2> |
---|
103 | |
---|
104 | <p><code><boost/python/operators.hpp></code> provides types and |
---|
105 | functions for automatically generating Python <a href= |
---|
106 | "http://www.python.org/doc/ref/specialnames.html">special methods</a> |
---|
107 | from the corresponding C++ constructs. Most of these constructs are |
---|
108 | operator expressions, hence the name. To use the facility, substitute the |
---|
109 | <code><a href="#self-spec">self</a></code> object for an object of the |
---|
110 | class type being wrapped in the expression to be exposed, and pass the |
---|
111 | result to <a href= |
---|
112 | "class.html#class_-spec-modifiers">class_<>::def()</a>. Much of |
---|
113 | what is exposed in this header should be considered part of the |
---|
114 | implementation, so is not documented in detail here.</p> |
---|
115 | |
---|
116 | <h2><a name="classes"></a>Classes</h2> |
---|
117 | |
---|
118 | <h3><a name="self_t-spec"></a>Class <code>self_ns::self_t</code></h3> |
---|
119 | |
---|
120 | <p><code>self_ns::self_t</code> is the actual type of the <a href= |
---|
121 | "#self-spec"><code>self</code></a> object. The library isolates |
---|
122 | <code>self_t</code> in its own namespace, <code>self_ns</code>, in order |
---|
123 | to prevent the generalized operator templates which operate on it from |
---|
124 | being found by argument-dependent lookup in other contexts. This should |
---|
125 | be considered an implementation detail, since users should never have to |
---|
126 | mention <code>self_t</code> directly.</p> |
---|
127 | |
---|
128 | <h4><a name="self_t-spec-synopsis"></a>Class <code>self_ns::self_t</code> |
---|
129 | synopsis</h4> |
---|
130 | <pre> |
---|
131 | namespace boost { namespace python { namespace self_ns { |
---|
132 | { |
---|
133 | <i>unspecified-type-declaration</i> self_t; |
---|
134 | |
---|
135 | // inplace operators |
---|
136 | template <class T> <a href= |
---|
137 | "#operator_-spec">operator_</a><<i>unspecified</i>> operator+=(self_t, T); |
---|
138 | template <class T> <a href= |
---|
139 | "#operator_-spec">operator_</a><<i>unspecified</i>> operator-=(self_t, T); |
---|
140 | template <class T> <a href= |
---|
141 | "#operator_-spec">operator_</a><<i>unspecified</i>> operator*=(self_t, T); |
---|
142 | template <class T> <a href= |
---|
143 | "#operator_-spec">operator_</a><<i>unspecified</i>> operator/=(self_t, T); |
---|
144 | template <class T> <a href= |
---|
145 | "#operator_-spec">operator_</a><<i>unspecified</i>> operator%=(self_t, T); |
---|
146 | template <class T> <a href= |
---|
147 | "#operator_-spec">operator_</a><<i>unspecified</i>> operator>>=(self_t, T); |
---|
148 | template <class T> <a href= |
---|
149 | "#operator_-spec">operator_</a><<i>unspecified</i>> operator<<=(self_t, T); |
---|
150 | template <class T> <a href= |
---|
151 | "#operator_-spec">operator_</a><<i>unspecified</i>> operator&=(self_t, T); |
---|
152 | template <class T> <a href= |
---|
153 | "#operator_-spec">operator_</a><<i>unspecified</i>> operator^=(self_t, T); |
---|
154 | template <class T> <a href= |
---|
155 | "#operator_-spec">operator_</a><<i>unspecified</i>> operator|=(self_t, T); |
---|
156 | |
---|
157 | // comparisons |
---|
158 | template <class L, class R> <a href= |
---|
159 | "#operator_-spec">operator_</a><<i>unspecified</i>> operator==(L const&, R const&); |
---|
160 | template <class L, class R> <a href= |
---|
161 | "#operator_-spec">operator_</a><<i>unspecified</i>> operator!=(L const&, R const&); |
---|
162 | template <class L, class R> <a href= |
---|
163 | "#operator_-spec">operator_</a><<i>unspecified</i>> operator<(L const&, R const&); |
---|
164 | template <class L, class R> <a href= |
---|
165 | "#operator_-spec">operator_</a><<i>unspecified</i>> operator>(L const&, R const&); |
---|
166 | template <class L, class R> <a href= |
---|
167 | "#operator_-spec">operator_</a><<i>unspecified</i>> operator<=(L const&, R const&); |
---|
168 | template <class L, class R> <a href= |
---|
169 | "#operator_-spec">operator_</a><<i>unspecified</i>> operator>=(L const&, R const&); |
---|
170 | |
---|
171 | // non-member operations |
---|
172 | template <class L, class R> <a href= |
---|
173 | "#operator_-spec">operator_</a><<i>unspecified</i>> operator+(L const&, R const&); |
---|
174 | template <class L, class R> <a href= |
---|
175 | "#operator_-spec">operator_</a><<i>unspecified</i>> operator-(L const&, R const&); |
---|
176 | template <class L, class R> <a href= |
---|
177 | "#operator_-spec">operator_</a><<i>unspecified</i>> operator*(L const&, R const&); |
---|
178 | template <class L, class R> <a href= |
---|
179 | "#operator_-spec">operator_</a><<i>unspecified</i>> operator/(L const&, R const&); |
---|
180 | template <class L, class R> <a href= |
---|
181 | "#operator_-spec">operator_</a><<i>unspecified</i>> operator%(L const&, R const&); |
---|
182 | template <class L, class R> <a href= |
---|
183 | "#operator_-spec">operator_</a><<i>unspecified</i>> operator>>(L const&, R const&); |
---|
184 | template <class L, class R> <a href= |
---|
185 | "#operator_-spec">operator_</a><<i>unspecified</i>> operator<<(L const&, R const&); |
---|
186 | template <class L, class R> <a href= |
---|
187 | "#operator_-spec">operator_</a><<i>unspecified</i>> operator&(L const&, R const&); |
---|
188 | template <class L, class R> <a href= |
---|
189 | "#operator_-spec">operator_</a><<i>unspecified</i>> operator^(L const&, R const&); |
---|
190 | template <class L, class R> <a href= |
---|
191 | "#operator_-spec">operator_</a><<i>unspecified</i>> operator|(L const&, R const&); |
---|
192 | template <class L, class R> <a href= |
---|
193 | "#operator_-spec">operator_</a><<i>unspecified</i>> pow(L const&, R const&); |
---|
194 | |
---|
195 | // unary operations |
---|
196 | <a href= |
---|
197 | "#operator_-spec">operator_</a><<i>unspecified</i>> operator-(self_t); |
---|
198 | <a href= |
---|
199 | "#operator_-spec">operator_</a><<i>unspecified</i>> operator+(self_t); |
---|
200 | <a href= |
---|
201 | "#operator_-spec">operator_</a><<i>unspecified</i>> operator~(self_t); |
---|
202 | <a href= |
---|
203 | "#operator_-spec">operator_</a><<i>unspecified</i>> operator!(self_t); |
---|
204 | |
---|
205 | // value operations |
---|
206 | <a href= |
---|
207 | "#operator_-spec">operator_</a><<i>unspecified</i>> int_(self_t); |
---|
208 | <a href= |
---|
209 | "#operator_-spec">operator_</a><<i>unspecified</i>> long_(self_t); |
---|
210 | <a href= |
---|
211 | "#operator_-spec">operator_</a><<i>unspecified</i>> float_(self_t); |
---|
212 | <a href= |
---|
213 | "#operator_-spec">operator_</a><<i>unspecified</i>> complex_(self_t); |
---|
214 | <a href= |
---|
215 | "#operator_-spec">operator_</a><<i>unspecified</i>> str(self_t); |
---|
216 | |
---|
217 | }}}; |
---|
218 | </pre> |
---|
219 | The tables below describe the methods generated when the results of the |
---|
220 | expressions described are passed as arguments to <a href= |
---|
221 | "class.html#class_-spec-modifiers">class_<>::def()</a>. |
---|
222 | <code><b>x</b></code> is an object of the class type being wrapped. |
---|
223 | |
---|
224 | <h4><a name="self_t-spec-inplace"></a>Class <code>self_t</code> inplace |
---|
225 | operators</h4> |
---|
226 | In the table below, If <code><b>r</b></code> is an object of type |
---|
227 | <code><a href="#other-spec">other</a><T></code>, |
---|
228 | <code><b>y</b></code> is an object of type <code>T</code>; otherwise, |
---|
229 | <code><b>y</b></code> is an object of the same type as |
---|
230 | <code><b>r</b></code>. |
---|
231 | |
---|
232 | <table border="1" summary="self_t inplace operators"> |
---|
233 | <tr> |
---|
234 | <th>C++ Expression</th> |
---|
235 | |
---|
236 | <th>Python Method Name</th> |
---|
237 | |
---|
238 | <th>C++ Implementation</th> |
---|
239 | </tr> |
---|
240 | |
---|
241 | <tr> |
---|
242 | <td><code>self += r</code></td> |
---|
243 | |
---|
244 | <td><code>__iadd__</code></td> |
---|
245 | |
---|
246 | <td><code>x += y</code></td> |
---|
247 | </tr> |
---|
248 | |
---|
249 | <tr> |
---|
250 | <td><code>self -= r</code></td> |
---|
251 | |
---|
252 | <td><code>__isub__</code></td> |
---|
253 | |
---|
254 | <td><code>x -= y</code></td> |
---|
255 | </tr> |
---|
256 | |
---|
257 | <tr> |
---|
258 | <td><code>self *= r</code></td> |
---|
259 | |
---|
260 | <td><code>__imul__</code></td> |
---|
261 | |
---|
262 | <td><code>x *= y</code></td> |
---|
263 | </tr> |
---|
264 | |
---|
265 | <tr> |
---|
266 | <td><code>self /= r</code></td> |
---|
267 | |
---|
268 | <td><code>__idiv__</code></td> |
---|
269 | |
---|
270 | <td><code>x /= y</code></td> |
---|
271 | </tr> |
---|
272 | |
---|
273 | <tr> |
---|
274 | <td><code>self %= r</code></td> |
---|
275 | |
---|
276 | <td><code>__imod__</code></td> |
---|
277 | |
---|
278 | <td><code>x %= y</code></td> |
---|
279 | </tr> |
---|
280 | |
---|
281 | <tr> |
---|
282 | <td><code>self >>= r</code></td> |
---|
283 | |
---|
284 | <td><code>__irshift__</code></td> |
---|
285 | |
---|
286 | <td><code>x >>= y</code></td> |
---|
287 | </tr> |
---|
288 | |
---|
289 | <tr> |
---|
290 | <td><code>self <<= r</code></td> |
---|
291 | |
---|
292 | <td><code>__ilshift__</code></td> |
---|
293 | |
---|
294 | <td><code>x <<= y</code></td> |
---|
295 | </tr> |
---|
296 | |
---|
297 | <tr> |
---|
298 | <td><code>self &= r</code></td> |
---|
299 | |
---|
300 | <td><code>__iand__</code></td> |
---|
301 | |
---|
302 | <td><code>x &= y</code></td> |
---|
303 | </tr> |
---|
304 | |
---|
305 | <tr> |
---|
306 | <td><code>self ^= r</code></td> |
---|
307 | |
---|
308 | <td><code>__ixor__</code></td> |
---|
309 | |
---|
310 | <td><code>x ^= y</code></td> |
---|
311 | </tr> |
---|
312 | |
---|
313 | <tr> |
---|
314 | <td><code>self |= r</code></td> |
---|
315 | |
---|
316 | <td><code>__ior__</code></td> |
---|
317 | |
---|
318 | <td><code>x |= y</code></td> |
---|
319 | </tr> |
---|
320 | </table> |
---|
321 | |
---|
322 | <h4><a name="self_t-spec-comparisons"></a>Class <code>self_t</code> |
---|
323 | comparison functions</h4> |
---|
324 | In the tables below, if <code><b>r</b></code> is of type <code><a href= |
---|
325 | "#self_t-spec">self_t</a></code>, <code><b>y</b></code> is an object of |
---|
326 | the same type as <code>x</code>; <br> |
---|
327 | if <code><b>l</b></code> or <code><b>r</b></code> is an object of type |
---|
328 | <code><a href="#other-spec">other</a><T></code>, |
---|
329 | <code><b>y</b></code> is an object of type <code>T</code>; <br> |
---|
330 | otherwise, <code><b>y</b></code> is an object of the same type as |
---|
331 | <code><b>l</b></code> or <code><b>r</b></code>.<br> |
---|
332 | <code><b>l</b></code> is never of type <code><a href= |
---|
333 | "#self_t-spec">self_t</a></code>. |
---|
334 | |
---|
335 | <p>The column of <b>Python Expressions</b> illustrates the expressions |
---|
336 | that will be supported in Python for objects convertible to the types of |
---|
337 | <code>x</code> and <code>y</code>. The secondary operation arises due to |
---|
338 | Python's <a href= |
---|
339 | "http://www.python.org/doc/ref/customization.html#l2h-89">reflection |
---|
340 | rules</a> for rich comparison operators, and are only used when the |
---|
341 | corresponding operation is not defined as a method of the <code>y</code> |
---|
342 | object.</p> |
---|
343 | |
---|
344 | <table border="1" summary="self_t comparison functions"> |
---|
345 | <tr> |
---|
346 | <th>C++ Expression</th> |
---|
347 | |
---|
348 | <th>Python Method Name</th> |
---|
349 | |
---|
350 | <th>C++ Implementation</th> |
---|
351 | |
---|
352 | <th>Python Expressions<br> |
---|
353 | (primary, secondary)</th> |
---|
354 | </tr> |
---|
355 | |
---|
356 | <tr> |
---|
357 | <td><code>self == r</code></td> |
---|
358 | |
---|
359 | <td><code>__eq__</code></td> |
---|
360 | |
---|
361 | <td><code>x == y</code></td> |
---|
362 | |
---|
363 | <td><code>x == y, y == x</code></td> |
---|
364 | </tr> |
---|
365 | |
---|
366 | <tr> |
---|
367 | <td><code>l == self</code></td> |
---|
368 | |
---|
369 | <td><code>__eq__</code></td> |
---|
370 | |
---|
371 | <td><code>y == x</code></td> |
---|
372 | |
---|
373 | <td><code>y == x, x == y</code></td> |
---|
374 | </tr> |
---|
375 | |
---|
376 | <tr> |
---|
377 | <td><code>self != r</code></td> |
---|
378 | |
---|
379 | <td><code>__ne__</code></td> |
---|
380 | |
---|
381 | <td><code>x != y</code></td> |
---|
382 | |
---|
383 | <td><code>x != y, y != x</code></td> |
---|
384 | </tr> |
---|
385 | |
---|
386 | <tr> |
---|
387 | <td><code>l != self</code></td> |
---|
388 | |
---|
389 | <td><code>__ne__</code></td> |
---|
390 | |
---|
391 | <td><code>y != x</code></td> |
---|
392 | |
---|
393 | <td><code>y != x, x != y</code></td> |
---|
394 | </tr> |
---|
395 | |
---|
396 | <tr> |
---|
397 | <td><code>self < r</code></td> |
---|
398 | |
---|
399 | <td><code>__lt__</code></td> |
---|
400 | |
---|
401 | <td><code>x < y</code></td> |
---|
402 | |
---|
403 | <td><code>x < y, y > x</code></td> |
---|
404 | </tr> |
---|
405 | |
---|
406 | <tr> |
---|
407 | <td><code>l < self</code></td> |
---|
408 | |
---|
409 | <td><code>__gt__</code></td> |
---|
410 | |
---|
411 | <td><code>y < x</code></td> |
---|
412 | |
---|
413 | <td><code>y > x, x < y</code></td> |
---|
414 | </tr> |
---|
415 | |
---|
416 | <tr> |
---|
417 | <td><code>self > r</code></td> |
---|
418 | |
---|
419 | <td><code>__gt__</code></td> |
---|
420 | |
---|
421 | <td><code>x > y</code></td> |
---|
422 | |
---|
423 | <td><code>x > y, y < x</code></td> |
---|
424 | </tr> |
---|
425 | |
---|
426 | <tr> |
---|
427 | <td><code>l > self</code></td> |
---|
428 | |
---|
429 | <td><code>__lt__</code></td> |
---|
430 | |
---|
431 | <td><code>y > x</code></td> |
---|
432 | |
---|
433 | <td><code>y < x, x > y</code></td> |
---|
434 | </tr> |
---|
435 | |
---|
436 | <tr> |
---|
437 | <td><code>self <= r</code></td> |
---|
438 | |
---|
439 | <td><code>__le__</code></td> |
---|
440 | |
---|
441 | <td><code>x <= y</code></td> |
---|
442 | |
---|
443 | <td><code>x <= y, y >= x</code></td> |
---|
444 | </tr> |
---|
445 | |
---|
446 | <tr> |
---|
447 | <td><code>l <= self</code></td> |
---|
448 | |
---|
449 | <td><code>__ge__</code></td> |
---|
450 | |
---|
451 | <td><code>y <= x</code></td> |
---|
452 | |
---|
453 | <td><code>y >= x, x <= y</code></td> |
---|
454 | </tr> |
---|
455 | |
---|
456 | <tr> |
---|
457 | <td><code>self >= r</code></td> |
---|
458 | |
---|
459 | <td><code>__ge__</code></td> |
---|
460 | |
---|
461 | <td><code>x >= y</code></td> |
---|
462 | |
---|
463 | <td><code>x >= y, y <= x</code></td> |
---|
464 | </tr> |
---|
465 | |
---|
466 | <tr> |
---|
467 | <td><code>l >= self</code></td> |
---|
468 | |
---|
469 | <td><code>__le__</code></td> |
---|
470 | |
---|
471 | <td><code>y >= x</code></td> |
---|
472 | |
---|
473 | <td><code>y <= x, x >= y</code></td> |
---|
474 | </tr> |
---|
475 | </table> |
---|
476 | |
---|
477 | <h4><a name="self_t-spec-ops"></a>Class <code>self_t</code> non-member |
---|
478 | operations</h4> |
---|
479 | The operations whose names begin with "<code>__r</code>" below will only |
---|
480 | be called if the left-hand operand does not already support the given |
---|
481 | operation, as described <a href= |
---|
482 | "http://www.python.org/doc/current/ref/numeric-types.html#l2h-152">here</a>. |
---|
483 | |
---|
484 | |
---|
485 | <table border="1" summary="self_t non-member operations"> |
---|
486 | <tr> |
---|
487 | <th>C++ Expression</th> |
---|
488 | |
---|
489 | <th>Python Method Name</th> |
---|
490 | |
---|
491 | <th>C++ Implementation</th> |
---|
492 | </tr> |
---|
493 | |
---|
494 | <tr> |
---|
495 | <td><code>self + r</code></td> |
---|
496 | |
---|
497 | <td><code>__add__</code></td> |
---|
498 | |
---|
499 | <td><code>x + y</code></td> |
---|
500 | </tr> |
---|
501 | |
---|
502 | <tr> |
---|
503 | <td><code>l + self</code></td> |
---|
504 | |
---|
505 | <td><code>__radd__</code></td> |
---|
506 | |
---|
507 | <td><code>y + x</code></td> |
---|
508 | </tr> |
---|
509 | |
---|
510 | <tr> |
---|
511 | <td><code>self - r</code></td> |
---|
512 | |
---|
513 | <td><code>__sub__</code></td> |
---|
514 | |
---|
515 | <td><code>x - y</code></td> |
---|
516 | </tr> |
---|
517 | |
---|
518 | <tr> |
---|
519 | <td><code>l - self</code></td> |
---|
520 | |
---|
521 | <td><code>__rsub__</code></td> |
---|
522 | |
---|
523 | <td><code>y - x</code></td> |
---|
524 | </tr> |
---|
525 | |
---|
526 | <tr> |
---|
527 | <td><code>self * r</code></td> |
---|
528 | |
---|
529 | <td><code>__mul__</code></td> |
---|
530 | |
---|
531 | <td><code>x * y</code></td> |
---|
532 | </tr> |
---|
533 | |
---|
534 | <tr> |
---|
535 | <td><code>l * self</code></td> |
---|
536 | |
---|
537 | <td><code>__rmul__</code></td> |
---|
538 | |
---|
539 | <td><code>y * x</code></td> |
---|
540 | </tr> |
---|
541 | |
---|
542 | <tr> |
---|
543 | <td><code>self / r</code></td> |
---|
544 | |
---|
545 | <td><code>__div__</code></td> |
---|
546 | |
---|
547 | <td><code>x / y</code></td> |
---|
548 | </tr> |
---|
549 | |
---|
550 | <tr> |
---|
551 | <td><code>l / self</code></td> |
---|
552 | |
---|
553 | <td><code>__rdiv__</code></td> |
---|
554 | |
---|
555 | <td><code>y / x</code></td> |
---|
556 | </tr> |
---|
557 | |
---|
558 | <tr> |
---|
559 | <td><code>self % r</code></td> |
---|
560 | |
---|
561 | <td><code>__mod__</code></td> |
---|
562 | |
---|
563 | <td><code>x % y</code></td> |
---|
564 | </tr> |
---|
565 | |
---|
566 | <tr> |
---|
567 | <td><code>l % self</code></td> |
---|
568 | |
---|
569 | <td><code>__rmod__</code></td> |
---|
570 | |
---|
571 | <td><code>y % x</code></td> |
---|
572 | </tr> |
---|
573 | |
---|
574 | <tr> |
---|
575 | <td><code>self >> r</code></td> |
---|
576 | |
---|
577 | <td><code>__rshift__</code></td> |
---|
578 | |
---|
579 | <td><code>x >> y</code></td> |
---|
580 | </tr> |
---|
581 | |
---|
582 | <tr> |
---|
583 | <td><code>l >> self</code></td> |
---|
584 | |
---|
585 | <td><code>__rrshift__</code></td> |
---|
586 | |
---|
587 | <td><code>y >> x</code></td> |
---|
588 | </tr> |
---|
589 | |
---|
590 | <tr> |
---|
591 | <td><code>self << r</code></td> |
---|
592 | |
---|
593 | <td><code>__lshift__</code></td> |
---|
594 | |
---|
595 | <td><code>x << y</code></td> |
---|
596 | </tr> |
---|
597 | |
---|
598 | <tr> |
---|
599 | <td><code>l << self</code></td> |
---|
600 | |
---|
601 | <td><code>__rlshift__</code></td> |
---|
602 | |
---|
603 | <td><code>y << x</code></td> |
---|
604 | </tr> |
---|
605 | |
---|
606 | <tr> |
---|
607 | <td><code>self & r</code></td> |
---|
608 | |
---|
609 | <td><code>__and__</code></td> |
---|
610 | |
---|
611 | <td><code>x & y</code></td> |
---|
612 | </tr> |
---|
613 | |
---|
614 | <tr> |
---|
615 | <td><code>l & self</code></td> |
---|
616 | |
---|
617 | <td><code>__rand__</code></td> |
---|
618 | |
---|
619 | <td><code>y & x</code></td> |
---|
620 | </tr> |
---|
621 | |
---|
622 | <tr> |
---|
623 | <td><code>self ^ r</code></td> |
---|
624 | |
---|
625 | <td><code>__xor__</code></td> |
---|
626 | |
---|
627 | <td><code>x ^ y</code></td> |
---|
628 | </tr> |
---|
629 | |
---|
630 | <tr> |
---|
631 | <td><code>l ^ self</code></td> |
---|
632 | |
---|
633 | <td><code>__rxor__</code></td> |
---|
634 | |
---|
635 | <td><code>y ^ x</code></td> |
---|
636 | </tr> |
---|
637 | |
---|
638 | <tr> |
---|
639 | <td><code>self | r</code></td> |
---|
640 | |
---|
641 | <td><code>__or__</code></td> |
---|
642 | |
---|
643 | <td><code>x | y</code></td> |
---|
644 | </tr> |
---|
645 | |
---|
646 | <tr> |
---|
647 | <td><code>l | self</code></td> |
---|
648 | |
---|
649 | <td><code>__ror__</code></td> |
---|
650 | |
---|
651 | <td><code>y | x</code></td> |
---|
652 | </tr> |
---|
653 | |
---|
654 | <tr> |
---|
655 | <td><code>pow(self, r)</code></td> |
---|
656 | |
---|
657 | <td><code>__pow__</code></td> |
---|
658 | |
---|
659 | <td><code>pow(x, y)</code></td> |
---|
660 | </tr> |
---|
661 | |
---|
662 | <tr> |
---|
663 | <td><code>pow(l, self)</code></td> |
---|
664 | |
---|
665 | <td><code>__rpow__</code></td> |
---|
666 | |
---|
667 | <td><code>pow(y, x)</code></td> |
---|
668 | </tr> |
---|
669 | </table> |
---|
670 | |
---|
671 | <h4><a name="self_t-spec-value-unary-ops"></a>Class <code>self_t</code> unary |
---|
672 | operations</h4> |
---|
673 | |
---|
674 | <table border="1" summary="self_t unary operations"> |
---|
675 | <tr> |
---|
676 | <th>C++ Expression</th> |
---|
677 | |
---|
678 | <th>Python Method Name</th> |
---|
679 | |
---|
680 | <th>C++ Implementation</th> |
---|
681 | </tr> |
---|
682 | |
---|
683 | <tr> |
---|
684 | <td><code>-self</code></td> |
---|
685 | |
---|
686 | <td><code>__neg__</code></td> |
---|
687 | |
---|
688 | <td><code>-x</code></td> |
---|
689 | </tr> |
---|
690 | |
---|
691 | <tr> |
---|
692 | <td><code>+self</code></td> |
---|
693 | |
---|
694 | <td><code>__pos__</code></td> |
---|
695 | |
---|
696 | <td><code>+x</code></td> |
---|
697 | </tr> |
---|
698 | |
---|
699 | <tr> |
---|
700 | <td><code>~self</code></td> |
---|
701 | |
---|
702 | <td><code>__invert__</code></td> |
---|
703 | |
---|
704 | <td><code>~x</code></td> |
---|
705 | </tr> |
---|
706 | |
---|
707 | <tr> |
---|
708 | <td><code>not self</code><br><i>or</i><br><code>!self</code></td> |
---|
709 | |
---|
710 | <td><code>__nonzero__</code></td> |
---|
711 | |
---|
712 | <td><code>!!x</code></td> |
---|
713 | </tr> |
---|
714 | </table> |
---|
715 | |
---|
716 | <h4><a name="self_t-spec-value-ops"></a>Class <code>self_t</code> value |
---|
717 | operations</h4> |
---|
718 | |
---|
719 | <table border="1" summary="self_t value operations"> |
---|
720 | <tr> |
---|
721 | <th>C++ Expression</th> |
---|
722 | |
---|
723 | <th>Python Method Name</th> |
---|
724 | |
---|
725 | <th>C++ Implementation</th> |
---|
726 | </tr> |
---|
727 | |
---|
728 | <tr> |
---|
729 | <td><code>int_(self)</code></td> |
---|
730 | |
---|
731 | <td><code>__int__</code></td> |
---|
732 | |
---|
733 | <td><code>long(x)</code></td> |
---|
734 | </tr> |
---|
735 | |
---|
736 | <tr> |
---|
737 | <td><code>long_</code></td> |
---|
738 | |
---|
739 | <td><code>__long__</code></td> |
---|
740 | |
---|
741 | <td><code>PyLong_FromLong(x)</code></td> |
---|
742 | </tr> |
---|
743 | |
---|
744 | <tr> |
---|
745 | <td><code>float_</code></td> |
---|
746 | |
---|
747 | <td><code>__float__</code></td> |
---|
748 | |
---|
749 | <td><code>double(x)</code></td> |
---|
750 | </tr> |
---|
751 | |
---|
752 | <tr> |
---|
753 | <td><code>complex_</code></td> |
---|
754 | |
---|
755 | <td><code>__complex__</code></td> |
---|
756 | |
---|
757 | <td><code>std::complex<double>(x)</code></td> |
---|
758 | </tr> |
---|
759 | |
---|
760 | <tr> |
---|
761 | <td><code>str</code></td> |
---|
762 | |
---|
763 | <td><code>__str__</code></td> |
---|
764 | |
---|
765 | <td><code><a href= |
---|
766 | "../../../conversion/lexical_cast.htm#lexical_cast">lexical_cast</a><std::string>(x)</code></td> |
---|
767 | </tr> |
---|
768 | </table> |
---|
769 | |
---|
770 | <h3><a name="other-spec"></a>Class Template <code>other</code></h3> |
---|
771 | |
---|
772 | <p>Instances of <code>other<T></code> can be used in operator |
---|
773 | expressions with <a href="#self-spec">self</a>; the result is equivalent |
---|
774 | to the same expression with a <code>T</code> object in place of |
---|
775 | <code>other<T></code>. Use <code>other<T></code> to prevent |
---|
776 | construction of a <code>T</code> object in case it is heavyweight, when |
---|
777 | no constructor is available, or simply for clarity.</p> |
---|
778 | |
---|
779 | <h4><a name="other-spec-synopsis"></a>Class Template other synopsis</h4> |
---|
780 | <pre> |
---|
781 | namespace boost { namespace python |
---|
782 | { |
---|
783 | template <class T> |
---|
784 | struct other |
---|
785 | { |
---|
786 | }; |
---|
787 | }} |
---|
788 | </pre> |
---|
789 | <!-- --> |
---|
790 | |
---|
791 | <h3><a name="operator_-spec"></a>Class Template |
---|
792 | <code>detail::operator_</code></h3> |
---|
793 | |
---|
794 | <p>Instantiations of <code>detail::operator_<></code> are used as |
---|
795 | the return type of operator expressions involving <code><a href= |
---|
796 | "#self-spec">self</a></code>. This should be considered an implementation |
---|
797 | detail and is only documented here as a way of showing how the result of |
---|
798 | <code>self</code>-expressions match calls to <a href= |
---|
799 | "class.html#class_-spec-modifiers">class_<>::def()</a>.</p> |
---|
800 | |
---|
801 | <h4><a name="operator_-spec-synopsis"></a>Class Template |
---|
802 | <code>detail::operator_</code> synopsis</h4> |
---|
803 | <pre> |
---|
804 | namespace boost { namespace python { namespace detail |
---|
805 | { |
---|
806 | template <<i>unspecified</i>> |
---|
807 | struct operator_ |
---|
808 | { |
---|
809 | }; |
---|
810 | }}} |
---|
811 | </pre> |
---|
812 | |
---|
813 | <h2><a name="objects"></a>Objects</h2> |
---|
814 | |
---|
815 | <p><a name="self-spec"><code>self</code></a></p> |
---|
816 | <pre> |
---|
817 | namespace boost { namespace python |
---|
818 | { |
---|
819 | using self_ns::self; |
---|
820 | }} |
---|
821 | </pre> |
---|
822 | |
---|
823 | <h2><a name="examples"></a>Example</h2> |
---|
824 | <pre> |
---|
825 | #include <boost/python/module.hpp> |
---|
826 | #include <boost/python/class.hpp> |
---|
827 | #include <boost/python/operators.hpp> |
---|
828 | #include <boost/operators.hpp> |
---|
829 | |
---|
830 | struct number |
---|
831 | : boost::<a href= |
---|
832 | "../../../utility/operators.htm#grpd_oprs">integer_arithmetic</a><number> |
---|
833 | { |
---|
834 | explicit number(long x_) : x(x_) {} |
---|
835 | operator long() const { return x; } |
---|
836 | |
---|
837 | template <class T> |
---|
838 | number& operator+=(T const& rhs) |
---|
839 | { x += rhs; return *this; } |
---|
840 | |
---|
841 | template <class T> |
---|
842 | number& operator-=(T const& rhs) |
---|
843 | { x -= rhs; return *this; } |
---|
844 | |
---|
845 | template <class T> |
---|
846 | number& operator*=(T const& rhs) |
---|
847 | { x *= rhs; return *this; } |
---|
848 | |
---|
849 | template <class T> |
---|
850 | number& operator/=(T const& rhs) |
---|
851 | { x /= rhs; return *this; } |
---|
852 | |
---|
853 | template <class T> |
---|
854 | number& operator%=(T const& rhs) |
---|
855 | { x %= rhs; return *this; } |
---|
856 | |
---|
857 | long x; |
---|
858 | }; |
---|
859 | |
---|
860 | using namespace boost::python; |
---|
861 | BOOST_PYTHON_MODULE(demo) |
---|
862 | { |
---|
863 | class_<number>("number", init<long>()) |
---|
864 | // interoperate with self |
---|
865 | .def(self += self) |
---|
866 | .def(self + self) |
---|
867 | .def(self -= self) |
---|
868 | .def(self - self) |
---|
869 | .def(self *= self) |
---|
870 | .def(self * self) |
---|
871 | .def(self /= self) |
---|
872 | .def(self / self) |
---|
873 | .def(self %= self) |
---|
874 | .def(self % self) |
---|
875 | |
---|
876 | // Convert to Python int |
---|
877 | .def(int_(self)) |
---|
878 | |
---|
879 | // interoperate with long |
---|
880 | .def(self += long()) |
---|
881 | .def(self + long()) |
---|
882 | .def(long() + self) |
---|
883 | .def(self -= long()) |
---|
884 | .def(self - long()) |
---|
885 | .def(long() - self) |
---|
886 | .def(self *= long()) |
---|
887 | .def(self * long()) |
---|
888 | .def(long() * self) |
---|
889 | .def(self /= long()) |
---|
890 | .def(self / long()) |
---|
891 | .def(long() / self) |
---|
892 | .def(self %= long()) |
---|
893 | .def(self % long()) |
---|
894 | .def(long() % self) |
---|
895 | ; |
---|
896 | } |
---|
897 | </pre> |
---|
898 | |
---|
899 | <p>Revised |
---|
900 | <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan --> |
---|
901 | 5 October, 2004 |
---|
902 | <!--webbot bot="Timestamp" endspan i-checksum="39359" --> |
---|
903 | </p> |
---|
904 | |
---|
905 | <p><i>© Copyright <a href= |
---|
906 | "../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> |
---|
907 | </body> |
---|
908 | </html> |
---|
909 | |
---|