blob: 4e868d939b74de78907eace85de146b784499987 [file] [log] [blame]
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001/*
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02002 tests/test_operator_overloading.cpp -- operator overloading
Wenzel Jakob38bd7112015-07-05 20:05:44 +02003
Wenzel Jakob8cb6cb32016-04-17 20:21:41 +02004 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
Wenzel Jakob38bd7112015-07-05 20:05:44 +02005
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
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020010#include "pybind11_tests.h"
11#include "constructor_stats.h"
Wenzel Jakob8f4eb002015-10-15 18:13:33 +020012#include <pybind11/operators.h>
Wenzel Jakob38bd7112015-07-05 20:05:44 +020013
14class Vector2 {
15public:
Jason Rhinelander3f589372016-08-07 13:05:26 -040016 Vector2(float x, float y) : x(x), y(y) { print_created(this, toString()); }
17 Vector2(const Vector2 &v) : x(v.x), y(v.y) { print_copy_created(this); }
18 Vector2(Vector2 &&v) : x(v.x), y(v.y) { print_move_created(this); v.x = v.y = 0; }
19 ~Vector2() { print_destroyed(this); }
Wenzel Jakob38bd7112015-07-05 20:05:44 +020020
21 std::string toString() const {
22 return "[" + std::to_string(x) + ", " + std::to_string(y) + "]";
23 }
24
25 void operator=(const Vector2 &v) {
Jason Rhinelander3f589372016-08-07 13:05:26 -040026 print_copy_assigned(this);
Wenzel Jakob38bd7112015-07-05 20:05:44 +020027 x = v.x;
28 y = v.y;
29 }
30
31 void operator=(Vector2 &&v) {
Jason Rhinelander3f589372016-08-07 13:05:26 -040032 print_move_assigned(this);
Wenzel Jakob38bd7112015-07-05 20:05:44 +020033 x = v.x; y = v.y; v.x = v.y = 0;
34 }
35
36 Vector2 operator+(const Vector2 &v) const { return Vector2(x + v.x, y + v.y); }
37 Vector2 operator-(const Vector2 &v) const { return Vector2(x - v.x, y - v.y); }
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/(float value) const { return Vector2(x / value, y / value); }
Jason Rhinelanderacad05c2017-05-20 20:19:26 -040042 Vector2 operator*(const Vector2 &v) const { return Vector2(x * v.x, y * v.y); }
43 Vector2 operator/(const Vector2 &v) const { return Vector2(x / v.x, y / v.y); }
Wenzel Jakob38bd7112015-07-05 20:05:44 +020044 Vector2& operator+=(const Vector2 &v) { x += v.x; y += v.y; return *this; }
45 Vector2& operator-=(const Vector2 &v) { x -= v.x; y -= v.y; return *this; }
46 Vector2& operator*=(float v) { x *= v; y *= v; return *this; }
47 Vector2& operator/=(float v) { x /= v; y /= v; return *this; }
Jason Rhinelanderacad05c2017-05-20 20:19:26 -040048 Vector2& operator*=(const Vector2 &v) { x *= v.x; y *= v.y; return *this; }
49 Vector2& operator/=(const Vector2 &v) { x /= v.x; y /= v.y; return *this; }
Wenzel Jakob73a50a02015-09-11 17:09:47 +020050
51 friend Vector2 operator+(float f, const Vector2 &v) { return Vector2(f + v.x, f + v.y); }
52 friend Vector2 operator-(float f, const Vector2 &v) { return Vector2(f - v.x, f - v.y); }
53 friend Vector2 operator*(float f, const Vector2 &v) { return Vector2(f * v.x, f * v.y); }
54 friend Vector2 operator/(float f, const Vector2 &v) { return Vector2(f / v.x, f / v.y); }
Wenzel Jakob38bd7112015-07-05 20:05:44 +020055private:
56 float x, y;
57};
58
Jason Rhinelander52f4be82016-09-03 14:54:22 -040059test_initializer operator_overloading([](py::module &m) {
Wenzel Jakob38bd7112015-07-05 20:05:44 +020060 py::class_<Vector2>(m, "Vector2")
61 .def(py::init<float, float>())
62 .def(py::self + py::self)
63 .def(py::self + float())
64 .def(py::self - py::self)
65 .def(py::self - float())
66 .def(py::self * float())
67 .def(py::self / float())
Jason Rhinelanderacad05c2017-05-20 20:19:26 -040068 .def(py::self * py::self)
69 .def(py::self / py::self)
Wenzel Jakob38bd7112015-07-05 20:05:44 +020070 .def(py::self += py::self)
71 .def(py::self -= py::self)
72 .def(py::self *= float())
73 .def(py::self /= float())
Jason Rhinelanderacad05c2017-05-20 20:19:26 -040074 .def(py::self *= py::self)
75 .def(py::self /= py::self)
Wenzel Jakob73a50a02015-09-11 17:09:47 +020076 .def(float() + py::self)
77 .def(float() - py::self)
78 .def(float() * py::self)
79 .def(float() / py::self)
Jason Rhinelander3f589372016-08-07 13:05:26 -040080 .def("__str__", &Vector2::toString)
81 ;
Wenzel Jakob38bd7112015-07-05 20:05:44 +020082
83 m.attr("Vector") = m.attr("Vector2");
Jason Rhinelander52f4be82016-09-03 14:54:22 -040084});