1 /*
2 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
3
4 All rights reserved. Use of this source code is governed by a
5 BSD-style license that can be found in the LICENSE file.
6 */
7
8
9
10
11 #pragma once
12
13 #include "pybind11.h"
14
15 #if defined(__clang__) && !defined(__INTEL_COMPILER)
16 # pragma clang diagnostic ignored "-Wunsequenced"
17 #elif defined(_MSC_VER)
18 # pragma warning(push)
19 # pragma warning(disable: 4127)
20 #endif
21
22 NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
23 NAMESPACE_BEGIN(detail)
24
25
26 enum op_id : int {
27 op_add, op_sub, op_mul, op_div, op_mod, op_divmod, op_pow, op_lshift,
28 op_rshift, op_and, op_xor, op_or, op_neg, op_pos, op_abs, op_invert,
29 op_int, op_long, op_float, op_str, op_cmp, op_gt, op_ge, op_lt, op_le,
30 op_eq, op_ne, op_iadd, op_isub, op_imul, op_idiv, op_imod, op_ilshift,
31 op_irshift, op_iand, op_ixor, op_ior, op_complex, op_bool, op_nonzero,
32 op_repr, op_truediv, op_itruediv, op_hash
33 };
34
35 enum op_type : int {
36 op_l,
37 op_r,
38 op_u
39 };
40
41 struct self_t { };
42 static const self_t self = self_t();
43
44
45 struct undefined_t { };
46
47
__self()48 inline self_t __self() { return self; }
49
50
51 template <op_id, op_type, typename B, typename L, typename R> struct op_impl { };
52
53
54 template <op_id id, op_type ot, typename L, typename R> struct op_ {
executeop_55 template <typename Class, typename... Extra> void execute(Class &cl, const Extra&... extra) const {
56 using Base = typename Class::type;
57 using L_type = conditional_t<std::is_same<L, self_t>::value, Base, L>;
58 using R_type = conditional_t<std::is_same<R, self_t>::value, Base, R>;
59 using op = op_impl<id, ot, Base, L_type, R_type>;
60 cl.def(op::name(), &op::execute, is_operator(), extra...);
61 #if PY_MAJOR_VERSION < 3
62 if (id == op_truediv || id == op_itruediv)
63 cl.def(id == op_itruediv ? "__idiv__" : ot == op_l ? "__div__" : "__rdiv__",
64 &op::execute, is_operator(), extra...);
65 #endif
66 }
execute_castop_67 template <typename Class, typename... Extra> void execute_cast(Class &cl, const Extra&... extra) const {
68 using Base = typename Class::type;
69 using L_type = conditional_t<std::is_same<L, self_t>::value, Base, L>;
70 using R_type = conditional_t<std::is_same<R, self_t>::value, Base, R>;
71 using op = op_impl<id, ot, Base, L_type, R_type>;
72 cl.def(op::name(), &op::execute_cast, is_operator(), extra...);
73 #if PY_MAJOR_VERSION < 3
74 if (id == op_truediv || id == op_itruediv)
75 cl.def(id == op_itruediv ? "__idiv__" : ot == op_l ? "__div__" : "__rdiv__",
76 &op::execute, is_operator(), extra...);
77 #endif
78 }
79 };
80
81 #define PYBIND11_BINARY_OPERATOR(id, rid, op, expr) \
82 template <typename B, typename L, typename R> struct op_impl<op_##id, op_l, B, L, R> { \
83 static char const* name() { return "__" #id "__"; } \
84 static auto execute(const L &l, const R &r) -> decltype(expr) { return (expr); } \
85 static B execute_cast(const L &l, const R &r) { return B(expr); } \
86 }; \
87 template <typename B, typename L, typename R> struct op_impl<op_##id, op_r, B, L, R> { \
88 static char const* name() { return "__" #rid "__"; } \
89 static auto execute(const R &r, const L &l) -> decltype(expr) { return (expr); } \
90 static B execute_cast(const R &r, const L &l) { return B(expr); } \
91 }; \
92 inline op_<op_##id, op_l, self_t, self_t> op(const self_t &, const self_t &) { \
93 return op_<op_##id, op_l, self_t, self_t>(); \
94 } \
95 template <typename T> op_<op_##id, op_l, self_t, T> op(const self_t &, const T &) { \
96 return op_<op_##id, op_l, self_t, T>(); \
97 } \
98 template <typename T> op_<op_##id, op_r, T, self_t> op(const T &, const self_t &) { \
99 return op_<op_##id, op_r, T, self_t>(); \
100 }
101
102 #define PYBIND11_INPLACE_OPERATOR(id, op, expr) \
103 template <typename B, typename L, typename R> struct op_impl<op_##id, op_l, B, L, R> { \
104 static char const* name() { return "__" #id "__"; } \
105 static auto execute(L &l, const R &r) -> decltype(expr) { return expr; } \
106 static B execute_cast(L &l, const R &r) { return B(expr); } \
107 }; \
108 template <typename T> op_<op_##id, op_l, self_t, T> op(const self_t &, const T &) { \
109 return op_<op_##id, op_l, self_t, T>(); \
110 }
111
112 #define PYBIND11_UNARY_OPERATOR(id, op, expr) \
113 template <typename B, typename L> struct op_impl<op_##id, op_u, B, L, undefined_t> { \
114 static char const* name() { return "__" #id "__"; } \
115 static auto execute(const L &l) -> decltype(expr) { return expr; } \
116 static B execute_cast(const L &l) { return B(expr); } \
117 }; \
118 inline op_<op_##id, op_u, self_t, undefined_t> op(const self_t &) { \
119 return op_<op_##id, op_u, self_t, undefined_t>(); \
120 }
121
122 PYBIND11_BINARY_OPERATOR(sub, rsub, operator-, l - r)
123 PYBIND11_BINARY_OPERATOR(add, radd, operator+, l + r)
124 PYBIND11_BINARY_OPERATOR(mul, rmul, operator*, l * r)
125 PYBIND11_BINARY_OPERATOR(truediv, rtruediv, operator/, l / r)
126 PYBIND11_BINARY_OPERATOR(mod, rmod, operator%, l % r)
127 PYBIND11_BINARY_OPERATOR(lshift, rlshift, operator<<, l << r)
128 PYBIND11_BINARY_OPERATOR(rshift, rrshift, operator>>, l >> r)
129 PYBIND11_BINARY_OPERATOR(and, rand, operator&, l & r)
130 PYBIND11_BINARY_OPERATOR(xor, rxor, operator^, l ^ r)
131 PYBIND11_BINARY_OPERATOR(eq, eq, operator==, l == r)
132 PYBIND11_BINARY_OPERATOR(ne, ne, operator!=, l != r)
133 PYBIND11_BINARY_OPERATOR(or, ror, operator|, l | r)
134 PYBIND11_BINARY_OPERATOR(gt, lt, operator>, l > r)
135 PYBIND11_BINARY_OPERATOR(ge, le, operator>=, l >= r)
136 PYBIND11_BINARY_OPERATOR(lt, gt, operator<, l < r)
137 PYBIND11_BINARY_OPERATOR(le, ge, operator<=, l <= r)
138
139 PYBIND11_INPLACE_OPERATOR(iadd, operator+=, l += r)
140 PYBIND11_INPLACE_OPERATOR(isub, operator-=, l -= r)
141 PYBIND11_INPLACE_OPERATOR(imul, operator*=, l *= r)
142 PYBIND11_INPLACE_OPERATOR(itruediv, operator/=, l /= r)
143 PYBIND11_INPLACE_OPERATOR(imod, operator%=, l %= r)
144 PYBIND11_INPLACE_OPERATOR(ilshift, operator<<=, l <<= r)
145 PYBIND11_INPLACE_OPERATOR(irshift, operator>>=, l >>= r)
146 PYBIND11_INPLACE_OPERATOR(iand, operator&=, l &= r)
147 PYBIND11_INPLACE_OPERATOR(ixor, operator^=, l ^= r)
148 PYBIND11_INPLACE_OPERATOR(ior, operator|=, l |= r)
149 PYBIND11_UNARY_OPERATOR(neg, operator-, -l)
150 PYBIND11_UNARY_OPERATOR(pos, operator+, +l)
151 PYBIND11_UNARY_OPERATOR(abs, abs, std::abs(l))
152 PYBIND11_UNARY_OPERATOR(hash, hash, std::hash<L>()(l))
153 PYBIND11_UNARY_OPERATOR(invert, operator~, (~l))
154 PYBIND11_UNARY_OPERATOR(bool, operator!, !!l)
155 PYBIND11_UNARY_OPERATOR(int, int_, (int) l)
156 PYBIND11_UNARY_OPERATOR(float, float_, (double) l)
157
158 #undef PYBIND11_BINARY_OPERATOR
159 #undef PYBIND11_INPLACE_OPERATOR
160 #undef PYBIND11_UNARY_OPERATOR
161 NAMESPACE_END(detail)
162
163 using detail::self;
164
165 NAMESPACE_END(PYBIND11_NAMESPACE)
166
167 #if defined(_MSC_VER)
168 # pragma warning(pop)
169 #endif
170