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  template <typename Class, typename... Extra>
88  void execute(Class &cl, const Extra &...extra) const {
89  using Base = typename Class::type;
91  using R_type = conditional_t<std::is_same<R, self_t>::value, Base, R>;
93  cl.def(op::name(), &op::execute, is_operator(), extra...);
94  }
95  template <typename Class, typename... Extra>
96  void execute_cast(Class &cl, const Extra &...extra) const {
97  using Base = typename Class::type;
99  using R_type = conditional_t<std::is_same<R, self_t>::value, Base, R>;
101  cl.def(op::name(), &op::execute_cast, is_operator(), extra...);
102  }
103 };
104 
105 #define PYBIND11_BINARY_OPERATOR(id, rid, op, expr) \
106  template <typename B, typename L, typename R> \
107  struct op_impl<op_##id, op_l, B, L, R> { \
108  static char const *name() { return "__" #id "__"; } \
109  static auto execute(const L &l, const R &r) -> decltype(expr) { return (expr); } \
110  static B execute_cast(const L &l, const R &r) { return B(expr); } \
111  }; \
112  template <typename B, typename L, typename R> \
113  struct op_impl<op_##id, op_r, B, L, R> { \
114  static char const *name() { return "__" #rid "__"; } \
115  static auto execute(const R &r, const L &l) -> decltype(expr) { return (expr); } \
116  static B execute_cast(const R &r, const L &l) { return B(expr); } \
117  }; \
118  inline op_<op_##id, op_l, self_t, self_t> op(const self_t &, const self_t &) { \
119  return op_<op_##id, op_l, self_t, self_t>(); \
120  } \
121  template <typename T> \
122  op_<op_##id, op_l, self_t, T> op(const self_t &, const T &) { \
123  return op_<op_##id, op_l, self_t, T>(); \
124  } \
125  template <typename T> \
126  op_<op_##id, op_r, T, self_t> op(const T &, const self_t &) { \
127  return op_<op_##id, op_r, T, self_t>(); \
128  }
129 
130 #define PYBIND11_INPLACE_OPERATOR(id, op, expr) \
131  template <typename B, typename L, typename R> \
132  struct op_impl<op_##id, op_l, B, L, R> { \
133  static char const *name() { return "__" #id "__"; } \
134  static auto execute(L &l, const R &r) -> decltype(expr) { return expr; } \
135  static B execute_cast(L &l, const R &r) { return B(expr); } \
136  }; \
137  template <typename T> \
138  op_<op_##id, op_l, self_t, T> op(const self_t &, const T &) { \
139  return op_<op_##id, op_l, self_t, T>(); \
140  }
141 
142 #define PYBIND11_UNARY_OPERATOR(id, op, expr) \
143  template <typename B, typename L> \
144  struct op_impl<op_##id, op_u, B, L, undefined_t> { \
145  static char const *name() { return "__" #id "__"; } \
146  static auto execute(const L &l) -> decltype(expr) { return expr; } \
147  static B execute_cast(const L &l) { return B(expr); } \
148  }; \
149  inline op_<op_##id, op_u, self_t, undefined_t> op(const self_t &) { \
150  return op_<op_##id, op_u, self_t, undefined_t>(); \
151  }
152 
153 PYBIND11_BINARY_OPERATOR(sub, rsub, operator-, l - r)
154 PYBIND11_BINARY_OPERATOR(add, radd, operator+, l + r)
155 PYBIND11_BINARY_OPERATOR(mul, rmul, operator*, l *r)
156 PYBIND11_BINARY_OPERATOR(truediv, rtruediv, operator/, l / r)
157 PYBIND11_BINARY_OPERATOR(mod, rmod, operator%, l % r)
158 PYBIND11_BINARY_OPERATOR(lshift, rlshift, operator<<, l << r)
159 PYBIND11_BINARY_OPERATOR(rshift, rrshift, operator>>, l >> r)
160 PYBIND11_BINARY_OPERATOR(and, rand, operator&, l &r)
161 PYBIND11_BINARY_OPERATOR(xor, rxor, operator^, l ^ r)
162 PYBIND11_BINARY_OPERATOR(eq, eq, operator==, l == r)
163 PYBIND11_BINARY_OPERATOR(ne, ne, operator!=, l != r)
164 PYBIND11_BINARY_OPERATOR(or, ror, operator|, l | r)
165 PYBIND11_BINARY_OPERATOR(gt, lt, operator>, l > r)
166 PYBIND11_BINARY_OPERATOR(ge, le, operator>=, l >= r)
167 PYBIND11_BINARY_OPERATOR(lt, gt, operator<, l < r)
168 PYBIND11_BINARY_OPERATOR(le, ge, operator<=, l <= r)
169 // PYBIND11_BINARY_OPERATOR(pow, rpow, pow, std::pow(l, r))
170 PYBIND11_INPLACE_OPERATOR(iadd, operator+=, l += r)
171 PYBIND11_INPLACE_OPERATOR(isub, operator-=, l -= r)
172 PYBIND11_INPLACE_OPERATOR(imul, operator*=, l *= r)
173 PYBIND11_INPLACE_OPERATOR(itruediv, operator/=, l /= r)
174 PYBIND11_INPLACE_OPERATOR(imod, operator%=, l %= r)
175 PYBIND11_INPLACE_OPERATOR(ilshift, operator<<=, l <<= r)
176 PYBIND11_INPLACE_OPERATOR(irshift, operator>>=, l >>= r)
177 PYBIND11_INPLACE_OPERATOR(iand, operator&=, l &= r)
178 PYBIND11_INPLACE_OPERATOR(ixor, operator^=, l ^= r)
179 PYBIND11_INPLACE_OPERATOR(ior, operator|=, l |= r)
180 PYBIND11_UNARY_OPERATOR(neg, operator-, -l)
181 PYBIND11_UNARY_OPERATOR(pos, operator+, +l)
182 // WARNING: This usage of `abs` should only be done for existing STL overloads.
183 // Adding overloads directly in to the `std::` namespace is advised against:
184 // https://en.cppreference.com/w/cpp/language/extending_std
186 PYBIND11_UNARY_OPERATOR(hash, hash, std::hash<L>()(l))
187 PYBIND11_UNARY_OPERATOR(invert, operator~, (~l))
188 PYBIND11_UNARY_OPERATOR(bool, operator!, !!l)
189 PYBIND11_UNARY_OPERATOR(int, int_, (int) l)
190 PYBIND11_UNARY_OPERATOR(float, float_, (double) l)
191 
192 #undef PYBIND11_BINARY_OPERATOR
193 #undef PYBIND11_INPLACE_OPERATOR
194 #undef PYBIND11_UNARY_OPERATOR
196 
197 using detail::self;
198 // Add named operators so that they are accessible via `py::`.
199 using detail::hash;
200 
typename std::conditional< B, T, F >::type conditional_t
ssize_t hash(handle obj)
Definition: pytypes.h:792
Definition: operators.h:66
Operator implementation generator.
Definition: attr.h:171
#define PYBIND11_UNARY_OPERATOR(id, op, expr)
Definition: operators.h:142
double mul(const double &a, const double &b)
Type for an unused type slot.
Definition: operators.h:75
Rot2 R(Rot2::fromAngle(0.1))
Definition: operators.h:68
static const self_t self
Definition: operators.h:72
MatrixXd L
Definition: LLT_example.cpp:6
#define PYBIND11_INPLACE_OPERATOR(id, op, expr)
Definition: operators.h:130
Annotation for operators.
Definition: attr.h:30
op_id
Enumeration with all supported operator types.
Definition: operators.h:18
Definition: operators.h:67
void execute(Class &cl, const Extra &...extra) const
Definition: operators.h:88
static const Line3 l(Rot3(), 1, 1)
base template of operator implementations
Definition: operators.h:82
EIGEN_DONT_INLINE T sub(T a, T b)
Definition: svd_common.h:299
Definition: pytypes.h:1663
self_t __self()
Don&#39;t warn about an unused variable.
Definition: operators.h:78
#define PYBIND11_BINARY_OPERATOR(id, rid, op, expr)
Definition: operators.h:105
graph add(PriorFactor< Pose2 >(1, priorMean, priorNoise))
#define abs(x)
Definition: datatypes.h:17
op_type
Definition: operators.h:65
void execute_cast(Class &cl, const Extra &...extra) const
Definition: operators.h:96
#define PYBIND11_NAMESPACE_END(name)
#define PYBIND11_NAMESPACE_BEGIN(name)


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:58