operators.h 10.0 KB

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