1 /* File : operator_overload.i */
3 This is a test of all the possible operator overloads
5 see bottom for a set of possible tests
7 %module operator_overload
9 #if defined(SWIGPYTHON)
10 %warnfilter(SWIGWARN_IGNORE_OPERATOR_EQ,
11 SWIGWARN_IGNORE_OPERATOR_INDEX,
12 SWIGWARN_IGNORE_OPERATOR_PLUSPLUS,
13 SWIGWARN_IGNORE_OPERATOR_MINUSMINUS,
14 SWIGWARN_IGNORE_OPERATOR_LAND,
15 SWIGWARN_IGNORE_OPERATOR_LOR);
18 #if !defined(SWIGLUA) && !defined(SWIGR)
19 %rename(Equal) operator =;
20 %rename(PlusEqual) operator +=;
21 %rename(MinusEqual) operator -=;
22 %rename(MultiplyEqual) operator *=;
23 %rename(DivideEqual) operator /=;
24 %rename(PercentEqual) operator %=;
25 %rename(Plus) operator +;
26 %rename(Minus) operator -;
27 %rename(Multiply) operator *;
28 %rename(Divide) operator /;
29 %rename(Percent) operator %;
30 %rename(Not) operator !;
31 %rename(IndexIntoConst) operator[](unsigned idx) const;
32 %rename(IndexInto) operator[](unsigned idx);
33 %rename(Functor) operator ();
34 %rename(EqualEqual) operator ==;
35 %rename(NotEqual) operator !=;
36 %rename(LessThan) operator <;
37 %rename(LessThanEqual) operator <=;
38 %rename(GreaterThan) operator >;
39 %rename(GreaterThanEqual) operator >=;
40 %rename(And) operator &&;
41 %rename(Or) operator ||;
42 %rename(PlusPlusPrefix) operator++();
43 %rename(PlusPlusPostfix) operator++(int);
44 %rename(MinusMinusPrefix) operator--();
45 %rename(MinusMinusPostfix) operator--(int);
49 %csmethodmodifiers operator++() "protected";
50 %csmethodmodifiers operator++(int) "private";
51 %csmethodmodifiers operator--() "private";
52 %csmethodmodifiers operator--(int) "protected";
53 %typemap(cscode) Op %{
54 public static Op operator++(Op op) {
55 // Unlike C++, operator++ must not modify the parameter and both prefix and postfix operations call this method
56 Op newOp = new Op(op.i);
57 newOp.PlusPlusPostfix(0);
60 public static Op operator--(Op op) {
61 // Unlike C++, operator-- must not modify the parameter and both prefix and postfix operations call this method
62 Op newOp = new Op(op.i);
63 newOp.MinusMinusPrefix();
70 %rename(AndOperator) operator &&;
71 %rename(OrOperator) operator ||;
74 #ifdef SWIG_ALLEGRO_CL
80 %rename(IntCast) operator int();
81 %rename(DoubleCast) operator double();
86 #include <iso646.h> /* for named logical operator, eg 'operator or' */
96 Op(const Op& o) : i(o.i)
101 friend Op operator &&(const Op& a,const Op& b){return Op(a.i&&b.i);}
102 friend Op operator or(const Op& a,const Op& b){return Op(a.i||b.i);}
104 Op &operator=(const Op& o) {
108 // +=,-=... are member fns
109 void operator+=(const Op& o){ i+=o.i;}
110 void operator-=(const Op& o){ i-=o.i;}
111 void operator*=(const Op& o){ i*=o.i;}
112 void operator/=(const Op& o){ i/=o.i;}
113 void operator%=(const Op& o){ i%=o.i;}
114 // the +,-,*,... are friends
115 // (just to make life harder)
116 friend Op operator+(const Op& a,const Op& b){return Op(a.i+b.i);}
117 friend Op operator-(const Op& a,const Op& b);
118 friend Op operator*(const Op& a,const Op& b){return Op(a.i*b.i);}
119 friend Op operator/(const Op& a,const Op& b){return Op(a.i/b.i);}
120 friend Op operator%(const Op& a,const Op& b){return Op(a.i%b.i);}
123 Op operator-() const {return Op(-i);}
124 bool operator !() const {return !(i);}
126 // overloading the [] operator
127 // need 2 versions: get & set
128 // note: C++ can be a little mixed up upon which version it calls
129 // most of the time it calls the second version
130 int operator[](unsigned idx)const
131 { if (idx==0) return i; return 0;}
132 int& operator[](unsigned idx)
133 { if (idx==0) return i; static int j;j=0; return j;}
135 // overloading the () operator
136 // this can have many parameters so we will test this
137 int operator()(int a=0){return i+a;}
138 int operator()(int a,int b){return i+a+b;}
140 // increment/decrement operators
141 Op& operator++() {++i; return *this;} // prefix ++
142 Op operator++(int) {Op o = *this; ++(*this); return o;} // postfix ++
143 Op& operator--() {--i; return *this;} // prefix --
144 Op operator--(int) {Op o = *this; --(*this); return o;} // postfix --
149 operator double() { return i; }
150 virtual operator int() { return i; }
152 // This method just checks that the operators are implemented correctly
153 static void sanity_check();
156 // just to complicate matters
157 // we have a couple of non class operators
158 inline bool operator==(const Op& a,const Op& b){return a.i==b.i;}
159 inline bool operator!=(const Op& a,const Op& b){return a.i!=b.i;}
160 inline bool operator< (const Op& a,const Op& b){return a.i<b.i;}
161 inline bool operator<=(const Op& a,const Op& b){return a.i<=b.i;}
162 inline bool operator> (const Op& a,const Op& b){return a.i>b.i;}
163 inline bool operator>=(const Op& a,const Op& b){return a.i>=b.i;}
168 // This one is not declared inline as VC++7.1 gets mixed up with the unary operator-
169 Op operator-(const Op& a,const Op& b){return Op(a.i-b.i);}
172 // in order to wrapper this correctly
173 // we need to extend the class
174 // to make the friends & non members part of the class
176 Op operator &&(const Op& b){return Op($self->i&&b.i);}
177 Op operator or(const Op& b){return Op($self->i||b.i);}
179 Op operator+(const Op& b){return Op($self->i+b.i);}
180 Op operator-(const Op& b){return Op($self->i-b.i);}
181 Op operator*(const Op& b){return Op($self->i*b.i);}
182 Op operator/(const Op& b){return Op($self->i/b.i);}
183 Op operator%(const Op& b){return Op($self->i%b.i);}
185 bool operator==(const Op& b){return $self->i==b.i;}
186 bool operator!=(const Op& b){return $self->i!=b.i;}
187 bool operator< (const Op& b){return $self->i<b.i;}
188 bool operator<=(const Op& b){return $self->i<=b.i;}
189 bool operator> (const Op& b){return $self->i>b.i;}
190 bool operator>=(const Op& b){return $self->i>=b.i;}
192 // subtraction with reversed arguments
193 Op __rsub__(const int b){return Op(b - $self->i);}
195 // we also add the __str__() fn to the class
196 // this allows it to be converted to a string (so it can be printed)
197 const char* __str__()
199 static char buffer[255];
200 sprintf(buffer,"Op(%d)",$self->i);
203 // to get the [] operator working correctly we need to extend with two function
204 // __getitem__ & __setitem__
205 int __getitem__(unsigned i)
206 { return (*$self)[i]; }
207 void __setitem__(unsigned i,int v)
212 Suggested list of operator overloads (mainly from python)
214 Operators overloaded with their C++ equivalent
215 __add__,__sub__,__mul__,__div__,__mod__ +,-,*,/,%
216 __iadd__,__isub__,__imul__,__idiv__,__imod__ +=,-=,*=,/=,%=
218 __eq__,__ne__,__lt__,__le__,__gt__,__ge__ ==,!=,<,<=,>,>=
219 __not__,__neg__ unary !, unary -
220 __and__,__or__,__xor__ logical and,logical or,logical xor
221 __rshift__,__lshift__ >>,<<
223 __getitem__,__setitem__ for operator[]
225 Operators overloaded without C++ equivilents
226 __pow__ for power operator
227 __str__ converts object to a string (should return a const char*)
228 __concat__ for contatenation (if language supports)
233 class OpDerived : public Op {
235 OpDerived(int a=0) : Op(a)
239 virtual operator int() { return i*2; }
248 void Op::sanity_check()
253 Op c=b; // copy construct
255 Op dd=d; // assignment operator
293 // test unary operators
315 // plus add some code to check the __str__ fn
316 //assert(str(Op(1))=="Op(1)");
317 //assert(str(Op(-3))=="Op(-3)");
324 int newInt = original++;
325 assert(j.i == original);
326 assert(newOp.i == newInt);
330 int newInt = original--;
331 assert(j.i == original);
332 assert(newOp.i == newInt);
336 int newInt = ++original;
337 assert(j.i == original);
338 assert(newOp.i == newInt);
342 int newInt = --original;
343 assert(j.i == original);
344 assert(newOp.i == newInt);
350 assert (check_k == 3);
354 assert (check_l == 4);