blob: 3b3a6253e0fe1966575d6a18b986fab6934a0c78 [file] [log] [blame]
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001/*
Wenzel Jakoba576e6a2015-07-29 17:51:54 +02002 example/example3.cpp -- operator overloading
Wenzel Jakob38bd7112015-07-05 20:05:44 +02003
4 Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch>
5
6 All rights reserved. Use of this source code is governed by a
7 BSD-style license that can be found in the LICENSE file.
8*/
9
10#include "example.h"
Wenzel Jakob8f4eb002015-10-15 18:13:33 +020011#include <pybind11/operators.h>
Wenzel Jakob38bd7112015-07-05 20:05:44 +020012
13class Vector2 {
14public:
15 Vector2(float x, float y) : x(x), y(y) { std::cout << "Value constructor" << std::endl; }
16 Vector2(const Vector2 &v) : x(v.x), y(v.y) { std::cout << "Copy constructor" << std::endl; }
17 Vector2(Vector2 &&v) : x(v.x), y(v.y) { std::cout << "Move constructor" << std::endl; v.x = v.y = 0; }
18 ~Vector2() { std::cout << "Destructor." << std::endl; }
19
20 std::string toString() const {
21 return "[" + std::to_string(x) + ", " + std::to_string(y) + "]";
22 }
23
24 void operator=(const Vector2 &v) {
25 cout << "Assignment operator" << endl;
26 x = v.x;
27 y = v.y;
28 }
29
30 void operator=(Vector2 &&v) {
31 cout << "Move assignment operator" << endl;
32 x = v.x; y = v.y; v.x = v.y = 0;
33 }
34
35 Vector2 operator+(const Vector2 &v) const { return Vector2(x + v.x, y + v.y); }
36 Vector2 operator-(const Vector2 &v) const { return Vector2(x - v.x, y - v.y); }
37 Vector2 operator-(float value) const { return Vector2(x - value, y - value); }
38 Vector2 operator+(float value) const { return Vector2(x + value, y + value); }
39 Vector2 operator*(float value) const { return Vector2(x * value, y * value); }
40 Vector2 operator/(float value) const { return Vector2(x / value, y / value); }
41 Vector2& operator+=(const Vector2 &v) { x += v.x; y += v.y; return *this; }
42 Vector2& operator-=(const Vector2 &v) { x -= v.x; y -= v.y; return *this; }
43 Vector2& operator*=(float v) { x *= v; y *= v; return *this; }
44 Vector2& operator/=(float v) { x /= v; y /= v; return *this; }
Wenzel Jakob73a50a02015-09-11 17:09:47 +020045
46 friend Vector2 operator+(float f, const Vector2 &v) { return Vector2(f + v.x, f + v.y); }
47 friend Vector2 operator-(float f, const Vector2 &v) { return Vector2(f - v.x, f - v.y); }
48 friend Vector2 operator*(float f, const Vector2 &v) { return Vector2(f * v.x, f * v.y); }
49 friend Vector2 operator/(float f, const Vector2 &v) { return Vector2(f / v.x, f / v.y); }
Wenzel Jakob38bd7112015-07-05 20:05:44 +020050private:
51 float x, y;
52};
53
Wenzel Jakob73a50a02015-09-11 17:09:47 +020054
Wenzel Jakob38bd7112015-07-05 20:05:44 +020055void init_ex3(py::module &m) {
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 *= float())
67 .def(py::self /= float())
Wenzel Jakob73a50a02015-09-11 17:09:47 +020068 .def(float() + py::self)
69 .def(float() - py::self)
70 .def(float() * py::self)
71 .def(float() / py::self)
Wenzel Jakob38bd7112015-07-05 20:05:44 +020072 .def("__str__", &Vector2::toString);
73
74 m.attr("Vector") = m.attr("Vector2");
75}