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