1/* ------------------------------------------------------------ 2 * Overloaded operator support 3 * ------------------------------------------------------------ */ 4 5 6#ifdef __cplusplus 7 8#define %pybinoperator(pyname,oper) %rename(pyname) oper; %pythonmaybecall oper 9 10%pybinoperator(__add__, *::operator+); 11%pybinoperator(__pos__, *::operator+()); 12%pybinoperator(__pos__, *::operator+() const); 13%pybinoperator(__sub__, *::operator-); 14%pybinoperator(__neg__, *::operator-()); 15%pybinoperator(__neg__, *::operator-() const); 16%pybinoperator(__mul__, *::operator*); 17%pybinoperator(__div__, *::operator/); 18%pybinoperator(__mod__, *::operator%); 19%pybinoperator(__lshift__, *::operator<<); 20%pybinoperator(__rshift__, *::operator>>); 21%pybinoperator(__and__, *::operator&); 22%pybinoperator(__or__, *::operator|); 23%pybinoperator(__xor__, *::operator^); 24%pybinoperator(__lt__, *::operator<); 25%pybinoperator(__le__, *::operator<=); 26%pybinoperator(__gt__, *::operator>); 27%pybinoperator(__ge__, *::operator>=); 28%pybinoperator(__eq__, *::operator==); 29%pybinoperator(__ne__, *::operator!=); 30 31 32 33/* Special cases */ 34%rename(__invert__) *::operator~; 35%rename(__call__) *::operator(); 36 37%feature("shadow") *::operator bool %{ 38def __nonzero__(self): 39 return $action(self) 40__bool__ = __nonzero__ 41%}; 42%rename(__nonzero__) *::operator bool; 43 44/* Ignored operators */ 45%ignoreoperator(LNOT) operator!; 46%ignoreoperator(LAND) operator&&; 47%ignoreoperator(LOR) operator||; 48%ignoreoperator(EQ) *::operator=; 49%ignoreoperator(PLUSPLUS) *::operator++; 50%ignoreoperator(MINUSMINUS) *::operator--; 51%ignoreoperator(ARROWSTAR) *::operator->*; 52%ignoreoperator(INDEX) *::operator[]; 53 54/* 55 Inplace operator declarations. 56 57 They translate the inplace C++ operators (+=, -=, ...) into the 58 corresponding python equivalents(__iadd__,__isub__), etc, 59 disabling the ownership of the input 'self' pointer, and assigning 60 it to the returning object: 61 62 %feature("del") *::Operator; 63 %feature("new") *::Operator; 64 65 This makes the most common case safe, ie: 66 67 A& A::operator+=(int i) { ...; return *this; } 68 ^^^^ ^^^^^^ 69 70 will work fine, even when the resulting python object shares the 71 'this' pointer with the input one. The input object is usually 72 deleted after the operation, including the shared 'this' pointer, 73 producing 'strange' seg faults, as reported by Lucriz 74 (lucriz@sitilandia.it). 75 76 If you have an interface that already takes care of that, ie, you 77 already are using inplace operators and you are not getting 78 seg. faults, with the new scheme you could end with 'free' elements 79 that never get deleted (maybe, not sure, it depends). But if that is 80 the case, you could recover the old behaviour using 81 82 %feature("del","") A::operator+=; 83 %feature("new","") A::operator+=; 84 85 which recovers the old behaviour for the class 'A', or if you are 86 100% sure your entire system works fine in the old way, use: 87 88 %feature("del","") *::operator+=; 89 %feature("new","") *::operator+=; 90 91*/ 92 93#define %pyinplaceoper(SwigPyOper, Oper) %delobject Oper; %newobject Oper; %rename(SwigPyOper) Oper 94 95%pyinplaceoper(__iadd__ , *::operator +=); 96%pyinplaceoper(__isub__ , *::operator -=); 97%pyinplaceoper(__imul__ , *::operator *=); 98%pyinplaceoper(__idiv__ , *::operator /=); 99%pyinplaceoper(__imod__ , *::operator %=); 100%pyinplaceoper(__iand__ , *::operator &=); 101%pyinplaceoper(__ior__ , *::operator |=); 102%pyinplaceoper(__ixor__ , *::operator ^=); 103%pyinplaceoper(__ilshift__, *::operator <<=); 104%pyinplaceoper(__irshift__, *::operator >>=); 105 106 107/* Finally, in python we need to mark the binary operations to fail as 108 'maybecall' methods */ 109 110#define %pybinopermaybecall(oper) %pythonmaybecall __ ## oper ## __; %pythonmaybecall __r ## oper ## __ 111 112%pybinopermaybecall(add); 113%pybinopermaybecall(pos); 114%pybinopermaybecall(pos); 115%pybinopermaybecall(sub); 116%pybinopermaybecall(neg); 117%pybinopermaybecall(neg); 118%pybinopermaybecall(mul); 119%pybinopermaybecall(div); 120%pybinopermaybecall(mod); 121%pybinopermaybecall(lshift); 122%pybinopermaybecall(rshift); 123%pybinopermaybecall(and); 124%pybinopermaybecall(or); 125%pybinopermaybecall(xor); 126%pybinopermaybecall(lt); 127%pybinopermaybecall(le); 128%pybinopermaybecall(gt); 129%pybinopermaybecall(ge); 130%pybinopermaybecall(eq); 131%pybinopermaybecall(ne); 132 133#endif 134 135 136 137