2015-07-05 18:05:44 +00:00
|
|
|
/*
|
2015-10-15 16:13:33 +00:00
|
|
|
pybind11/operator.h: Metatemplates for operator overloading
|
2015-07-05 18:05:44 +00:00
|
|
|
|
2016-04-17 18:21:41 +00:00
|
|
|
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
|
2015-07-05 18:05:44 +00:00
|
|
|
|
|
|
|
All rights reserved. Use of this source code is governed by a
|
|
|
|
BSD-style license that can be found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2015-07-11 15:41:48 +00:00
|
|
|
#pragma once
|
2015-07-05 18:05:44 +00:00
|
|
|
|
2015-10-15 16:13:33 +00:00
|
|
|
#include "pybind11.h"
|
2015-07-05 18:05:44 +00:00
|
|
|
|
2016-08-26 14:50:11 +00:00
|
|
|
#if defined(__clang__) && !defined(__INTEL_COMPILER)
|
2016-02-16 12:36:04 +00:00
|
|
|
# pragma clang diagnostic ignored "-Wunsequenced" // multiple unsequenced modifications to 'self' (when using def(py::self OP Type()))
|
2017-05-21 00:34:51 +00:00
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
# pragma warning(push)
|
|
|
|
# pragma warning(disable: 4127) // warning C4127: Conditional expression is constant
|
2016-02-16 12:36:04 +00:00
|
|
|
#endif
|
|
|
|
|
2015-10-15 16:13:33 +00:00
|
|
|
NAMESPACE_BEGIN(pybind11)
|
2015-07-05 18:05:44 +00:00
|
|
|
NAMESPACE_BEGIN(detail)
|
|
|
|
|
|
|
|
/// Enumeration with all supported operator types
|
|
|
|
enum op_id : int {
|
2015-07-26 14:33:49 +00:00
|
|
|
op_add, op_sub, op_mul, op_div, op_mod, op_divmod, op_pow, op_lshift,
|
|
|
|
op_rshift, op_and, op_xor, op_or, op_neg, op_pos, op_abs, op_invert,
|
|
|
|
op_int, op_long, op_float, op_str, op_cmp, op_gt, op_ge, op_lt, op_le,
|
|
|
|
op_eq, op_ne, op_iadd, op_isub, op_imul, op_idiv, op_imod, op_ilshift,
|
|
|
|
op_irshift, op_iand, op_ixor, op_ior, op_complex, op_bool, op_nonzero,
|
2017-05-21 00:19:26 +00:00
|
|
|
op_repr, op_truediv, op_itruediv
|
2015-07-05 18:05:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum op_type : int {
|
|
|
|
op_l, /* base type on left */
|
|
|
|
op_r, /* base type on right */
|
|
|
|
op_u /* unary operator */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct self_t { };
|
2015-07-26 14:33:49 +00:00
|
|
|
static const self_t self = self_t();
|
2015-07-05 18:05:44 +00:00
|
|
|
|
|
|
|
/// Type for an unused type slot
|
|
|
|
struct undefined_t { };
|
|
|
|
|
|
|
|
/// Don't warn about an unused variable
|
|
|
|
inline self_t __self() { return self; }
|
|
|
|
|
|
|
|
/// base template of operator implementations
|
|
|
|
template <op_id, op_type, typename B, typename L, typename R> struct op_impl { };
|
|
|
|
|
|
|
|
/// Operator implementation generator
|
|
|
|
template <op_id id, op_type ot, typename L, typename R> struct op_ {
|
Allow arbitrary class_ template option ordering
The current pybind11::class_<Type, Holder, Trampoline> fixed template
ordering results in a requirement to repeat the Holder with its default
value (std::unique_ptr<Type>) argument, which is a little bit annoying:
it needs to be specified not because we want to override the default,
but rather because we need to specify the third argument.
This commit removes this limitation by making the class_ template take
the type name plus a parameter pack of options. It then extracts the
first valid holder type and the first subclass type for holder_type and
trampoline type_alias, respectively. (If unfound, both fall back to
their current defaults, `std::unique_ptr<type>` and `type`,
respectively). If any unmatched template arguments are provided, a
static assertion fails.
What this means is that you can specify or omit the arguments in any
order:
py::class_<A, PyA> c1(m, "A");
py::class_<B, PyB, std::shared_ptr<B>> c2(m, "B");
py::class_<C, std::shared_ptr<C>, PyB> c3(m, "C");
It also allows future class attributes (such as base types in the next
commit) to be passed as class template types rather than needing to use
a py::base<> wrapper.
2016-09-06 16:17:06 +00:00
|
|
|
template <typename Class, typename... Extra> void execute(Class &cl, const Extra&... extra) const {
|
2017-05-21 00:33:18 +00:00
|
|
|
using Base = typename Class::type;
|
|
|
|
using L_type = conditional_t<std::is_same<L, self_t>::value, Base, L>;
|
|
|
|
using R_type = conditional_t<std::is_same<R, self_t>::value, Base, R>;
|
|
|
|
using op = op_impl<id, ot, Base, L_type, R_type>;
|
2016-09-10 06:28:37 +00:00
|
|
|
cl.def(op::name(), &op::execute, is_operator(), extra...);
|
2017-05-21 00:34:51 +00:00
|
|
|
#if PY_MAJOR_VERSION < 3
|
|
|
|
if (id == op_truediv || id == op_itruediv)
|
|
|
|
cl.def(id == op_itruediv ? "__idiv__" : ot == op_l ? "__div__" : "__rdiv__",
|
|
|
|
&op::execute, is_operator(), extra...);
|
|
|
|
#endif
|
2015-07-05 18:05:44 +00:00
|
|
|
}
|
Allow arbitrary class_ template option ordering
The current pybind11::class_<Type, Holder, Trampoline> fixed template
ordering results in a requirement to repeat the Holder with its default
value (std::unique_ptr<Type>) argument, which is a little bit annoying:
it needs to be specified not because we want to override the default,
but rather because we need to specify the third argument.
This commit removes this limitation by making the class_ template take
the type name plus a parameter pack of options. It then extracts the
first valid holder type and the first subclass type for holder_type and
trampoline type_alias, respectively. (If unfound, both fall back to
their current defaults, `std::unique_ptr<type>` and `type`,
respectively). If any unmatched template arguments are provided, a
static assertion fails.
What this means is that you can specify or omit the arguments in any
order:
py::class_<A, PyA> c1(m, "A");
py::class_<B, PyB, std::shared_ptr<B>> c2(m, "B");
py::class_<C, std::shared_ptr<C>, PyB> c3(m, "C");
It also allows future class attributes (such as base types in the next
commit) to be passed as class template types rather than needing to use
a py::base<> wrapper.
2016-09-06 16:17:06 +00:00
|
|
|
template <typename Class, typename... Extra> void execute_cast(Class &cl, const Extra&... extra) const {
|
2017-05-21 00:33:18 +00:00
|
|
|
using Base = typename Class::type;
|
|
|
|
using L_type = conditional_t<std::is_same<L, self_t>::value, Base, L>;
|
|
|
|
using R_type = conditional_t<std::is_same<R, self_t>::value, Base, R>;
|
|
|
|
using op = op_impl<id, ot, Base, L_type, R_type>;
|
2016-09-10 06:28:37 +00:00
|
|
|
cl.def(op::name(), &op::execute_cast, is_operator(), extra...);
|
2017-05-21 00:34:51 +00:00
|
|
|
#if PY_MAJOR_VERSION < 3
|
|
|
|
if (id == op_truediv || id == op_itruediv)
|
|
|
|
cl.def(id == op_itruediv ? "__idiv__" : ot == op_l ? "__div__" : "__rdiv__",
|
|
|
|
&op::execute, is_operator(), extra...);
|
|
|
|
#endif
|
2015-07-05 18:05:44 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-05-21 00:33:18 +00:00
|
|
|
#define PYBIND11_BINARY_OPERATOR(id, rid, op, expr) \
|
2015-07-05 18:05:44 +00:00
|
|
|
template <typename B, typename L, typename R> struct op_impl<op_##id, op_l, B, L, R> { \
|
|
|
|
static char const* name() { return "__" #id "__"; } \
|
|
|
|
static auto execute(const L &l, const R &r) -> decltype(expr) { return (expr); } \
|
|
|
|
static B execute_cast(const L &l, const R &r) { return B(expr); } \
|
|
|
|
}; \
|
|
|
|
template <typename B, typename L, typename R> struct op_impl<op_##id, op_r, B, L, R> { \
|
|
|
|
static char const* name() { return "__" #rid "__"; } \
|
2015-09-11 15:09:47 +00:00
|
|
|
static auto execute(const R &r, const L &l) -> decltype(expr) { return (expr); } \
|
|
|
|
static B execute_cast(const R &r, const L &l) { return B(expr); } \
|
2015-07-05 18:05:44 +00:00
|
|
|
}; \
|
|
|
|
inline op_<op_##id, op_l, self_t, self_t> op(const self_t &, const self_t &) { \
|
|
|
|
return op_<op_##id, op_l, self_t, self_t>(); \
|
2016-02-18 18:20:15 +00:00
|
|
|
} \
|
2015-07-05 18:05:44 +00:00
|
|
|
template <typename T> op_<op_##id, op_l, self_t, T> op(const self_t &, const T &) { \
|
|
|
|
return op_<op_##id, op_l, self_t, T>(); \
|
2016-02-18 18:20:15 +00:00
|
|
|
} \
|
2015-07-05 18:05:44 +00:00
|
|
|
template <typename T> op_<op_##id, op_r, T, self_t> op(const T &, const self_t &) { \
|
|
|
|
return op_<op_##id, op_r, T, self_t>(); \
|
2016-02-18 18:20:15 +00:00
|
|
|
}
|
2015-07-05 18:05:44 +00:00
|
|
|
|
2017-05-21 00:33:18 +00:00
|
|
|
#define PYBIND11_INPLACE_OPERATOR(id, op, expr) \
|
2015-07-05 18:05:44 +00:00
|
|
|
template <typename B, typename L, typename R> struct op_impl<op_##id, op_l, B, L, R> { \
|
|
|
|
static char const* name() { return "__" #id "__"; } \
|
|
|
|
static auto execute(L &l, const R &r) -> decltype(expr) { return expr; } \
|
|
|
|
static B execute_cast(L &l, const R &r) { return B(expr); } \
|
|
|
|
}; \
|
|
|
|
template <typename T> op_<op_##id, op_l, self_t, T> op(const self_t &, const T &) { \
|
|
|
|
return op_<op_##id, op_l, self_t, T>(); \
|
2016-02-18 18:20:15 +00:00
|
|
|
}
|
2015-07-05 18:05:44 +00:00
|
|
|
|
2017-05-21 00:33:18 +00:00
|
|
|
#define PYBIND11_UNARY_OPERATOR(id, op, expr) \
|
2015-07-05 18:05:44 +00:00
|
|
|
template <typename B, typename L> struct op_impl<op_##id, op_u, B, L, undefined_t> { \
|
|
|
|
static char const* name() { return "__" #id "__"; } \
|
|
|
|
static auto execute(const L &l) -> decltype(expr) { return expr; } \
|
|
|
|
static B execute_cast(const L &l) { return B(expr); } \
|
|
|
|
}; \
|
|
|
|
inline op_<op_##id, op_u, self_t, undefined_t> op(const self_t &) { \
|
|
|
|
return op_<op_##id, op_u, self_t, undefined_t>(); \
|
2016-02-18 18:20:15 +00:00
|
|
|
}
|
2015-07-05 18:05:44 +00:00
|
|
|
|
2015-10-18 14:48:30 +00:00
|
|
|
PYBIND11_BINARY_OPERATOR(sub, rsub, operator-, l - r)
|
|
|
|
PYBIND11_BINARY_OPERATOR(add, radd, operator+, l + r)
|
|
|
|
PYBIND11_BINARY_OPERATOR(mul, rmul, operator*, l * r)
|
|
|
|
PYBIND11_BINARY_OPERATOR(truediv, rtruediv, operator/, l / r)
|
|
|
|
PYBIND11_BINARY_OPERATOR(mod, rmod, operator%, l % r)
|
|
|
|
PYBIND11_BINARY_OPERATOR(lshift, rlshift, operator<<, l << r)
|
|
|
|
PYBIND11_BINARY_OPERATOR(rshift, rrshift, operator>>, l >> r)
|
|
|
|
PYBIND11_BINARY_OPERATOR(and, rand, operator&, l & r)
|
|
|
|
PYBIND11_BINARY_OPERATOR(xor, rxor, operator^, l ^ r)
|
|
|
|
PYBIND11_BINARY_OPERATOR(eq, eq, operator==, l == r)
|
|
|
|
PYBIND11_BINARY_OPERATOR(ne, ne, operator!=, l != r)
|
|
|
|
PYBIND11_BINARY_OPERATOR(or, ror, operator|, l | r)
|
|
|
|
PYBIND11_BINARY_OPERATOR(gt, lt, operator>, l > r)
|
|
|
|
PYBIND11_BINARY_OPERATOR(ge, le, operator>=, l >= r)
|
|
|
|
PYBIND11_BINARY_OPERATOR(lt, gt, operator<, l < r)
|
|
|
|
PYBIND11_BINARY_OPERATOR(le, ge, operator<=, l <= r)
|
|
|
|
//PYBIND11_BINARY_OPERATOR(pow, rpow, pow, std::pow(l, r))
|
|
|
|
PYBIND11_INPLACE_OPERATOR(iadd, operator+=, l += r)
|
|
|
|
PYBIND11_INPLACE_OPERATOR(isub, operator-=, l -= r)
|
|
|
|
PYBIND11_INPLACE_OPERATOR(imul, operator*=, l *= r)
|
2017-05-21 00:19:26 +00:00
|
|
|
PYBIND11_INPLACE_OPERATOR(itruediv, operator/=, l /= r)
|
2015-10-18 14:48:30 +00:00
|
|
|
PYBIND11_INPLACE_OPERATOR(imod, operator%=, l %= r)
|
|
|
|
PYBIND11_INPLACE_OPERATOR(ilshift, operator<<=, l <<= r)
|
|
|
|
PYBIND11_INPLACE_OPERATOR(irshift, operator>>=, l >>= r)
|
|
|
|
PYBIND11_INPLACE_OPERATOR(iand, operator&=, l &= r)
|
|
|
|
PYBIND11_INPLACE_OPERATOR(ixor, operator^=, l ^= r)
|
|
|
|
PYBIND11_INPLACE_OPERATOR(ior, operator|=, l |= r)
|
|
|
|
PYBIND11_UNARY_OPERATOR(neg, operator-, -l)
|
|
|
|
PYBIND11_UNARY_OPERATOR(pos, operator+, +l)
|
|
|
|
PYBIND11_UNARY_OPERATOR(abs, abs, std::abs(l))
|
2015-10-22 19:38:11 +00:00
|
|
|
PYBIND11_UNARY_OPERATOR(invert, operator~, (~l))
|
2015-10-18 14:48:30 +00:00
|
|
|
PYBIND11_UNARY_OPERATOR(bool, operator!, !!l)
|
|
|
|
PYBIND11_UNARY_OPERATOR(int, int_, (int) l)
|
|
|
|
PYBIND11_UNARY_OPERATOR(float, float_, (double) l)
|
|
|
|
|
|
|
|
#undef PYBIND11_BINARY_OPERATOR
|
|
|
|
#undef PYBIND11_INPLACE_OPERATOR
|
|
|
|
#undef PYBIND11_UNARY_OPERATOR
|
2015-07-05 18:05:44 +00:00
|
|
|
NAMESPACE_END(detail)
|
|
|
|
|
|
|
|
using detail::self;
|
|
|
|
|
2015-10-15 16:13:33 +00:00
|
|
|
NAMESPACE_END(pybind11)
|
2017-05-21 00:34:51 +00:00
|
|
|
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
# pragma warning(pop)
|
|
|
|
#endif
|