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