Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1 | /* |
| 2 | pybind/operator.h: Metatemplates for operator overloading |
| 3 | |
| 4 | Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.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 | |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 10 | #pragma once |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 11 | |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 12 | #include <pybind/pybind.h> |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 13 | #include <type_traits> |
| 14 | |
| 15 | NAMESPACE_BEGIN(pybind) |
| 16 | NAMESPACE_BEGIN(detail) |
| 17 | |
| 18 | /// Enumeration with all supported operator types |
| 19 | enum op_id : int { |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 20 | op_add, op_sub, op_mul, op_div, op_mod, op_divmod, op_pow, op_lshift, |
| 21 | op_rshift, op_and, op_xor, op_or, op_neg, op_pos, op_abs, op_invert, |
| 22 | op_int, op_long, op_float, op_str, op_cmp, op_gt, op_ge, op_lt, op_le, |
| 23 | op_eq, op_ne, op_iadd, op_isub, op_imul, op_idiv, op_imod, op_ilshift, |
| 24 | op_irshift, op_iand, op_ixor, op_ior, op_complex, op_bool, op_nonzero, |
| 25 | op_repr, op_truediv |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | enum op_type : int { |
| 29 | op_l, /* base type on left */ |
| 30 | op_r, /* base type on right */ |
| 31 | op_u /* unary operator */ |
| 32 | }; |
| 33 | |
| 34 | struct self_t { }; |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 35 | static const self_t self = self_t(); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 36 | |
| 37 | /// Type for an unused type slot |
| 38 | struct undefined_t { }; |
| 39 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 40 | /// Don't warn about an unused variable |
| 41 | inline self_t __self() { return self; } |
| 42 | |
| 43 | /// base template of operator implementations |
| 44 | template <op_id, op_type, typename B, typename L, typename R> struct op_impl { }; |
| 45 | |
| 46 | /// Operator implementation generator |
| 47 | template <op_id id, op_type ot, typename L, typename R> struct op_ { |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 48 | template <typename Base, typename Holder, typename... Extra> void execute(pybind::class_<Base, Holder> &class_, Extra&&... extra) const { |
| 49 | typedef typename std::conditional<std::is_same<L, self_t>::value, Base, L>::type L_type; |
| 50 | typedef typename std::conditional<std::is_same<R, self_t>::value, Base, R>::type R_type; |
| 51 | typedef op_impl<id, ot, Base, L_type, R_type> op; |
| 52 | class_.def(op::name(), &op::execute, std::forward<Extra>(extra)...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 53 | } |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 54 | template <typename Base, typename Holder, typename... Extra> void execute_cast(pybind::class_<Base, Holder> &class_, Extra&&... extra) const { |
| 55 | typedef typename std::conditional<std::is_same<L, self_t>::value, Base, L>::type L_type; |
| 56 | typedef typename std::conditional<std::is_same<R, self_t>::value, Base, R>::type R_type; |
| 57 | typedef op_impl<id, ot, Base, L_type, R_type> op; |
| 58 | class_.def(op::name(), &op::execute_cast, std::forward<Extra>(extra)...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 59 | } |
| 60 | }; |
| 61 | |
| 62 | #define PYBIND_BINARY_OPERATOR(id, rid, op, expr) \ |
| 63 | template <typename B, typename L, typename R> struct op_impl<op_##id, op_l, B, L, R> { \ |
| 64 | static char const* name() { return "__" #id "__"; } \ |
| 65 | static auto execute(const L &l, const R &r) -> decltype(expr) { return (expr); } \ |
| 66 | static B execute_cast(const L &l, const R &r) { return B(expr); } \ |
| 67 | }; \ |
| 68 | template <typename B, typename L, typename R> struct op_impl<op_##id, op_r, B, L, R> { \ |
| 69 | static char const* name() { return "__" #rid "__"; } \ |
| 70 | static auto execute(const L &l, const R &r) -> decltype(expr) { return (expr); } \ |
| 71 | static B execute_cast(const L &l, const R &r) { return B(expr); } \ |
| 72 | }; \ |
| 73 | inline op_<op_##id, op_l, self_t, self_t> op(const self_t &, const self_t &) { \ |
| 74 | return op_<op_##id, op_l, self_t, self_t>(); \ |
| 75 | }; \ |
| 76 | template <typename T> op_<op_##id, op_l, self_t, T> op(const self_t &, const T &) { \ |
| 77 | return op_<op_##id, op_l, self_t, T>(); \ |
| 78 | }; \ |
| 79 | template <typename T> op_<op_##id, op_r, T, self_t> op(const T &, const self_t &) { \ |
| 80 | return op_<op_##id, op_r, T, self_t>(); \ |
| 81 | }; |
| 82 | |
| 83 | #define PYBIND_INPLACE_OPERATOR(id, op, expr) \ |
| 84 | template <typename B, typename L, typename R> struct op_impl<op_##id, op_l, B, L, R> { \ |
| 85 | static char const* name() { return "__" #id "__"; } \ |
| 86 | static auto execute(L &l, const R &r) -> decltype(expr) { return expr; } \ |
| 87 | static B execute_cast(L &l, const R &r) { return B(expr); } \ |
| 88 | }; \ |
| 89 | template <typename T> op_<op_##id, op_l, self_t, T> op(const self_t &, const T &) { \ |
| 90 | return op_<op_##id, op_l, self_t, T>(); \ |
| 91 | }; |
| 92 | |
| 93 | #define PYBIND_UNARY_OPERATOR(id, op, expr) \ |
| 94 | template <typename B, typename L> struct op_impl<op_##id, op_u, B, L, undefined_t> { \ |
| 95 | static char const* name() { return "__" #id "__"; } \ |
| 96 | static auto execute(const L &l) -> decltype(expr) { return expr; } \ |
| 97 | static B execute_cast(const L &l) { return B(expr); } \ |
| 98 | }; \ |
| 99 | inline op_<op_##id, op_u, self_t, undefined_t> op(const self_t &) { \ |
| 100 | return op_<op_##id, op_u, self_t, undefined_t>(); \ |
| 101 | }; |
| 102 | |
| 103 | PYBIND_BINARY_OPERATOR(sub, rsub, operator-, l - r) |
| 104 | PYBIND_BINARY_OPERATOR(add, radd, operator+, l + r) |
| 105 | PYBIND_BINARY_OPERATOR(mul, rmul, operator*, l * r) |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame^] | 106 | #if PY_MAJOR_VERSION >= 3 |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 107 | PYBIND_BINARY_OPERATOR(truediv, rtruediv, operator/, l / r) |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame^] | 108 | #else |
| 109 | PYBIND_BINARY_OPERATOR(div, rdiv, operator/, l / r) |
| 110 | #endif |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 111 | PYBIND_BINARY_OPERATOR(mod, rmod, operator%, l % r) |
| 112 | PYBIND_BINARY_OPERATOR(lshift, rlshift, operator<<, l << r) |
| 113 | PYBIND_BINARY_OPERATOR(rshift, rrshift, operator>>, l >> r) |
| 114 | PYBIND_BINARY_OPERATOR(and, rand, operator&, l & r) |
| 115 | PYBIND_BINARY_OPERATOR(xor, rxor, operator^, l ^ r) |
| 116 | PYBIND_BINARY_OPERATOR(eq, eq, operator==, l == r) |
| 117 | PYBIND_BINARY_OPERATOR(ne, ne, operator!=, l != r) |
| 118 | PYBIND_BINARY_OPERATOR(or, ror, operator|, l | r) |
| 119 | PYBIND_BINARY_OPERATOR(gt, lt, operator>, l > r) |
| 120 | PYBIND_BINARY_OPERATOR(ge, le, operator>=, l >= r) |
| 121 | PYBIND_BINARY_OPERATOR(lt, gt, operator<, l < r) |
| 122 | PYBIND_BINARY_OPERATOR(le, ge, operator<=, l <= r) |
| 123 | //PYBIND_BINARY_OPERATOR(pow, rpow, pow, std::pow(l, r)) |
| 124 | PYBIND_INPLACE_OPERATOR(iadd, operator+=, l += r) |
| 125 | PYBIND_INPLACE_OPERATOR(isub, operator-=, l -= r) |
| 126 | PYBIND_INPLACE_OPERATOR(imul, operator*=, l *= r) |
| 127 | PYBIND_INPLACE_OPERATOR(idiv, operator/=, l /= r) |
| 128 | PYBIND_INPLACE_OPERATOR(imod, operator%=, l %= r) |
| 129 | PYBIND_INPLACE_OPERATOR(ilshift, operator<<=, l <<= r) |
| 130 | PYBIND_INPLACE_OPERATOR(irshift, operator>>=, l >>= r) |
| 131 | PYBIND_INPLACE_OPERATOR(iand, operator&=, l &= r) |
| 132 | PYBIND_INPLACE_OPERATOR(ixor, operator^=, l ^= r) |
| 133 | PYBIND_INPLACE_OPERATOR(ior, operator|=, l |= r) |
| 134 | PYBIND_UNARY_OPERATOR(neg, operator-, -l) |
| 135 | PYBIND_UNARY_OPERATOR(pos, operator+, +l) |
| 136 | PYBIND_UNARY_OPERATOR(abs, abs, std::abs(l)) |
| 137 | PYBIND_UNARY_OPERATOR(invert, operator~, ~l) |
| 138 | PYBIND_UNARY_OPERATOR(bool, operator!, !!l) |
| 139 | PYBIND_UNARY_OPERATOR(int, int_, (int) l) |
| 140 | PYBIND_UNARY_OPERATOR(float, float_, (double) l) |
| 141 | |
| 142 | #undef PYBIND_BINARY_OPERATOR |
| 143 | #undef PYBIND_INPLACE_OPERATOR |
| 144 | #undef PYBIND_UNARY_OPERATOR |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 145 | NAMESPACE_END(detail) |
| 146 | |
| 147 | using detail::self; |
| 148 | |
| 149 | NAMESPACE_END(pybind) |