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