You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
8.0 KiB

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