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.

152 lines
5.2 KiB

  1. /*
  2. tests/test_operator_overloading.cpp -- operator overloading
  3. Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
  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. class Vector2 {
  11. public:
  12. Vector2(float x, float y) : x(x), y(y) { print_created(this, toString()); }
  13. Vector2(const Vector2 &v) : x(v.x), y(v.y) { print_copy_created(this); }
  14. Vector2(Vector2 &&v) : x(v.x), y(v.y) { print_move_created(this); v.x = v.y = 0; }
  15. ~Vector2() { print_destroyed(this); }
  16. std::string toString() const {
  17. return "[" + std::to_string(x) + ", " + std::to_string(y) + "]";
  18. }
  19. void operator=(const Vector2 &v) {
  20. print_copy_assigned(this);
  21. x = v.x;
  22. y = v.y;
  23. }
  24. void operator=(Vector2 &&v) {
  25. print_move_assigned(this);
  26. x = v.x; y = v.y; v.x = v.y = 0;
  27. }
  28. Vector2 operator+(const Vector2 &v) const { return Vector2(x + v.x, y + v.y); }
  29. Vector2 operator-(const Vector2 &v) const { return Vector2(x - v.x, y - v.y); }
  30. Vector2 operator-(float value) const { return Vector2(x - value, y - value); }
  31. Vector2 operator+(float value) const { return Vector2(x + value, y + value); }
  32. Vector2 operator*(float value) const { return Vector2(x * value, y * value); }
  33. Vector2 operator/(float value) const { return Vector2(x / value, y / value); }
  34. Vector2 operator*(const Vector2 &v) const { return Vector2(x * v.x, y * v.y); }
  35. Vector2 operator/(const Vector2 &v) const { return Vector2(x / v.x, y / v.y); }
  36. Vector2& operator+=(const Vector2 &v) { x += v.x; y += v.y; return *this; }
  37. Vector2& operator-=(const Vector2 &v) { x -= v.x; y -= v.y; return *this; }
  38. Vector2& operator*=(float v) { x *= v; y *= v; return *this; }
  39. Vector2& operator/=(float v) { x /= v; y /= v; return *this; }
  40. Vector2& operator*=(const Vector2 &v) { x *= v.x; y *= v.y; return *this; }
  41. Vector2& operator/=(const Vector2 &v) { x /= v.x; y /= v.y; return *this; }
  42. friend Vector2 operator+(float f, const Vector2 &v) { return Vector2(f + v.x, f + v.y); }
  43. friend Vector2 operator-(float f, const Vector2 &v) { return Vector2(f - v.x, f - v.y); }
  44. friend Vector2 operator*(float f, const Vector2 &v) { return Vector2(f * v.x, f * v.y); }
  45. friend Vector2 operator/(float f, const Vector2 &v) { return Vector2(f / v.x, f / v.y); }
  46. private:
  47. float x, y;
  48. };
  49. class C1 { };
  50. class C2 { };
  51. int operator+(const C1 &, const C1 &) { return 11; }
  52. int operator+(const C2 &, const C2 &) { return 22; }
  53. int operator+(const C2 &, const C1 &) { return 21; }
  54. int operator+(const C1 &, const C2 &) { return 12; }
  55. struct NestABase {
  56. int value = -2;
  57. };
  58. struct NestA : NestABase {
  59. int value = 3;
  60. NestA& operator+=(int i) { value += i; return *this; }
  61. };
  62. struct NestB {
  63. NestA a;
  64. int value = 4;
  65. NestB& operator-=(int i) { value -= i; return *this; }
  66. };
  67. struct NestC {
  68. NestB b;
  69. int value = 5;
  70. NestC& operator*=(int i) { value *= i; return *this; }
  71. };
  72. test_initializer operator_overloading([](py::module &pm) {
  73. auto m = pm.def_submodule("operators");
  74. py::class_<Vector2>(m, "Vector2")
  75. .def(py::init<float, float>())
  76. .def(py::self + py::self)
  77. .def(py::self + float())
  78. .def(py::self - py::self)
  79. .def(py::self - float())
  80. .def(py::self * float())
  81. .def(py::self / float())
  82. .def(py::self * py::self)
  83. .def(py::self / py::self)
  84. .def(py::self += py::self)
  85. .def(py::self -= py::self)
  86. .def(py::self *= float())
  87. .def(py::self /= float())
  88. .def(py::self *= py::self)
  89. .def(py::self /= py::self)
  90. .def(float() + py::self)
  91. .def(float() - py::self)
  92. .def(float() * py::self)
  93. .def(float() / py::self)
  94. .def("__str__", &Vector2::toString)
  95. ;
  96. m.attr("Vector") = m.attr("Vector2");
  97. // #393: need to return NotSupported to ensure correct arithmetic operator behavior
  98. py::class_<C1>(m, "C1")
  99. .def(py::init<>())
  100. .def(py::self + py::self);
  101. py::class_<C2>(m, "C2")
  102. .def(py::init<>())
  103. .def(py::self + py::self)
  104. .def("__add__", [](const C2& c2, const C1& c1) { return c2 + c1; })
  105. .def("__radd__", [](const C2& c2, const C1& c1) { return c1 + c2; });
  106. // #328: first member in a class can't be used in operators
  107. py::class_<NestABase>(m, "NestABase")
  108. .def(py::init<>())
  109. .def_readwrite("value", &NestABase::value);
  110. py::class_<NestA>(m, "NestA")
  111. .def(py::init<>())
  112. .def(py::self += int())
  113. .def("as_base", [](NestA &a) -> NestABase& {
  114. return (NestABase&) a;
  115. }, py::return_value_policy::reference_internal);
  116. py::class_<NestB>(m, "NestB")
  117. .def(py::init<>())
  118. .def(py::self -= int())
  119. .def_readwrite("a", &NestB::a);
  120. py::class_<NestC>(m, "NestC")
  121. .def(py::init<>())
  122. .def(py::self *= int())
  123. .def_readwrite("b", &NestC::b);
  124. m.def("get_NestA", [](const NestA &a) { return a.value; });
  125. m.def("get_NestB", [](const NestB &b) { return b.value; });
  126. m.def("get_NestC", [](const NestC &c) { return c.value; });
  127. });