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.

75 lines
2.9 KiB

  1. /*
  2. example/example3.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 "example.h"
  8. #include <pybind11/operators.h>
  9. class Vector2 {
  10. public:
  11. Vector2(float x, float y) : x(x), y(y) { std::cout << "Value constructor" << std::endl; }
  12. Vector2(const Vector2 &v) : x(v.x), y(v.y) { std::cout << "Copy constructor" << std::endl; }
  13. Vector2(Vector2 &&v) : x(v.x), y(v.y) { std::cout << "Move constructor" << std::endl; v.x = v.y = 0; }
  14. ~Vector2() { std::cout << "Destructor." << std::endl; }
  15. std::string toString() const {
  16. return "[" + std::to_string(x) + ", " + std::to_string(y) + "]";
  17. }
  18. void operator=(const Vector2 &v) {
  19. cout << "Assignment operator" << endl;
  20. x = v.x;
  21. y = v.y;
  22. }
  23. void operator=(Vector2 &&v) {
  24. cout << "Move assignment operator" << endl;
  25. x = v.x; y = v.y; v.x = v.y = 0;
  26. }
  27. Vector2 operator+(const Vector2 &v) const { return Vector2(x + v.x, y + v.y); }
  28. Vector2 operator-(const Vector2 &v) const { return Vector2(x - v.x, y - v.y); }
  29. Vector2 operator-(float value) const { return Vector2(x - value, y - value); }
  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+=(const Vector2 &v) { x += v.x; y += v.y; return *this; }
  34. Vector2& operator-=(const Vector2 &v) { x -= v.x; y -= v.y; return *this; }
  35. Vector2& operator*=(float v) { x *= v; y *= v; return *this; }
  36. Vector2& operator/=(float v) { x /= v; y /= v; return *this; }
  37. friend Vector2 operator+(float f, const Vector2 &v) { return Vector2(f + v.x, f + v.y); }
  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. private:
  42. float x, y;
  43. };
  44. void init_ex3(py::module &m) {
  45. py::class_<Vector2>(m, "Vector2")
  46. .def(py::init<float, float>())
  47. .def(py::self + py::self)
  48. .def(py::self + float())
  49. .def(py::self - py::self)
  50. .def(py::self - float())
  51. .def(py::self * float())
  52. .def(py::self / float())
  53. .def(py::self += py::self)
  54. .def(py::self -= py::self)
  55. .def(py::self *= float())
  56. .def(py::self /= float())
  57. .def(float() + py::self)
  58. .def(float() - py::self)
  59. .def(float() * py::self)
  60. .def(float() / py::self)
  61. .def("__str__", &Vector2::toString);
  62. m.attr("Vector") = m.attr("Vector2");
  63. }