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.

76 lines
2.8 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) { x += v.x; y += v.y; return *this; }
  35. Vector2& operator-=(const Vector2 &v) { x -= v.x; y -= v.y; return *this; }
  36. Vector2& operator*=(float v) { x *= v; y *= v; return *this; }
  37. Vector2& operator/=(float v) { x /= v; y /= v; return *this; }
  38. friend Vector2 operator+(float f, const Vector2 &v) { return Vector2(f + v.x, f + v.y); }
  39. friend Vector2 operator-(float f, const Vector2 &v) { return Vector2(f - v.x, f - v.y); }
  40. friend Vector2 operator*(float f, const Vector2 &v) { return Vector2(f * v.x, f * v.y); }
  41. friend Vector2 operator/(float f, const Vector2 &v) { return Vector2(f / v.x, f / v.y); }
  42. private:
  43. float x, y;
  44. };
  45. test_initializer operator_overloading([](py::module &m) {
  46. py::class_<Vector2>(m, "Vector2")
  47. .def(py::init<float, float>())
  48. .def(py::self + py::self)
  49. .def(py::self + float())
  50. .def(py::self - py::self)
  51. .def(py::self - float())
  52. .def(py::self * float())
  53. .def(py::self / float())
  54. .def(py::self += py::self)
  55. .def(py::self -= py::self)
  56. .def(py::self *= float())
  57. .def(py::self /= float())
  58. .def(float() + py::self)
  59. .def(float() - py::self)
  60. .def(float() * py::self)
  61. .def(float() / py::self)
  62. .def("__str__", &Vector2::toString)
  63. ;
  64. m.attr("Vector") = m.attr("Vector2");
  65. });