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