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