Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
4.2 KiB

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