From 1723c5a831e28832d6618f938f88f5c2c7bd188d Mon Sep 17 00:00:00 2001 From: Zachary Wimer Date: Mon, 10 Oct 2022 16:00:30 -0700 Subject: [PATCH] adding support for operators `~ | & ^ << >> |= &= ^= <<= >>=` --- source/function.cpp | 11 +++++++++++ test/T12.operator.hpp | 12 ++++++++++++ test/T12.operator.ref | 11 +++++++++++ 3 files changed, 34 insertions(+) diff --git a/source/function.cpp b/source/function.cpp index fe9788e7..6d158a41 100644 --- a/source/function.cpp +++ b/source/function.cpp @@ -46,12 +46,23 @@ static std::map const cpp_python_operator_map{ {"operator*", "__mul__"}, // {"operator/", "__truediv__"}, // {"operator%", "__mod__"}, // + {"operator~", "__invert__"}, // + {"operator|", "__or__"}, // + {"operator&", "__and__"}, // + {"operator^", "__xor__"}, // + {"operator<<", "__lshift__"}, // + {"operator>>", "__rshift__"}, // {"operator+=", "__iadd__"}, // {"operator-=", "__isub__"}, // {"operator*=", "__imul__"}, // {"operator/=", "__itruediv__"}, // {"operator%=", "__imod__"}, // + {"operator|=", "__ior__"}, // + {"operator&=", "__iand__"}, // + {"operator^=", "__ixor__"}, // + {"operator<<=", "__ilshift__"}, // + {"operator>>=", "__irshift__"}, // {"operator()", "__call__"}, // {"operator==", "__eq__"}, // diff --git a/test/T12.operator.hpp b/test/T12.operator.hpp index 23f1b542..5d185b49 100644 --- a/test/T12.operator.hpp +++ b/test/T12.operator.hpp @@ -14,17 +14,29 @@ struct T { + T &operator~() { return *this; } + T &operator+(int) { return *this; } T &operator-(int) { return *this; } T &operator*(int) { return *this; } T &operator/(int) { return *this; } T &operator%(int) { return *this; } + T &operator|(int) { return *this; } + T &operator&(int) { return *this; } + T &operator^(int) { return *this; } + T &operator<<(int) { return *this; } + T &operator>>(int) { return *this; } void operator+=(int) {} void operator-=(int) {} void operator*=(int) {} void operator/=(int) {} void operator%=(int) {} + void operator|=(int) {} + void operator&=(int) {} + void operator^=(int) {} + void operator<<=(int) {} + void operator>>=(int) {} void operator()(int) {} diff --git a/test/T12.operator.ref b/test/T12.operator.ref index 17b23610..94a03923 100644 --- a/test/T12.operator.ref +++ b/test/T12.operator.ref @@ -18,16 +18,27 @@ void bind_T12_operator(std::function< pybind11::module &(std::string const &name { // T file:T12.operator.hpp line:15 pybind11::class_> cl(M(""), "T", ""); cl.def( pybind11::init( [](){ return new T(); } ) ); + cl.def("__invert__", (struct T & (T::*)()) &T::operator~, "C++: T::operator~() --> struct T &", pybind11::return_value_policy::automatic); cl.def("__add__", (struct T & (T::*)(int)) &T::operator+, "C++: T::operator+(int) --> struct T &", pybind11::return_value_policy::automatic, pybind11::arg("")); cl.def("__sub__", (struct T & (T::*)(int)) &T::operator-, "C++: T::operator-(int) --> struct T &", pybind11::return_value_policy::automatic, pybind11::arg("")); cl.def("__mul__", (struct T & (T::*)(int)) &T::operator*, "C++: T::operator*(int) --> struct T &", pybind11::return_value_policy::automatic, pybind11::arg("")); cl.def("__truediv__", (struct T & (T::*)(int)) &T::operator/, "C++: T::operator/(int) --> struct T &", pybind11::return_value_policy::automatic, pybind11::arg("")); cl.def("__mod__", (struct T & (T::*)(int)) &T::operator%, "C++: T::operator%(int) --> struct T &", pybind11::return_value_policy::automatic, pybind11::arg("")); + cl.def("__or__", (struct T & (T::*)(int)) &T::operator|, "C++: T::operator|(int) --> struct T &", pybind11::return_value_policy::automatic, pybind11::arg("")); + cl.def("__and__", (struct T & (T::*)(int)) &T::operator&, "C++: T::operator&(int) --> struct T &", pybind11::return_value_policy::automatic, pybind11::arg("")); + cl.def("__xor__", (struct T & (T::*)(int)) &T::operator^, "C++: T::operator^(int) --> struct T &", pybind11::return_value_policy::automatic, pybind11::arg("")); + cl.def("__lshift__", (struct T & (T::*)(int)) &T::operator<<, "C++: T::operator<<(int) --> struct T &", pybind11::return_value_policy::automatic, pybind11::arg("")); + cl.def("__rshift__", (struct T & (T::*)(int)) &T::operator>>, "C++: T::operator>>(int) --> struct T &", pybind11::return_value_policy::automatic, pybind11::arg("")); cl.def("__iadd__", (void (T::*)(int)) &T::operator+=, "C++: T::operator+=(int) --> void", pybind11::arg("")); cl.def("__isub__", (void (T::*)(int)) &T::operator-=, "C++: T::operator-=(int) --> void", pybind11::arg("")); cl.def("__imul__", (void (T::*)(int)) &T::operator*=, "C++: T::operator*=(int) --> void", pybind11::arg("")); cl.def("__itruediv__", (void (T::*)(int)) &T::operator/=, "C++: T::operator/=(int) --> void", pybind11::arg("")); cl.def("__imod__", (void (T::*)(int)) &T::operator%=, "C++: T::operator%=(int) --> void", pybind11::arg("")); + cl.def("__ior__", (void (T::*)(int)) &T::operator|=, "C++: T::operator|=(int) --> void", pybind11::arg("")); + cl.def("__iand__", (void (T::*)(int)) &T::operator&=, "C++: T::operator&=(int) --> void", pybind11::arg("")); + cl.def("__ixor__", (void (T::*)(int)) &T::operator^=, "C++: T::operator^=(int) --> void", pybind11::arg("")); + cl.def("__ilshift__", (void (T::*)(int)) &T::operator<<=, "C++: T::operator<<=(int) --> void", pybind11::arg("")); + cl.def("__irshift__", (void (T::*)(int)) &T::operator>>=, "C++: T::operator>>=(int) --> void", pybind11::arg("")); cl.def("__call__", (void (T::*)(int)) &T::operator(), "C++: T::operator()(int) --> void", pybind11::arg("")); cl.def("__eq__", (bool (T::*)(const struct T &)) &T::operator==, "C++: T::operator==(const struct T &) --> bool", pybind11::arg("")); cl.def("__ne__", (bool (T::*)(const struct T &)) &T::operator!=, "C++: T::operator!=(const struct T &) --> bool", pybind11::arg(""));