PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/rel-1.3.35/Examples/test-suite/operator_overload.i

#
Swig | 352 lines | 280 code | 46 blank | 26 comment | 0 complexity | 9ed8871d0bbdb880f882d2cb07d1f36c MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* File : operator_overload.i */
  2. /*
  3. This is a test of all the possible operator overloads
  4. see bottom for a set of possible tests
  5. */
  6. %module operator_overload
  7. #if defined(SWIGPYTHON)
  8. %warnfilter(SWIGWARN_IGNORE_OPERATOR_EQ,
  9. SWIGWARN_IGNORE_OPERATOR_INDEX,
  10. SWIGWARN_IGNORE_OPERATOR_PLUSPLUS,
  11. SWIGWARN_IGNORE_OPERATOR_MINUSMINUS,
  12. SWIGWARN_IGNORE_OPERATOR_LAND,
  13. SWIGWARN_IGNORE_OPERATOR_LOR);
  14. #endif
  15. #if !defined(SWIGLUA) && !defined(SWIGR)
  16. %rename(Equal) operator =;
  17. %rename(PlusEqual) operator +=;
  18. %rename(MinusEqual) operator -=;
  19. %rename(MultiplyEqual) operator *=;
  20. %rename(DivideEqual) operator /=;
  21. %rename(PercentEqual) operator %=;
  22. %rename(Plus) operator +;
  23. %rename(Minus) operator -;
  24. %rename(Multiply) operator *;
  25. %rename(Divide) operator /;
  26. %rename(Percent) operator %;
  27. %rename(Not) operator !;
  28. %rename(IndexIntoConst) operator[](unsigned idx) const;
  29. %rename(IndexInto) operator[](unsigned idx);
  30. %rename(Functor) operator ();
  31. %rename(EqualEqual) operator ==;
  32. %rename(NotEqual) operator !=;
  33. %rename(LessThan) operator <;
  34. %rename(LessThanEqual) operator <=;
  35. %rename(GreaterThan) operator >;
  36. %rename(GreaterThanEqual) operator >=;
  37. %rename(And) operator &&;
  38. %rename(Or) operator ||;
  39. %rename(PlusPlusPrefix) operator++();
  40. %rename(PlusPlusPostfix) operator++(int);
  41. %rename(MinusMinusPrefix) operator--();
  42. %rename(MinusMinusPostfix) operator--(int);
  43. #endif
  44. #ifdef SWIGCSHARP
  45. %csmethodmodifiers operator++() "protected";
  46. %csmethodmodifiers operator++(int) "private";
  47. %csmethodmodifiers operator--() "private";
  48. %csmethodmodifiers operator--(int) "protected";
  49. %typemap(cscode) Op %{
  50. public static Op operator++(Op op) {
  51. // Unlike C++, operator++ must not modify the parameter and both prefix and postfix operations call this method
  52. Op newOp = new Op(op.i);
  53. newOp.PlusPlusPostfix(0);
  54. return newOp;
  55. }
  56. public static Op operator--(Op op) {
  57. // Unlike C++, operator-- must not modify the parameter and both prefix and postfix operations call this method
  58. Op newOp = new Op(op.i);
  59. newOp.MinusMinusPrefix();
  60. return newOp;
  61. }
  62. %}
  63. #endif
  64. #ifdef SWIGPHP
  65. %rename(AndOperator) operator &&;
  66. %rename(OrOperator) operator ||;
  67. #endif
  68. %rename(IntCast) operator int();
  69. %rename(DoubleCast) operator double();
  70. %inline %{
  71. #if defined(_MSC_VER)
  72. #include <iso646.h> /* for named logical operator, eg 'operator or' */
  73. #endif
  74. #include <assert.h>
  75. class Op {
  76. public:
  77. int i;
  78. Op(int a=0) : i(a)
  79. {}
  80. Op(const Op& o) : i(o.i)
  81. {}
  82. virtual ~Op()
  83. {}
  84. friend Op operator &&(const Op& a,const Op& b){return Op(a.i&&b.i);}
  85. friend Op operator or(const Op& a,const Op& b){return Op(a.i||b.i);}
  86. Op &operator=(const Op& o) {
  87. i=o.i;
  88. return *this;
  89. }
  90. // +=,-=... are member fns
  91. void operator+=(const Op& o){ i+=o.i;}
  92. void operator-=(const Op& o){ i-=o.i;}
  93. void operator*=(const Op& o){ i*=o.i;}
  94. void operator/=(const Op& o){ i/=o.i;}
  95. void operator%=(const Op& o){ i%=o.i;}
  96. // the +,-,*,... are friends
  97. // (just to make life harder)
  98. friend Op operator+(const Op& a,const Op& b){return Op(a.i+b.i);}
  99. friend Op operator-(const Op& a,const Op& b);
  100. friend Op operator*(const Op& a,const Op& b){return Op(a.i*b.i);}
  101. friend Op operator/(const Op& a,const Op& b){return Op(a.i/b.i);}
  102. friend Op operator%(const Op& a,const Op& b){return Op(a.i%b.i);}
  103. // unary operators
  104. Op operator-() const {return Op(-i);}
  105. bool operator !() const {return !(i);}
  106. // overloading the [] operator
  107. // need 2 versions: get & set
  108. // note: C++ can be a little mixed up upon which version it calls
  109. // most of the time it calls the second version
  110. int operator[](unsigned idx)const
  111. { if (idx==0) return i; return 0;}
  112. int& operator[](unsigned idx)
  113. { if (idx==0) return i; static int j;j=0; return j;}
  114. // overloading the () operator
  115. // this can have many parameters so we will test this
  116. int operator()(int a=0){return i+a;}
  117. int operator()(int a,int b){return i+a+b;}
  118. // increment/decrement operators
  119. Op& operator++() {++i; return *this;} // prefix ++
  120. Op operator++(int) {Op o = *this; ++(*this); return o;} // postfix ++
  121. Op& operator--() {--i; return *this;} // prefix --
  122. Op operator--(int) {Op o = *this; --(*this); return o;} // postfix --
  123. // TODO: <<,<<=
  124. // cast operators
  125. operator double() { return i; }
  126. virtual operator int() { return i; }
  127. // This method just checks that the operators are implemented correctly
  128. static void sanity_check();
  129. };
  130. // just to complicate matters
  131. // we have a couple of non class operators
  132. inline bool operator==(const Op& a,const Op& b){return a.i==b.i;}
  133. inline bool operator!=(const Op& a,const Op& b){return a.i!=b.i;}
  134. inline bool operator< (const Op& a,const Op& b){return a.i<b.i;}
  135. inline bool operator<=(const Op& a,const Op& b){return a.i<=b.i;}
  136. inline bool operator> (const Op& a,const Op& b){return a.i>b.i;}
  137. inline bool operator>=(const Op& a,const Op& b){return a.i>=b.i;}
  138. %}
  139. %{
  140. // This one is not declared inline as VC++7.1 gets mixed up with the unary operator-
  141. Op operator-(const Op& a,const Op& b){return Op(a.i-b.i);}
  142. %}
  143. // in order to wrapper this correctly
  144. // we need to extend the class
  145. // to make the friends & non members part of the class
  146. %extend Op{
  147. Op operator &&(const Op& b){return Op($self->i&&b.i);}
  148. Op operator or(const Op& b){return Op($self->i||b.i);}
  149. Op operator+(const Op& b){return Op($self->i+b.i);}
  150. Op operator-(const Op& b){return Op($self->i-b.i);}
  151. Op operator*(const Op& b){return Op($self->i*b.i);}
  152. Op operator/(const Op& b){return Op($self->i/b.i);}
  153. Op operator%(const Op& b){return Op($self->i%b.i);}
  154. bool operator==(const Op& b){return $self->i==b.i;}
  155. bool operator!=(const Op& b){return $self->i!=b.i;}
  156. bool operator< (const Op& b){return $self->i<b.i;}
  157. bool operator<=(const Op& b){return $self->i<=b.i;}
  158. bool operator> (const Op& b){return $self->i>b.i;}
  159. bool operator>=(const Op& b){return $self->i>=b.i;}
  160. // subtraction with reversed arguments
  161. Op __rsub__(const int b){return Op(b - $self->i);}
  162. // we also add the __str__() fn to the class
  163. // this allows it to be converted to a string (so it can be printed)
  164. const char* __str__()
  165. {
  166. static char buffer[255];
  167. sprintf(buffer,"Op(%d)",$self->i);
  168. return buffer;
  169. }
  170. // to get the [] operator working correctly we need to extend with two function
  171. // __getitem__ & __setitem__
  172. int __getitem__(unsigned i)
  173. { return (*$self)[i]; }
  174. void __setitem__(unsigned i,int v)
  175. { (*$self)[i]=v; }
  176. }
  177. /*
  178. Suggested list of operator overloads (mainly from python)
  179. Operators overloaded with their C++ equivalent
  180. __add__,__sub__,__mul__,__div__,__mod__ +,-,*,/,%
  181. __iadd__,__isub__,__imul__,__idiv__,__imod__ +=,-=,*=,/=,%=
  182. __eq__,__ne__,__lt__,__le__,__gt__,__ge__ ==,!=,<,<=,>,>=
  183. __not__,__neg__ unary !, unary -
  184. __and__,__or__,__xor__ logical and,logical or,logical xor
  185. __rshift__,__lshift__ >>,<<
  186. __getitem__,__setitem__ for operator[]
  187. Operators overloaded without C++ equivilents
  188. __pow__ for power operator
  189. __str__ converts object to a string (should return a const char*)
  190. __concat__ for contatenation (if language supports)
  191. */
  192. %inline %{
  193. class OpDerived : public Op {
  194. public:
  195. OpDerived(int a=0) : Op(a)
  196. {}
  197. // overloaded
  198. virtual operator int() { return i*2; }
  199. };
  200. %}
  201. %{
  202. #include <assert.h>
  203. void Op::sanity_check()
  204. {
  205. // test routine:
  206. Op a;
  207. Op b=5;
  208. Op c=b; // copy construct
  209. Op d=2;
  210. Op dd=d; // assignment operator
  211. // test equality
  212. assert(a!=b);
  213. assert(b==c);
  214. assert(a!=d);
  215. assert(d==dd);
  216. // test <
  217. assert(a<b);
  218. assert(a<=b);
  219. assert(b<=c);
  220. assert(b>=c);
  221. assert(b>d);
  222. assert(b>=d);
  223. // test +=
  224. Op e=3;
  225. e+=d;
  226. assert(e==b);
  227. e-=c;
  228. assert(e==a);
  229. e=Op(1);
  230. e*=b;
  231. assert(e==c);
  232. e/=d;
  233. assert(e==d);
  234. e%=c;
  235. assert(e==d);
  236. // test +
  237. Op f(1),g(1);
  238. assert(f+g==Op(2));
  239. assert(f-g==Op(0));
  240. assert(f*g==Op(1));
  241. assert(f/g==Op(1));
  242. assert(f%g==Op(0));
  243. // test unary operators
  244. assert(!a==true);
  245. assert(!b==false);
  246. assert(-a==a);
  247. assert(-b==Op(-5));
  248. // test []
  249. Op h=3;
  250. assert(h[0]==3);
  251. assert(h[1]==0);
  252. h[0]=2; // set
  253. assert(h[0]==2);
  254. h[1]=2; // ignored
  255. assert(h[0]==2);
  256. assert(h[1]==0);
  257. // test ()
  258. Op i=3;
  259. assert(i()==3);
  260. assert(i(1)==4);
  261. assert(i(1,2)==6);
  262. // plus add some code to check the __str__ fn
  263. //assert(str(Op(1))=="Op(1)");
  264. //assert(str(Op(-3))=="Op(-3)");
  265. // test ++ and --
  266. Op j(100);
  267. int original = j.i;
  268. {
  269. Op newOp = j++;
  270. int newInt = original++;
  271. assert(j.i == original);
  272. assert(newOp.i == newInt);
  273. }
  274. {
  275. Op newOp = j--;
  276. int newInt = original--;
  277. assert(j.i == original);
  278. assert(newOp.i == newInt);
  279. }
  280. {
  281. Op newOp = ++j;
  282. int newInt = ++original;
  283. assert(j.i == original);
  284. assert(newOp.i == newInt);
  285. }
  286. {
  287. Op newOp = --j;
  288. int newInt = --original;
  289. assert(j.i == original);
  290. assert(newOp.i == newInt);
  291. }
  292. // cast operators
  293. Op k=3;
  294. int check_k = k;
  295. assert (check_k == 3);
  296. Op l=4;
  297. double check_l = l;
  298. assert (check_l == 4);
  299. }
  300. %}