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>
14
15 class Vector2 {
16 public:
Vector2(float x,float y)17 Vector2(float x, float y) : x(x), y(y) { print_created(this, toString()); }
Vector2(const Vector2 & v)18 Vector2(const Vector2 &v) : x(v.x), y(v.y) { print_copy_created(this); }
Vector2(Vector2 && v)19 Vector2(Vector2 &&v) : x(v.x), y(v.y) { print_move_created(this); v.x = v.y = 0; }
operator =(const Vector2 & v)20 Vector2 &operator=(const Vector2 &v) { x = v.x; y = v.y; print_copy_assigned(this); return *this; }
operator =(Vector2 && v)21 Vector2 &operator=(Vector2 &&v) { x = v.x; y = v.y; v.x = v.y = 0; print_move_assigned(this); return *this; }
~Vector2()22 ~Vector2() { print_destroyed(this); }
23
toString() const24 std::string toString() const { return "[" + std::to_string(x) + ", " + std::to_string(y) + "]"; }
25
operator -() const26 Vector2 operator-() const { return Vector2(-x, -y); }
operator +(const Vector2 & v) const27 Vector2 operator+(const Vector2 &v) const { return Vector2(x + v.x, y + v.y); }
operator -(const Vector2 & v) const28 Vector2 operator-(const Vector2 &v) const { return Vector2(x - v.x, y - v.y); }
operator -(float value) const29 Vector2 operator-(float value) const { return Vector2(x - value, y - value); }
operator +(float value) const30 Vector2 operator+(float value) const { return Vector2(x + value, y + value); }
operator *(float value) const31 Vector2 operator*(float value) const { return Vector2(x * value, y * value); }
operator /(float value) const32 Vector2 operator/(float value) const { return Vector2(x / value, y / value); }
operator *(const Vector2 & v) const33 Vector2 operator*(const Vector2 &v) const { return Vector2(x * v.x, y * v.y); }
operator /(const Vector2 & v) const34 Vector2 operator/(const Vector2 &v) const { return Vector2(x / v.x, y / v.y); }
operator +=(const Vector2 & v)35 Vector2& operator+=(const Vector2 &v) { x += v.x; y += v.y; return *this; }
operator -=(const Vector2 & v)36 Vector2& operator-=(const Vector2 &v) { x -= v.x; y -= v.y; return *this; }
operator *=(float v)37 Vector2& operator*=(float v) { x *= v; y *= v; return *this; }
operator /=(float v)38 Vector2& operator/=(float v) { x /= v; y /= v; return *this; }
operator *=(const Vector2 & v)39 Vector2& operator*=(const Vector2 &v) { x *= v.x; y *= v.y; return *this; }
operator /=(const Vector2 & v)40 Vector2& operator/=(const Vector2 &v) { x /= v.x; y /= v.y; return *this; }
41
operator +(float f,const Vector2 & v)42 friend Vector2 operator+(float f, const Vector2 &v) { return Vector2(f + v.x, f + v.y); }
operator -(float f,const Vector2 & v)43 friend Vector2 operator-(float f, const Vector2 &v) { return Vector2(f - v.x, f - v.y); }
operator *(float f,const Vector2 & v)44 friend Vector2 operator*(float f, const Vector2 &v) { return Vector2(f * v.x, f * v.y); }
operator /(float f,const Vector2 & v)45 friend Vector2 operator/(float f, const Vector2 &v) { return Vector2(f / v.x, f / v.y); }
46
operator ==(const Vector2 & v) const47 bool operator==(const Vector2 &v) const {
48 return x == v.x && y == v.y;
49 }
operator !=(const Vector2 & v) const50 bool operator!=(const Vector2 &v) const {
51 return x != v.x || y != v.y;
52 }
53 private:
54 float x, y;
55 };
56
57 class C1 { };
58 class C2 { };
59
operator +(const C1 &,const C1 &)60 int operator+(const C1 &, const C1 &) { return 11; }
operator +(const C2 &,const C2 &)61 int operator+(const C2 &, const C2 &) { return 22; }
operator +(const C2 &,const C1 &)62 int operator+(const C2 &, const C1 &) { return 21; }
operator +(const C1 &,const C2 &)63 int operator+(const C1 &, const C2 &) { return 12; }
64
65 // Note: Specializing explicit within `namespace std { ... }` is done due to a
66 // bug in GCC<7. If you are supporting compilers later than this, consider
67 // specializing `using template<> struct std::hash<...>` in the global
68 // namespace instead, per this recommendation:
69 // https://en.cppreference.com/w/cpp/language/extending_std#Adding_template_specializations
70 namespace std {
71 template<>
72 struct hash<Vector2> {
73 // Not a good hash function, but easy to test
operator ()std::hash74 size_t operator()(const Vector2 &) { return 4; }
75 };
76 } // namespace std
77
78 // Not a good abs function, but easy to test.
abs(const Vector2 &)79 std::string abs(const Vector2&) {
80 return "abs(Vector2)";
81 }
82
83 // MSVC warns about unknown pragmas, and warnings are errors.
84 #ifndef _MSC_VER
85 #pragma GCC diagnostic push
86 // clang 7.0.0 and Apple LLVM 10.0.1 introduce `-Wself-assign-overloaded` to
87 // `-Wall`, which is used here for overloading (e.g. `py::self += py::self `).
88 // Here, we suppress the warning using `#pragma diagnostic`.
89 // Taken from: https://github.com/RobotLocomotion/drake/commit/aaf84b46
90 // TODO(eric): This could be resolved using a function / functor (e.g. `py::self()`).
91 #if defined(__APPLE__) && defined(__clang__)
92 #if (__clang_major__ >= 10)
93 #pragma GCC diagnostic ignored "-Wself-assign-overloaded"
94 #endif
95 #elif defined(__clang__)
96 #if (__clang_major__ >= 7)
97 #pragma GCC diagnostic ignored "-Wself-assign-overloaded"
98 #endif
99 #endif
100 #endif
101
TEST_SUBMODULE(operators,m)102 TEST_SUBMODULE(operators, m) {
103
104 // test_operator_overloading
105 py::class_<Vector2>(m, "Vector2")
106 .def(py::init<float, float>())
107 .def(py::self + py::self)
108 .def(py::self + float())
109 .def(py::self - py::self)
110 .def(py::self - float())
111 .def(py::self * float())
112 .def(py::self / float())
113 .def(py::self * py::self)
114 .def(py::self / py::self)
115 .def(py::self += py::self)
116 .def(py::self -= py::self)
117 .def(py::self *= float())
118 .def(py::self /= float())
119 .def(py::self *= py::self)
120 .def(py::self /= py::self)
121 .def(float() + py::self)
122 .def(float() - py::self)
123 .def(float() * py::self)
124 .def(float() / py::self)
125 .def(-py::self)
126 .def("__str__", &Vector2::toString)
127 .def("__repr__", &Vector2::toString)
128 .def(py::self == py::self)
129 .def(py::self != py::self)
130 .def(py::hash(py::self))
131 // N.B. See warning about usage of `py::detail::abs(py::self)` in
132 // `operators.h`.
133 .def("__abs__", [](const Vector2& v) { return abs(v); })
134 ;
135
136 m.attr("Vector") = m.attr("Vector2");
137
138 // test_operators_notimplemented
139 // #393: need to return NotSupported to ensure correct arithmetic operator behavior
140 py::class_<C1>(m, "C1")
141 .def(py::init<>())
142 .def(py::self + py::self);
143
144 py::class_<C2>(m, "C2")
145 .def(py::init<>())
146 .def(py::self + py::self)
147 .def("__add__", [](const C2& c2, const C1& c1) { return c2 + c1; })
148 .def("__radd__", [](const C2& c2, const C1& c1) { return c1 + c2; });
149
150 // test_nested
151 // #328: first member in a class can't be used in operators
152 struct NestABase { int value = -2; };
153 py::class_<NestABase>(m, "NestABase")
154 .def(py::init<>())
155 .def_readwrite("value", &NestABase::value);
156
157 struct NestA : NestABase {
158 int value = 3;
159 NestA& operator+=(int i) { value += i; return *this; }
160 };
161 py::class_<NestA>(m, "NestA")
162 .def(py::init<>())
163 .def(py::self += int())
164 .def("as_base", [](NestA &a) -> NestABase& {
165 return (NestABase&) a;
166 }, py::return_value_policy::reference_internal);
167 m.def("get_NestA", [](const NestA &a) { return a.value; });
168
169 struct NestB {
170 NestA a;
171 int value = 4;
172 NestB& operator-=(int i) { value -= i; return *this; }
173 };
174 py::class_<NestB>(m, "NestB")
175 .def(py::init<>())
176 .def(py::self -= int())
177 .def_readwrite("a", &NestB::a);
178 m.def("get_NestB", [](const NestB &b) { return b.value; });
179
180 struct NestC {
181 NestB b;
182 int value = 5;
183 NestC& operator*=(int i) { value *= i; return *this; }
184 };
185 py::class_<NestC>(m, "NestC")
186 .def(py::init<>())
187 .def(py::self *= int())
188 .def_readwrite("b", &NestC::b);
189 m.def("get_NestC", [](const NestC &c) { return c.value; });
190
191
192 // test_overriding_eq_reset_hash
193 // #2191 Overriding __eq__ should set __hash__ to None
194 struct Comparable {
195 int value;
196 bool operator==(const Comparable& rhs) const {return value == rhs.value;}
197 };
198
199 struct Hashable : Comparable {
200 explicit Hashable(int value): Comparable{value}{};
201 size_t hash() const { return static_cast<size_t>(value); }
202 };
203
204 struct Hashable2 : Hashable {
205 using Hashable::Hashable;
206 };
207
208 py::class_<Comparable>(m, "Comparable")
209 .def(py::init<int>())
210 .def(py::self == py::self);
211
212 py::class_<Hashable>(m, "Hashable")
213 .def(py::init<int>())
214 .def(py::self == py::self)
215 .def("__hash__", &Hashable::hash);
216
217 // define __hash__ before __eq__
218 py::class_<Hashable2>(m, "Hashable2")
219 .def("__hash__", &Hashable::hash)
220 .def(py::init<int>())
221 .def(py::self == py::self);
222 }
223
224 #ifndef _MSC_VER
225 #pragma GCC diagnostic pop
226 #endif
227