operators.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. pybind11/operator.h: Metatemplates for operator overloading
  3. Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
  4. All rights reserved. Use of this source code is governed by a
  5. BSD-style license that can be found in the LICENSE file.
  6. */
  7. #pragma once
  8. #include "pybind11.h"
  9. PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
  10. PYBIND11_NAMESPACE_BEGIN(detail)
  11. /// Enumeration with all supported operator types
  12. enum op_id : int {
  13. op_add,
  14. op_sub,
  15. op_mul,
  16. op_div,
  17. op_mod,
  18. op_divmod,
  19. op_pow,
  20. op_lshift,
  21. op_rshift,
  22. op_and,
  23. op_xor,
  24. op_or,
  25. op_neg,
  26. op_pos,
  27. op_abs,
  28. op_invert,
  29. op_int,
  30. op_long,
  31. op_float,
  32. op_str,
  33. op_cmp,
  34. op_gt,
  35. op_ge,
  36. op_lt,
  37. op_le,
  38. op_eq,
  39. op_ne,
  40. op_iadd,
  41. op_isub,
  42. op_imul,
  43. op_idiv,
  44. op_imod,
  45. op_ilshift,
  46. op_irshift,
  47. op_iand,
  48. op_ixor,
  49. op_ior,
  50. op_complex,
  51. op_bool,
  52. op_nonzero,
  53. op_repr,
  54. op_truediv,
  55. op_itruediv,
  56. op_hash
  57. };
  58. enum op_type : int {
  59. op_l, /* base type on left */
  60. op_r, /* base type on right */
  61. op_u /* unary operator */
  62. };
  63. struct self_t {};
  64. static const self_t self = self_t();
  65. /// Type for an unused type slot
  66. struct undefined_t {};
  67. /// Don't warn about an unused variable
  68. inline self_t __self() { return self; }
  69. /// base template of operator implementations
  70. template <op_id, op_type, typename B, typename L, typename R>
  71. struct op_impl {};
  72. /// Operator implementation generator
  73. template <op_id id, op_type ot, typename L, typename R>
  74. struct op_ {
  75. static constexpr bool op_enable_if_hook = true;
  76. template <typename Class, typename... Extra>
  77. void execute(Class &cl, const Extra &...extra) const {
  78. using Base = typename Class::type;
  79. using L_type = conditional_t<std::is_same<L, self_t>::value, Base, L>;
  80. using R_type = conditional_t<std::is_same<R, self_t>::value, Base, R>;
  81. using op = op_impl<id, ot, Base, L_type, R_type>;
  82. cl.def(op::name(), &op::execute, is_operator(), extra...);
  83. }
  84. template <typename Class, typename... Extra>
  85. void execute_cast(Class &cl, const Extra &...extra) const {
  86. using Base = typename Class::type;
  87. using L_type = conditional_t<std::is_same<L, self_t>::value, Base, L>;
  88. using R_type = conditional_t<std::is_same<R, self_t>::value, Base, R>;
  89. using op = op_impl<id, ot, Base, L_type, R_type>;
  90. cl.def(op::name(), &op::execute_cast, is_operator(), extra...);
  91. }
  92. };
  93. #define PYBIND11_BINARY_OPERATOR(id, rid, op, expr) \
  94. template <typename B, typename L, typename R> \
  95. struct op_impl<op_##id, op_l, B, L, R> { \
  96. static char const *name() { return "__" #id "__"; } \
  97. static auto execute(const L &l, const R &r) -> decltype(expr) { return (expr); } \
  98. static B execute_cast(const L &l, const R &r) { return B(expr); } \
  99. }; \
  100. template <typename B, typename L, typename R> \
  101. struct op_impl<op_##id, op_r, B, L, R> { \
  102. static char const *name() { return "__" #rid "__"; } \
  103. static auto execute(const R &r, const L &l) -> decltype(expr) { return (expr); } \
  104. static B execute_cast(const R &r, const L &l) { return B(expr); } \
  105. }; \
  106. inline op_<op_##id, op_l, self_t, self_t> op(const self_t &, const self_t &) { \
  107. return op_<op_##id, op_l, self_t, self_t>(); \
  108. } \
  109. template <typename T> \
  110. op_<op_##id, op_l, self_t, T> op(const self_t &, const T &) { \
  111. return op_<op_##id, op_l, self_t, T>(); \
  112. } \
  113. template <typename T> \
  114. op_<op_##id, op_r, T, self_t> op(const T &, const self_t &) { \
  115. return op_<op_##id, op_r, T, self_t>(); \
  116. }
  117. #define PYBIND11_INPLACE_OPERATOR(id, op, expr) \
  118. template <typename B, typename L, typename R> \
  119. struct op_impl<op_##id, op_l, B, L, R> { \
  120. static char const *name() { return "__" #id "__"; } \
  121. static auto execute(L &l, const R &r) -> decltype(expr) { return expr; } \
  122. static B execute_cast(L &l, const R &r) { return B(expr); } \
  123. }; \
  124. template <typename T> \
  125. op_<op_##id, op_l, self_t, T> op(const self_t &, const T &) { \
  126. return op_<op_##id, op_l, self_t, T>(); \
  127. }
  128. #define PYBIND11_UNARY_OPERATOR(id, op, expr) \
  129. template <typename B, typename L> \
  130. struct op_impl<op_##id, op_u, B, L, undefined_t> { \
  131. static char const *name() { return "__" #id "__"; } \
  132. static auto execute(const L &l) -> decltype(expr) { return expr; } \
  133. static B execute_cast(const L &l) { return B(expr); } \
  134. }; \
  135. inline op_<op_##id, op_u, self_t, undefined_t> op(const self_t &) { \
  136. return op_<op_##id, op_u, self_t, undefined_t>(); \
  137. }
  138. PYBIND11_BINARY_OPERATOR(sub, rsub, operator-, l - r)
  139. PYBIND11_BINARY_OPERATOR(add, radd, operator+, l + r)
  140. PYBIND11_BINARY_OPERATOR(mul, rmul, operator*, l *r)
  141. PYBIND11_BINARY_OPERATOR(truediv, rtruediv, operator/, l / r)
  142. PYBIND11_BINARY_OPERATOR(mod, rmod, operator%, l % r)
  143. PYBIND11_BINARY_OPERATOR(lshift, rlshift, operator<<, l << r)
  144. PYBIND11_BINARY_OPERATOR(rshift, rrshift, operator>>, l >> r)
  145. PYBIND11_BINARY_OPERATOR(and, rand, operator&, l &r)
  146. PYBIND11_BINARY_OPERATOR(xor, rxor, operator^, l ^ r)
  147. PYBIND11_BINARY_OPERATOR(eq, eq, operator==, l == r)
  148. PYBIND11_BINARY_OPERATOR(ne, ne, operator!=, l != r)
  149. PYBIND11_BINARY_OPERATOR(or, ror, operator|, l | r)
  150. PYBIND11_BINARY_OPERATOR(gt, lt, operator>, l > r)
  151. PYBIND11_BINARY_OPERATOR(ge, le, operator>=, l >= r)
  152. PYBIND11_BINARY_OPERATOR(lt, gt, operator<, l < r)
  153. PYBIND11_BINARY_OPERATOR(le, ge, operator<=, l <= r)
  154. // PYBIND11_BINARY_OPERATOR(pow, rpow, pow, std::pow(l, r))
  155. PYBIND11_INPLACE_OPERATOR(iadd, operator+=, l += r)
  156. PYBIND11_INPLACE_OPERATOR(isub, operator-=, l -= r)
  157. PYBIND11_INPLACE_OPERATOR(imul, operator*=, l *= r)
  158. PYBIND11_INPLACE_OPERATOR(itruediv, operator/=, l /= r)
  159. PYBIND11_INPLACE_OPERATOR(imod, operator%=, l %= r)
  160. PYBIND11_INPLACE_OPERATOR(ilshift, operator<<=, l <<= r)
  161. PYBIND11_INPLACE_OPERATOR(irshift, operator>>=, l >>= r)
  162. PYBIND11_INPLACE_OPERATOR(iand, operator&=, l &= r)
  163. PYBIND11_INPLACE_OPERATOR(ixor, operator^=, l ^= r)
  164. PYBIND11_INPLACE_OPERATOR(ior, operator|=, l |= r)
  165. PYBIND11_UNARY_OPERATOR(neg, operator-, -l)
  166. PYBIND11_UNARY_OPERATOR(pos, operator+, +l)
  167. // WARNING: This usage of `abs` should only be done for existing STL overloads.
  168. // Adding overloads directly in to the `std::` namespace is advised against:
  169. // https://en.cppreference.com/w/cpp/language/extending_std
  170. PYBIND11_UNARY_OPERATOR(abs, abs, std::abs(l))
  171. PYBIND11_UNARY_OPERATOR(hash, hash, std::hash<L>()(l))
  172. PYBIND11_UNARY_OPERATOR(invert, operator~, (~l))
  173. PYBIND11_UNARY_OPERATOR(bool, operator!, !!l)
  174. PYBIND11_UNARY_OPERATOR(int, int_, (int) l)
  175. PYBIND11_UNARY_OPERATOR(float, float_, (double) l)
  176. #undef PYBIND11_BINARY_OPERATOR
  177. #undef PYBIND11_INPLACE_OPERATOR
  178. #undef PYBIND11_UNARY_OPERATOR
  179. PYBIND11_NAMESPACE_END(detail)
  180. using detail::self;
  181. // Add named operators so that they are accessible via `py::`.
  182. using detail::hash;
  183. PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)