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