blob: 08fcd991a25b6a201814da73bbdff915d018c8ba [file] [log] [blame]
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001/*
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
16NAMESPACE_BEGIN(pybind)
17NAMESPACE_BEGIN(detail)
18
19/// Enumeration with all supported operator types
20enum 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
30enum op_type : int {
31 op_l, /* base type on left */
32 op_r, /* base type on right */
33 op_u /* unary operator */
34};
35
36struct self_t { };
37
38/// Type for an unused type slot
39struct undefined_t { };
40
41static const self_t self = self_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 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) \
66template <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}; \
71template <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}; \
76inline 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}; \
79template <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}; \
82template <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) \
87template <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}; \
92template <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) \
97template <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}; \
102inline 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
106PYBIND_BINARY_OPERATOR(sub, rsub, operator-, l - r)
107PYBIND_BINARY_OPERATOR(add, radd, operator+, l + r)
108PYBIND_BINARY_OPERATOR(mul, rmul, operator*, l * r)
109PYBIND_BINARY_OPERATOR(truediv, rtruediv, operator/, l / r)
110PYBIND_BINARY_OPERATOR(mod, rmod, operator%, l % r)
111PYBIND_BINARY_OPERATOR(lshift, rlshift, operator<<, l << r)
112PYBIND_BINARY_OPERATOR(rshift, rrshift, operator>>, l >> r)
113PYBIND_BINARY_OPERATOR(and, rand, operator&, l & r)
114PYBIND_BINARY_OPERATOR(xor, rxor, operator^, l ^ r)
115PYBIND_BINARY_OPERATOR(eq, eq, operator==, l == r)
116PYBIND_BINARY_OPERATOR(ne, ne, operator!=, l != r)
117PYBIND_BINARY_OPERATOR(or, ror, operator|, l | r)
118PYBIND_BINARY_OPERATOR(gt, lt, operator>, l > r)
119PYBIND_BINARY_OPERATOR(ge, le, operator>=, l >= r)
120PYBIND_BINARY_OPERATOR(lt, gt, operator<, l < r)
121PYBIND_BINARY_OPERATOR(le, ge, operator<=, l <= r)
122//PYBIND_BINARY_OPERATOR(pow, rpow, pow, std::pow(l, r))
123PYBIND_INPLACE_OPERATOR(iadd, operator+=, l += r)
124PYBIND_INPLACE_OPERATOR(isub, operator-=, l -= r)
125PYBIND_INPLACE_OPERATOR(imul, operator*=, l *= r)
126PYBIND_INPLACE_OPERATOR(idiv, operator/=, l /= r)
127PYBIND_INPLACE_OPERATOR(imod, operator%=, l %= r)
128PYBIND_INPLACE_OPERATOR(ilshift, operator<<=, l <<= r)
129PYBIND_INPLACE_OPERATOR(irshift, operator>>=, l >>= r)
130PYBIND_INPLACE_OPERATOR(iand, operator&=, l &= r)
131PYBIND_INPLACE_OPERATOR(ixor, operator^=, l ^= r)
132PYBIND_INPLACE_OPERATOR(ior, operator|=, l |= r)
133PYBIND_UNARY_OPERATOR(neg, operator-, -l)
134PYBIND_UNARY_OPERATOR(pos, operator+, +l)
135PYBIND_UNARY_OPERATOR(abs, abs, std::abs(l))
136PYBIND_UNARY_OPERATOR(invert, operator~, ~l)
137PYBIND_UNARY_OPERATOR(bool, operator!, !!l)
138PYBIND_UNARY_OPERATOR(int, int_, (int) l)
139PYBIND_UNARY_OPERATOR(float, float_, (double) l)
140
141#undef PYBIND_BINARY_OPERATOR
142#undef PYBIND_INPLACE_OPERATOR
143#undef PYBIND_UNARY_OPERATOR
144
145NAMESPACE_END(detail)
146
147using detail::self;
148
149NAMESPACE_END(pybind)
150
151#endif /* __PYBIND_OPERATOR */