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 enum op_id : int {
63 };
64 
65 enum op_type : int {
66  op_l, /* base type on left */
67  op_r, /* base type on right */
68  op_u /* unary operator */
69 };
70 
71 struct self_t {};
72 static const self_t self = self_t();
73 
75 struct undefined_t {};
76 
78 inline self_t __self() { return self; }
79 
81 template <op_id, op_type, typename B, typename L, typename R>
82 struct op_impl {};
83 
85 template <op_id id, op_type ot, typename L, typename R>
86 struct 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 
154 PYBIND11_BINARY_OPERATOR(sub, rsub, operator-, l - r)
155 PYBIND11_BINARY_OPERATOR(add, radd, operator+, l + r)
156 PYBIND11_BINARY_OPERATOR(mul, rmul, operator*, l *r)
157 PYBIND11_BINARY_OPERATOR(truediv, rtruediv, operator/, l / r)
158 PYBIND11_BINARY_OPERATOR(mod, rmod, operator%, l % r)
159 PYBIND11_BINARY_OPERATOR(lshift, rlshift, operator<<, l << r)
160 PYBIND11_BINARY_OPERATOR(rshift, rrshift, operator>>, l >> r)
161 PYBIND11_BINARY_OPERATOR(and, rand, operator&, l &r)
162 PYBIND11_BINARY_OPERATOR(xor, rxor, operator^, l ^ r)
163 PYBIND11_BINARY_OPERATOR(eq, eq, operator==, l == r)
164 PYBIND11_BINARY_OPERATOR(ne, ne, operator!=, l != r)
165 PYBIND11_BINARY_OPERATOR(or, ror, operator|, l | r)
166 PYBIND11_BINARY_OPERATOR(gt, lt, operator>, l > r)
167 PYBIND11_BINARY_OPERATOR(ge, le, operator>=, l >= r)
168 PYBIND11_BINARY_OPERATOR(lt, gt, operator<, l < r)
169 PYBIND11_BINARY_OPERATOR(le, ge, operator<=, l <= r)
170 // PYBIND11_BINARY_OPERATOR(pow, rpow, pow, std::pow(l, r))
171 PYBIND11_INPLACE_OPERATOR(iadd, operator+=, l += r)
172 PYBIND11_INPLACE_OPERATOR(isub, operator-=, l -= r)
173 PYBIND11_INPLACE_OPERATOR(imul, operator*=, l *= r)
174 PYBIND11_INPLACE_OPERATOR(itruediv, operator/=, l /= r)
175 PYBIND11_INPLACE_OPERATOR(imod, operator%=, l %= r)
176 PYBIND11_INPLACE_OPERATOR(ilshift, operator<<=, l <<= r)
177 PYBIND11_INPLACE_OPERATOR(irshift, operator>>=, l >>= r)
178 PYBIND11_INPLACE_OPERATOR(iand, operator&=, l &= r)
179 PYBIND11_INPLACE_OPERATOR(ixor, operator^=, l ^= r)
180 PYBIND11_INPLACE_OPERATOR(ior, operator|=, l |= r)
181 PYBIND11_UNARY_OPERATOR(neg, operator-, -l)
182 PYBIND11_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
187 PYBIND11_UNARY_OPERATOR(hash, hash, std::hash<L>()(l))
188 PYBIND11_UNARY_OPERATOR(invert, operator~, (~l))
189 PYBIND11_UNARY_OPERATOR(bool, operator!, !!l)
190 PYBIND11_UNARY_OPERATOR(int, int_, (int) l)
191 PYBIND11_UNARY_OPERATOR(float, float_, (double) l)
192 
193 #undef PYBIND11_BINARY_OPERATOR
194 #undef PYBIND11_INPLACE_OPERATOR
195 #undef PYBIND11_UNARY_OPERATOR
197 
198 using detail::self;
199 // Add named operators so that they are accessible via `py::`.
200 using detail::hash;
201 
op_rshift
@ op_rshift
Definition: operators.h:27
op_abs
@ op_abs
Definition: operators.h:33
int_
Definition: pytypes.h:1835
op_lshift
@ op_lshift
Definition: operators.h:26
op_nonzero
@ op_nonzero
Definition: operators.h:58
PYBIND11_INPLACE_OPERATOR
#define PYBIND11_INPLACE_OPERATOR(id, op, expr)
Definition: operators.h:131
op_::execute_cast
void execute_cast(Class &cl, const Extra &...extra) const
Definition: operators.h:97
op_ne
@ op_ne
Definition: operators.h:45
op_add
@ op_add
Definition: operators.h:19
gtsam.examples.DogLegOptimizerExample.type
type
Definition: DogLegOptimizerExample.py:111
op_truediv
@ op_truediv
Definition: operators.h:60
op_ixor
@ op_ixor
Definition: operators.h:54
op_bool
@ op_bool
Definition: operators.h:57
is_operator
Annotation for operators.
Definition: attr.h:33
op_mod
@ op_mod
Definition: operators.h:23
op_str
@ op_str
Definition: operators.h:38
op_idiv
@ op_idiv
Definition: operators.h:49
op_pos
@ op_pos
Definition: operators.h:32
PYBIND11_NAMESPACE_END
#define PYBIND11_NAMESPACE_END(name)
Definition: wrap/pybind11/include/pybind11/detail/common.h:80
op_itruediv
@ op_itruediv
Definition: operators.h:61
op_eq
@ op_eq
Definition: operators.h:44
PYBIND11_BINARY_OPERATOR
#define PYBIND11_BINARY_OPERATOR(id, rid, op, expr)
Definition: operators.h:106
mul
double mul(const double &a, const double &b)
Definition: testAlgebraicDecisionTree.cpp:94
op_::execute
void execute(Class &cl, const Extra &...extra) const
Definition: operators.h:89
detail
Definition: testSerializationNonlinear.cpp:70
op_::op_enable_if_hook
static constexpr bool op_enable_if_hook
Definition: operators.h:87
PYBIND11_NAMESPACE_BEGIN
#define PYBIND11_NAMESPACE_BEGIN(name)
Definition: wrap/pybind11/include/pybind11/detail/common.h:76
op_
Operator implementation generator.
Definition: attr.h:174
op_id
op_id
Enumeration with all supported operator types.
Definition: operators.h:18
op_ior
@ op_ior
Definition: operators.h:55
op_invert
@ op_invert
Definition: operators.h:34
hash
ssize_t hash(handle obj)
Definition: pytypes.h:934
__self
self_t __self()
Don't warn about an unused variable.
Definition: operators.h:78
conditional_t
typename std::conditional< B, T, F >::type conditional_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:656
name
static char name[]
Definition: rgamma.c:72
op_repr
@ op_repr
Definition: operators.h:59
op_imul
@ op_imul
Definition: operators.h:48
op_div
@ op_div
Definition: operators.h:22
op_long
@ op_long
Definition: operators.h:36
op_u
@ op_u
Definition: operators.h:68
op_r
@ op_r
Definition: operators.h:67
undefined_t
Type for an unused type slot.
Definition: operators.h:75
op_complex
@ op_complex
Definition: operators.h:56
op_divmod
@ op_divmod
Definition: operators.h:24
op_mul
@ op_mul
Definition: operators.h:21
op_hash
@ op_hash
Definition: operators.h:62
op_gt
@ op_gt
Definition: operators.h:40
add
graph add(PriorFactor< Pose2 >(1, priorMean, priorNoise))
op_iand
@ op_iand
Definition: operators.h:53
l
static const Line3 l(Rot3(), 1, 1)
self_t
Definition: operators.h:71
op_and
@ op_and
Definition: operators.h:28
cl
Definition: cxx11_tensor_builtins_sycl.cpp:30
L
MatrixXd L
Definition: LLT_example.cpp:6
op_imod
@ op_imod
Definition: operators.h:50
PYBIND11_UNARY_OPERATOR
#define PYBIND11_UNARY_OPERATOR(id, op, expr)
Definition: operators.h:143
PYBIND11_NAMESPACE
Definition: test_custom_type_casters.cpp:24
op_neg
@ op_neg
Definition: operators.h:31
op_lt
@ op_lt
Definition: operators.h:42
op_cmp
@ op_cmp
Definition: operators.h:39
op_ilshift
@ op_ilshift
Definition: operators.h:51
op_impl
base template of operator implementations
Definition: operators.h:82
pybind11.h
op_ge
@ op_ge
Definition: operators.h:41
op_irshift
@ op_irshift
Definition: operators.h:52
Class
Definition: testExpression.cpp:116
float_
Definition: pytypes.h:1870
op_le
@ op_le
Definition: operators.h:43
op_isub
@ op_isub
Definition: operators.h:47
self
static const self_t self
Definition: operators.h:72
op_sub
@ op_sub
Definition: operators.h:20
op_float
@ op_float
Definition: operators.h:37
abs
#define abs(x)
Definition: datatypes.h:17
op_iadd
@ op_iadd
Definition: operators.h:46
op_pow
@ op_pow
Definition: operators.h:25
Base
Definition: test_virtual_functions.cpp:156
pos
Definition: example-NearestNeighbor.cpp:32
op_or
@ op_or
Definition: operators.h:30
op_int
@ op_int
Definition: operators.h:35
op_type
op_type
Definition: operators.h:65
op_xor
@ op_xor
Definition: operators.h:29
R
Rot2 R(Rot2::fromAngle(0.1))
op_l
@ op_l
Definition: operators.h:66
sub
EIGEN_DONT_INLINE T sub(T a, T b)
Definition: svd_common.h:299


gtsam
Author(s):
autogenerated on Fri Nov 1 2024 03:33:52