operators.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. #if defined(__clang__) && !defined(__INTEL_COMPILER)
  10. # pragma clang diagnostic ignored "-Wunsequenced" // multiple unsequenced modifications to 'self' (when using def(py::self OP Type()))
  11. #elif defined(_MSC_VER)
  12. # pragma warning(push)
  13. # pragma warning(disable: 4127) // warning C4127: Conditional expression is constant
  14. #endif
  15. NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
  16. NAMESPACE_BEGIN(detail)
  17. /// Enumeration with all supported operator types
  18. enum op_id : int {
  19. op_add, op_sub, op_mul, op_div, op_mod, op_divmod, op_pow, op_lshift,
  20. op_rshift, op_and, op_xor, op_or, op_neg, op_pos, op_abs, op_invert,
  21. op_int, op_long, op_float, op_str, op_cmp, op_gt, op_ge, op_lt, op_le,
  22. op_eq, op_ne, op_iadd, op_isub, op_imul, op_idiv, op_imod, op_ilshift,
  23. op_irshift, op_iand, op_ixor, op_ior, op_complex, op_bool, op_nonzero,
  24. op_repr, op_truediv, op_itruediv, op_hash
  25. };
  26. enum op_type : int {
  27. op_l, /* base type on left */
  28. op_r, /* base type on right */
  29. op_u /* unary operator */
  30. };
  31. struct self_t { };
  32. static const self_t self = self_t();
  33. /// Type for an unused type slot
  34. struct undefined_t { };
  35. /// Don't warn about an unused variable
  36. inline self_t __self() { return self; }
  37. /// base template of operator implementations
  38. template <op_id, op_type, typename B, typename L, typename R> struct op_impl { };
  39. /// Operator implementation generator
  40. template <op_id id, op_type ot, typename L, typename R> struct op_ {
  41. template <typename Class, typename... Extra> void execute(Class &cl, const Extra&... extra) const {
  42. using Base = typename Class::type;
  43. using L_type = conditional_t<std::is_same<L, self_t>::value, Base, L>;
  44. using R_type = conditional_t<std::is_same<R, self_t>::value, Base, R>;
  45. using op = op_impl<id, ot, Base, L_type, R_type>;
  46. cl.def(op::name(), &op::execute, is_operator(), extra...);
  47. #if PY_MAJOR_VERSION < 3
  48. if (id == op_truediv || id == op_itruediv)
  49. cl.def(id == op_itruediv ? "__idiv__" : ot == op_l ? "__div__" : "__rdiv__",
  50. &op::execute, is_operator(), extra...);
  51. #endif
  52. }
  53. template <typename Class, typename... Extra> void execute_cast(Class &cl, const Extra&... extra) const {
  54. using Base = typename Class::type;
  55. using L_type = conditional_t<std::is_same<L, self_t>::value, Base, L>;
  56. using R_type = conditional_t<std::is_same<R, self_t>::value, Base, R>;
  57. using op = op_impl<id, ot, Base, L_type, R_type>;
  58. cl.def(op::name(), &op::execute_cast, is_operator(), extra...);
  59. #if PY_MAJOR_VERSION < 3
  60. if (id == op_truediv || id == op_itruediv)
  61. cl.def(id == op_itruediv ? "__idiv__" : ot == op_l ? "__div__" : "__rdiv__",
  62. &op::execute, is_operator(), extra...);
  63. #endif
  64. }
  65. };
  66. #define PYBIND11_BINARY_OPERATOR(id, rid, op, expr) \
  67. template <typename B, typename L, typename R> struct op_impl<op_##id, op_l, B, L, R> { \
  68. static char const* name() { return "__" #id "__"; } \
  69. static auto execute(const L &l, const R &r) -> decltype(expr) { return (expr); } \
  70. static B execute_cast(const L &l, const R &r) { return B(expr); } \
  71. }; \
  72. template <typename B, typename L, typename R> struct op_impl<op_##id, op_r, B, L, R> { \
  73. static char const* name() { return "__" #rid "__"; } \
  74. static auto execute(const R &r, const L &l) -> decltype(expr) { return (expr); } \
  75. static B execute_cast(const R &r, const L &l) { return B(expr); } \
  76. }; \
  77. inline op_<op_##id, op_l, self_t, self_t> op(const self_t &, const self_t &) { \
  78. return op_<op_##id, op_l, self_t, self_t>(); \
  79. } \
  80. template <typename T> op_<op_##id, op_l, self_t, T> op(const self_t &, const T &) { \
  81. return op_<op_##id, op_l, self_t, T>(); \
  82. } \
  83. template <typename T> op_<op_##id, op_r, T, self_t> op(const T &, const self_t &) { \
  84. return op_<op_##id, op_r, T, self_t>(); \
  85. }
  86. #define PYBIND11_INPLACE_OPERATOR(id, op, expr) \
  87. template <typename B, typename L, typename R> struct op_impl<op_##id, op_l, B, L, R> { \
  88. static char const* name() { return "__" #id "__"; } \
  89. static auto execute(L &l, const R &r) -> decltype(expr) { return expr; } \
  90. static B execute_cast(L &l, const R &r) { return B(expr); } \
  91. }; \
  92. template <typename T> op_<op_##id, op_l, self_t, T> op(const self_t &, const T &) { \
  93. return op_<op_##id, op_l, self_t, T>(); \
  94. }
  95. #define PYBIND11_UNARY_OPERATOR(id, op, expr) \
  96. template <typename B, typename L> struct op_impl<op_##id, op_u, B, L, undefined_t> { \
  97. static char const* name() { return "__" #id "__"; } \
  98. static auto execute(const L &l) -> decltype(expr) { return expr; } \
  99. static B execute_cast(const L &l) { return B(expr); } \
  100. }; \
  101. inline op_<op_##id, op_u, self_t, undefined_t> op(const self_t &) { \
  102. return op_<op_##id, op_u, self_t, undefined_t>(); \
  103. }
  104. PYBIND11_BINARY_OPERATOR(sub, rsub, operator-, l - r)
  105. PYBIND11_BINARY_OPERATOR(add, radd, operator+, l + r)
  106. PYBIND11_BINARY_OPERATOR(mul, rmul, operator*, l * r)
  107. PYBIND11_BINARY_OPERATOR(truediv, rtruediv, operator/, l / r)
  108. PYBIND11_BINARY_OPERATOR(mod, rmod, operator%, l % r)
  109. PYBIND11_BINARY_OPERATOR(lshift, rlshift, operator<<, l << r)
  110. PYBIND11_BINARY_OPERATOR(rshift, rrshift, operator>>, l >> r)
  111. PYBIND11_BINARY_OPERATOR(and, rand, operator&, l & r)
  112. PYBIND11_BINARY_OPERATOR(xor, rxor, operator^, l ^ r)
  113. PYBIND11_BINARY_OPERATOR(eq, eq, operator==, l == r)
  114. PYBIND11_BINARY_OPERATOR(ne, ne, operator!=, l != r)
  115. PYBIND11_BINARY_OPERATOR(or, ror, operator|, l | r)
  116. PYBIND11_BINARY_OPERATOR(gt, lt, operator>, l > r)
  117. PYBIND11_BINARY_OPERATOR(ge, le, operator>=, l >= r)
  118. PYBIND11_BINARY_OPERATOR(lt, gt, operator<, l < r)
  119. PYBIND11_BINARY_OPERATOR(le, ge, operator<=, l <= r)
  120. //PYBIND11_BINARY_OPERATOR(pow, rpow, pow, std::pow(l, r))
  121. PYBIND11_INPLACE_OPERATOR(iadd, operator+=, l += r)
  122. PYBIND11_INPLACE_OPERATOR(isub, operator-=, l -= r)
  123. PYBIND11_INPLACE_OPERATOR(imul, operator*=, l *= r)
  124. PYBIND11_INPLACE_OPERATOR(itruediv, operator/=, l /= r)
  125. PYBIND11_INPLACE_OPERATOR(imod, operator%=, l %= r)
  126. PYBIND11_INPLACE_OPERATOR(ilshift, operator<<=, l <<= r)
  127. PYBIND11_INPLACE_OPERATOR(irshift, operator>>=, l >>= r)
  128. PYBIND11_INPLACE_OPERATOR(iand, operator&=, l &= r)
  129. PYBIND11_INPLACE_OPERATOR(ixor, operator^=, l ^= r)
  130. PYBIND11_INPLACE_OPERATOR(ior, operator|=, l |= r)
  131. PYBIND11_UNARY_OPERATOR(neg, operator-, -l)
  132. PYBIND11_UNARY_OPERATOR(pos, operator+, +l)
  133. PYBIND11_UNARY_OPERATOR(abs, abs, std::abs(l))
  134. PYBIND11_UNARY_OPERATOR(hash, hash, std::hash<L>()(l))
  135. PYBIND11_UNARY_OPERATOR(invert, operator~, (~l))
  136. PYBIND11_UNARY_OPERATOR(bool, operator!, !!l)
  137. PYBIND11_UNARY_OPERATOR(int, int_, (int) l)
  138. PYBIND11_UNARY_OPERATOR(float, float_, (double) l)
  139. #undef PYBIND11_BINARY_OPERATOR
  140. #undef PYBIND11_INPLACE_OPERATOR
  141. #undef PYBIND11_UNARY_OPERATOR
  142. NAMESPACE_END(detail)
  143. using detail::self;
  144. NAMESPACE_END(PYBIND11_NAMESPACE)
  145. #if defined(_MSC_VER)
  146. # pragma warning(pop)
  147. #endif