Scarab  v2.9.0
Project 8 C++ Utility Library
test_operator_overloading.cpp
Go to the documentation of this file.
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:
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; }
23 
24  std::string toString() const { return "[" + std::to_string(x) + ", " + std::to_string(y) + "]"; }
25 
26  Vector2 operator-() const { return Vector2(-x, -y); }
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) const { return Vector2(x * v.x, y * v.y); }
34  Vector2 operator/(const Vector2 &v) const { return Vector2(x / v.x, y / v.y); }
35  Vector2& operator+=(const Vector2 &v) { x += v.x; y += v.y; return *this; }
36  Vector2& operator-=(const Vector2 &v) { x -= v.x; y -= v.y; return *this; }
37  Vector2& operator*=(float v) { x *= v; y *= v; return *this; }
38  Vector2& operator/=(float v) { x /= v; y /= v; return *this; }
39  Vector2& operator*=(const Vector2 &v) { x *= v.x; y *= v.y; return *this; }
40  Vector2& operator/=(const Vector2 &v) { x /= v.x; y /= v.y; return *this; }
41 
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); }
45  friend Vector2 operator/(float f, const Vector2 &v) { return Vector2(f / v.x, f / v.y); }
46 private:
47  float x, y;
48 };
49 
50 class C1 { };
51 class C2 { };
52 
53 int operator+(const C1 &, const C1 &) { return 11; }
54 int operator+(const C2 &, const C2 &) { return 22; }
55 int operator+(const C2 &, const C1 &) { return 21; }
56 int operator+(const C1 &, const C2 &) { return 12; }
57 
58 namespace std {
59  template<>
60  struct hash<Vector2> {
61  // Not a good hash function, but easy to test
62  size_t operator()(const Vector2 &) { return 4; }
63  };
64 }
65 
66 // MSVC warns about unknown pragmas, and warnings are errors.
67 #ifndef _MSC_VER
68  #pragma GCC diagnostic push
69  // clang 7.0.0 and Apple LLVM 10.0.1 introduce `-Wself-assign-overloaded` to
70  // `-Wall`, which is used here for overloading (e.g. `py::self += py::self `).
71  // Here, we suppress the warning using `#pragma diagnostic`.
72  // Taken from: https://github.com/RobotLocomotion/drake/commit/aaf84b46
73  // TODO(eric): This could be resolved using a function / functor (e.g. `py::self()`).
74  #if (__APPLE__) && (__clang__)
75  #if (__clang_major__ >= 10) && (__clang_minor__ >= 0) && (__clang_patchlevel__ >= 1)
76  #pragma GCC diagnostic ignored "-Wself-assign-overloaded"
77  #endif
78  #elif (__clang__)
79  #if (__clang_major__ >= 7)
80  #pragma GCC diagnostic ignored "-Wself-assign-overloaded"
81  #endif
82  #endif
83 #endif
84 
86 
87  // test_operator_overloading
88  py::class_<Vector2>(m, "Vector2")
89  .def(py::init<float, float>())
90  .def(py::self + py::self)
91  .def(py::self + float())
93  .def(py::self - float())
94  .def(py::self * float())
95  .def(py::self / float())
97  .def(py::self / py::self)
98  .def(py::self += py::self)
99  .def(py::self -= py::self)
100  .def(py::self *= float())
101  .def(py::self /= float())
102  .def(py::self *= py::self)
103  .def(py::self /= py::self)
104  .def(float() + py::self)
105  .def(float() - py::self)
106  .def(float() * py::self)
107  .def(float() / py::self)
108  .def(-py::self)
109  .def("__str__", &Vector2::toString)
110  .def(hash(py::self))
111  ;
112 
113  m.attr("Vector") = m.attr("Vector2");
114 
115  // test_operators_notimplemented
116  // #393: need to return NotSupported to ensure correct arithmetic operator behavior
117  py::class_<C1>(m, "C1")
118  .def(py::init<>())
119  .def(py::self + py::self);
120 
121  py::class_<C2>(m, "C2")
122  .def(py::init<>())
123  .def(py::self + py::self)
124  .def("__add__", [](const C2& c2, const C1& c1) { return c2 + c1; })
125  .def("__radd__", [](const C2& c2, const C1& c1) { return c1 + c2; });
126 
127  // test_nested
128  // #328: first member in a class can't be used in operators
129  struct NestABase { int value = -2; };
130  py::class_<NestABase>(m, "NestABase")
131  .def(py::init<>())
132  .def_readwrite("value", &NestABase::value);
133 
134  struct NestA : NestABase {
135  int value = 3;
136  NestA& operator+=(int i) { value += i; return *this; }
137  };
138  py::class_<NestA>(m, "NestA")
139  .def(py::init<>())
140  .def(py::self += int())
141  .def("as_base", [](NestA &a) -> NestABase& {
142  return (NestABase&) a;
143  }, py::return_value_policy::reference_internal);
144  m.def("get_NestA", [](const NestA &a) { return a.value; });
145 
146  struct NestB {
147  NestA a;
148  int value = 4;
149  NestB& operator-=(int i) { value -= i; return *this; }
150  };
151  py::class_<NestB>(m, "NestB")
152  .def(py::init<>())
153  .def(py::self -= int())
154  .def_readwrite("a", &NestB::a);
155  m.def("get_NestB", [](const NestB &b) { return b.value; });
156 
157  struct NestC {
158  NestB b;
159  int value = 5;
160  NestC& operator*=(int i) { value *= i; return *this; }
161  };
162  py::class_<NestC>(m, "NestC")
163  .def(py::init<>())
164  .def(py::self *= int())
165  .def_readwrite("b", &NestC::b);
166  m.def("get_NestC", [](const NestC &c) { return c.value; });
167 }
168 
169 #ifndef _MSC_VER
170  #pragma GCC diagnostic pop
171 #endif
Vector2 operator/(float value) const
Vector2 operator+(const Vector2 &v) const
Vector2 & operator/=(const Vector2 &v)
friend Vector2 operator/(float f, const Vector2 &v)
Vector2 & operator+=(const Vector2 &v)
static const self_t self
Definition: operators.h:41
Vector2(Vector2 &&v)
void print_destroyed(T *inst, Values &&...values)
Vector2 operator*(const Vector2 &v) const
STL namespace.
void print_copy_assigned(T *inst, Values &&...values)
void print_copy_created(T *inst, Values &&...values)
Vector2 & operator/=(float v)
Vector2 & operator-=(const Vector2 &v)
obj_attr_accessor attr(handle key) const
Definition: pytypes.h:1410
Vector2(const Vector2 &v)
op_< op_hash, op_u, self_t, undefined_t > hash(const self_t &)
Definition: operators.h:151
Vector2 operator-(const Vector2 &v) const
Vector2 operator-() const
void print_move_assigned(T *inst, Values &&...values)
Vector2 & operator=(const Vector2 &v)
Vector2 & operator*=(float v)
size_t operator()(const Vector2 &)
Vector2 operator-(float value) const
test_initializer operators("operators", test_submodule_operators)
Vector2 operator*(float value) const
Vector2 & operator=(Vector2 &&v)
void print_created(T *inst, Values &&...values)
Vector2 operator/(const Vector2 &v) const
Vector2 operator+(float value) const
Vector2(float x, float y)
friend Vector2 operator+(float f, const Vector2 &v)
friend Vector2 operator-(float f, const Vector2 &v)
Vector2 & operator*=(const Vector2 &v)
void print_move_created(T *inst, Values &&...values)
#define TEST_SUBMODULE(name, variable)
friend Vector2 operator*(float f, const Vector2 &v)
bool typename Extra class_ & def(const char *name_, Func &&f, const Extra &... extra)
Definition: pybind11.h:1110
std::string toString() const
auto to_string(T &&value) -> decltype(std::forward< T >(value))
Convert an object to a string (directly forward if this can become a string)
Definition: CLI11.hpp:1028