test_operator_overloading.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. tests/test_operator_overloading.cpp -- operator overloading
  3. Copyright (c) 2016 Wenzel Jakob <[email protected]>
  4. All rights reserved. Use of this source code is governed by a
  5. BSD-style license that can be found in the LICENSE file.
  6. */
  7. #include "pybind11_tests.h"
  8. #include "constructor_stats.h"
  9. #include <pybind11/operators.h>
  10. #include <functional>
  11. class Vector2 {
  12. public:
  13. Vector2(float x, float y) : x(x), y(y) { print_created(this, toString()); }
  14. Vector2(const Vector2 &v) : x(v.x), y(v.y) { print_copy_created(this); }
  15. Vector2(Vector2 &&v) : x(v.x), y(v.y) { print_move_created(this); v.x = v.y = 0; }
  16. Vector2 &operator=(const Vector2 &v) { x = v.x; y = v.y; print_copy_assigned(this); return *this; }
  17. Vector2 &operator=(Vector2 &&v) { x = v.x; y = v.y; v.x = v.y = 0; print_move_assigned(this); return *this; }
  18. ~Vector2() { print_destroyed(this); }
  19. std::string toString() const { return "[" + std::to_string(x) + ", " + std::to_string(y) + "]"; }
  20. Vector2 operator+(const Vector2 &v) const { return Vector2(x + v.x, y + v.y); }
  21. Vector2 operator-(const Vector2 &v) const { return Vector2(x - v.x, y - v.y); }
  22. Vector2 operator-(float value) const { return Vector2(x - value, y - value); }
  23. Vector2 operator+(float value) const { return Vector2(x + value, y + value); }
  24. Vector2 operator*(float value) const { return Vector2(x * value, y * value); }
  25. Vector2 operator/(float value) const { return Vector2(x / value, y / value); }
  26. Vector2 operator*(const Vector2 &v) const { return Vector2(x * v.x, y * v.y); }
  27. Vector2 operator/(const Vector2 &v) const { return Vector2(x / v.x, y / v.y); }
  28. Vector2& operator+=(const Vector2 &v) { x += v.x; y += v.y; return *this; }
  29. Vector2& operator-=(const Vector2 &v) { x -= v.x; y -= v.y; return *this; }
  30. Vector2& operator*=(float v) { x *= v; y *= v; return *this; }
  31. Vector2& operator/=(float v) { x /= v; y /= v; return *this; }
  32. Vector2& operator*=(const Vector2 &v) { x *= v.x; y *= v.y; return *this; }
  33. Vector2& operator/=(const Vector2 &v) { x /= v.x; y /= v.y; return *this; }
  34. friend Vector2 operator+(float f, const Vector2 &v) { return Vector2(f + v.x, f + v.y); }
  35. friend Vector2 operator-(float f, const Vector2 &v) { return Vector2(f - v.x, f - v.y); }
  36. friend Vector2 operator*(float f, const Vector2 &v) { return Vector2(f * v.x, f * v.y); }
  37. friend Vector2 operator/(float f, const Vector2 &v) { return Vector2(f / v.x, f / v.y); }
  38. private:
  39. float x, y;
  40. };
  41. class C1 { };
  42. class C2 { };
  43. int operator+(const C1 &, const C1 &) { return 11; }
  44. int operator+(const C2 &, const C2 &) { return 22; }
  45. int operator+(const C2 &, const C1 &) { return 21; }
  46. int operator+(const C1 &, const C2 &) { return 12; }
  47. namespace std {
  48. template<>
  49. struct hash<Vector2> {
  50. // Not a good hash function, but easy to test
  51. size_t operator()(const Vector2 &) { return 4; }
  52. };
  53. }
  54. TEST_SUBMODULE(operators, m) {
  55. // test_operator_overloading
  56. py::class_<Vector2>(m, "Vector2")
  57. .def(py::init<float, float>())
  58. .def(py::self + py::self)
  59. .def(py::self + float())
  60. .def(py::self - py::self)
  61. .def(py::self - float())
  62. .def(py::self * float())
  63. .def(py::self / float())
  64. .def(py::self * py::self)
  65. .def(py::self / py::self)
  66. .def(py::self += py::self)
  67. .def(py::self -= py::self)
  68. .def(py::self *= float())
  69. .def(py::self /= float())
  70. .def(py::self *= py::self)
  71. .def(py::self /= py::self)
  72. .def(float() + py::self)
  73. .def(float() - py::self)
  74. .def(float() * py::self)
  75. .def(float() / py::self)
  76. .def("__str__", &Vector2::toString)
  77. .def(hash(py::self))
  78. ;
  79. m.attr("Vector") = m.attr("Vector2");
  80. // test_operators_notimplemented
  81. // #393: need to return NotSupported to ensure correct arithmetic operator behavior
  82. py::class_<C1>(m, "C1")
  83. .def(py::init<>())
  84. .def(py::self + py::self);
  85. py::class_<C2>(m, "C2")
  86. .def(py::init<>())
  87. .def(py::self + py::self)
  88. .def("__add__", [](const C2& c2, const C1& c1) { return c2 + c1; })
  89. .def("__radd__", [](const C2& c2, const C1& c1) { return c1 + c2; });
  90. // test_nested
  91. // #328: first member in a class can't be used in operators
  92. struct NestABase { int value = -2; };
  93. py::class_<NestABase>(m, "NestABase")
  94. .def(py::init<>())
  95. .def_readwrite("value", &NestABase::value);
  96. struct NestA : NestABase {
  97. int value = 3;
  98. NestA& operator+=(int i) { value += i; return *this; }
  99. };
  100. py::class_<NestA>(m, "NestA")
  101. .def(py::init<>())
  102. .def(py::self += int())
  103. .def("as_base", [](NestA &a) -> NestABase& {
  104. return (NestABase&) a;
  105. }, py::return_value_policy::reference_internal);
  106. m.def("get_NestA", [](const NestA &a) { return a.value; });
  107. struct NestB {
  108. NestA a;
  109. int value = 4;
  110. NestB& operator-=(int i) { value -= i; return *this; }
  111. };
  112. py::class_<NestB>(m, "NestB")
  113. .def(py::init<>())
  114. .def(py::self -= int())
  115. .def_readwrite("a", &NestB::a);
  116. m.def("get_NestB", [](const NestB &b) { return b.value; });
  117. struct NestC {
  118. NestB b;
  119. int value = 5;
  120. NestC& operator*=(int i) { value *= i; return *this; }
  121. };
  122. py::class_<NestC>(m, "NestC")
  123. .def(py::init<>())
  124. .def(py::self *= int())
  125. .def_readwrite("b", &NestC::b);
  126. m.def("get_NestC", [](const NestC &c) { return c.value; });
  127. }