μHAL (v2.8.17)
Part of the IPbus software repository
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
operators.h
Go to the documentation of this file.
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
16
17
18enum op_id : int {
63};
64
65enum op_type : int {
66 op_l, /* base type on left */
67 op_r, /* base type on right */
68 op_u /* unary operator */
69};
70
71struct self_t {};
72static const self_t self = self_t();
73
75struct undefined_t {};
76
78inline self_t __self() { return self; }
79
81template <op_id, op_type, typename B, typename L, typename R>
82struct op_impl {};
83
85template <op_id id, op_type ot, typename L, typename R>
86struct op_ {
87 static constexpr bool op_enable_if_hook = true;
88 template <typename Class, typename... Extra>
89 void execute(Class &cl, const Extra &...extra) const {
90 using Base = typename Class::type;
94 cl.def(op::name(), &op::execute, is_operator(), extra...);
95 }
96 template <typename Class, typename... Extra>
97 void execute_cast(Class &cl, const Extra &...extra) const {
98 using Base = typename Class::type;
102 cl.def(op::name(), &op::execute_cast, is_operator(), extra...);
103 }
104};
105
106#define PYBIND11_BINARY_OPERATOR(id, rid, op, expr) \
107 template <typename B, typename L, typename R> \
108 struct op_impl<op_##id, op_l, B, L, R> { \
109 static char const *name() { return "__" #id "__"; } \
110 static auto execute(const L &l, const R &r) -> decltype(expr) { return (expr); } \
111 static B execute_cast(const L &l, const R &r) { return B(expr); } \
112 }; \
113 template <typename B, typename L, typename R> \
114 struct op_impl<op_##id, op_r, B, L, R> { \
115 static char const *name() { return "__" #rid "__"; } \
116 static auto execute(const R &r, const L &l) -> decltype(expr) { return (expr); } \
117 static B execute_cast(const R &r, const L &l) { return B(expr); } \
118 }; \
119 inline op_<op_##id, op_l, self_t, self_t> op(const self_t &, const self_t &) { \
120 return op_<op_##id, op_l, self_t, self_t>(); \
121 } \
122 template <typename T> \
123 op_<op_##id, op_l, self_t, T> op(const self_t &, const T &) { \
124 return op_<op_##id, op_l, self_t, T>(); \
125 } \
126 template <typename T> \
127 op_<op_##id, op_r, T, self_t> op(const T &, const self_t &) { \
128 return op_<op_##id, op_r, T, self_t>(); \
129 }
130
131#define PYBIND11_INPLACE_OPERATOR(id, op, expr) \
132 template <typename B, typename L, typename R> \
133 struct op_impl<op_##id, op_l, B, L, R> { \
134 static char const *name() { return "__" #id "__"; } \
135 static auto execute(L &l, const R &r) -> decltype(expr) { return expr; } \
136 static B execute_cast(L &l, const R &r) { return B(expr); } \
137 }; \
138 template <typename T> \
139 op_<op_##id, op_l, self_t, T> op(const self_t &, const T &) { \
140 return op_<op_##id, op_l, self_t, T>(); \
141 }
142
143#define PYBIND11_UNARY_OPERATOR(id, op, expr) \
144 template <typename B, typename L> \
145 struct op_impl<op_##id, op_u, B, L, undefined_t> { \
146 static char const *name() { return "__" #id "__"; } \
147 static auto execute(const L &l) -> decltype(expr) { return expr; } \
148 static B execute_cast(const L &l) { return B(expr); } \
149 }; \
150 inline op_<op_##id, op_u, self_t, undefined_t> op(const self_t &) { \
151 return op_<op_##id, op_u, self_t, undefined_t>(); \
152 }
153
154PYBIND11_BINARY_OPERATOR(sub, rsub, operator-, l - r)
155PYBIND11_BINARY_OPERATOR(add, radd, operator+, l + r)
156PYBIND11_BINARY_OPERATOR(mul, rmul, operator*, l *r)
157PYBIND11_BINARY_OPERATOR(truediv, rtruediv, operator/, l / r)
158PYBIND11_BINARY_OPERATOR(mod, rmod, operator%, l % r)
159PYBIND11_BINARY_OPERATOR(lshift, rlshift, operator<<, l << r)
160PYBIND11_BINARY_OPERATOR(rshift, rrshift, operator>>, l >> r)
161PYBIND11_BINARY_OPERATOR(and, rand, operator&, l &r)
162PYBIND11_BINARY_OPERATOR(xor, rxor, operator^, l ^ r)
163PYBIND11_BINARY_OPERATOR(eq, eq, operator==, l == r)
164PYBIND11_BINARY_OPERATOR(ne, ne, operator!=, l != r)
165PYBIND11_BINARY_OPERATOR(or, ror, operator|, l | r)
166PYBIND11_BINARY_OPERATOR(gt, lt, operator>, l > r)
167PYBIND11_BINARY_OPERATOR(ge, le, operator>=, l >= r)
168PYBIND11_BINARY_OPERATOR(lt, gt, operator<, l < r)
169PYBIND11_BINARY_OPERATOR(le, ge, operator<=, l <= r)
170// PYBIND11_BINARY_OPERATOR(pow, rpow, pow, std::pow(l, r))
171PYBIND11_INPLACE_OPERATOR(iadd, operator+=, l += r)
172PYBIND11_INPLACE_OPERATOR(isub, operator-=, l -= r)
173PYBIND11_INPLACE_OPERATOR(imul, operator*=, l *= r)
174PYBIND11_INPLACE_OPERATOR(itruediv, operator/=, l /= r)
175PYBIND11_INPLACE_OPERATOR(imod, operator%=, l %= r)
176PYBIND11_INPLACE_OPERATOR(ilshift, operator<<=, l <<= r)
177PYBIND11_INPLACE_OPERATOR(irshift, operator>>=, l >>= r)
178PYBIND11_INPLACE_OPERATOR(iand, operator&=, l &= r)
179PYBIND11_INPLACE_OPERATOR(ixor, operator^=, l ^= r)
180PYBIND11_INPLACE_OPERATOR(ior, operator|=, l |= r)
181PYBIND11_UNARY_OPERATOR(neg, operator-, -l)
182PYBIND11_UNARY_OPERATOR(pos, operator+, +l)
183// WARNING: This usage of `abs` should only be done for existing STL overloads.
184// Adding overloads directly in to the `std::` namespace is advised against:
185// https://en.cppreference.com/w/cpp/language/extending_std
186PYBIND11_UNARY_OPERATOR(abs, abs, std::abs(l))
187PYBIND11_UNARY_OPERATOR(hash, hash, std::hash<L>()(l))
188PYBIND11_UNARY_OPERATOR(invert, operator~, (~l))
189PYBIND11_UNARY_OPERATOR(bool, operator!, !!l)
190PYBIND11_UNARY_OPERATOR(int, int_, (int) l)
191PYBIND11_UNARY_OPERATOR(float, float_, (double) l)
192
193#undef PYBIND11_BINARY_OPERATOR
194#undef PYBIND11_INPLACE_OPERATOR
195#undef PYBIND11_UNARY_OPERATOR
197
198using detail::self;
199// Add named operators so that they are accessible via `py::`.
200using detail::hash;
201
Definition: pytypes.h:1475
#define PYBIND11_NAMESPACE_END(name)
Definition: common.h:21
#define PYBIND11_NAMESPACE_BEGIN(name)
Definition: common.h:20
typename std::conditional< B, T, F >::type conditional_t
Definition: common.h:627
op_id
Enumeration with all supported operator types.
Definition: operators.h:18
std::string abs(const Vector2 &)
#define PYBIND11_INPLACE_OPERATOR(id, op, expr)
Definition: operators.h:131
#define PYBIND11_UNARY_OPERATOR(id, op, expr)
Definition: operators.h:143
#define PYBIND11_BINARY_OPERATOR(id, rid, op, expr)
Definition: operators.h:106
op_type
Definition: operators.h:65
@ op_l
Definition: operators.h:66
@ op_r
Definition: operators.h:67
@ op_u
Definition: operators.h:68
static const self_t self
Definition: operators.h:72
@ op_repr
Definition: operators.h:59
@ op_ilshift
Definition: operators.h:51
@ op_gt
Definition: operators.h:40
@ op_le
Definition: operators.h:43
@ op_int
Definition: operators.h:35
@ op_mul
Definition: operators.h:21
@ op_nonzero
Definition: operators.h:58
@ op_lt
Definition: operators.h:42
@ op_pos
Definition: operators.h:32
@ op_lshift
Definition: operators.h:26
@ op_str
Definition: operators.h:38
@ op_iadd
Definition: operators.h:46
@ op_long
Definition: operators.h:36
@ op_float
Definition: operators.h:37
@ op_isub
Definition: operators.h:47
@ op_rshift
Definition: operators.h:27
@ op_eq
Definition: operators.h:44
@ op_ior
Definition: operators.h:55
@ op_itruediv
Definition: operators.h:61
@ op_pow
Definition: operators.h:25
@ op_imul
Definition: operators.h:48
@ op_invert
Definition: operators.h:34
@ op_sub
Definition: operators.h:20
@ op_mod
Definition: operators.h:23
@ op_complex
Definition: operators.h:56
@ op_div
Definition: operators.h:22
@ op_hash
Definition: operators.h:62
@ op_irshift
Definition: operators.h:52
@ op_abs
Definition: operators.h:33
@ op_ne
Definition: operators.h:45
@ op_truediv
Definition: operators.h:60
@ op_add
Definition: operators.h:19
@ op_xor
Definition: operators.h:29
@ op_and
Definition: operators.h:28
@ op_ixor
Definition: operators.h:54
@ op_or
Definition: operators.h:30
@ op_ge
Definition: operators.h:41
@ op_bool
Definition: operators.h:57
@ op_divmod
Definition: operators.h:24
@ op_iand
Definition: operators.h:53
@ op_idiv
Definition: operators.h:49
@ op_cmp
Definition: operators.h:39
@ op_imod
Definition: operators.h:50
@ op_neg
Definition: operators.h:31
self_t __self()
Don't warn about an unused variable.
Definition: operators.h:78
Annotation for operators.
Definition: attr.h:29
Operator implementation generator.
Definition: operators.h:86
static constexpr bool op_enable_if_hook
Definition: operators.h:87
void execute(Class &cl, const Extra &...extra) const
Definition: operators.h:89
void execute_cast(Class &cl, const Extra &...extra) const
Definition: operators.h:97
base template of operator implementations
Definition: operators.h:82
Type for an unused type slot.
Definition: operators.h:75