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