From c7e12ecf3dba222e07728340588c75b7c4198cf9 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Wed, 18 Jul 2001 15:24:41 +0000 Subject: [PATCH] richcmp3.cpp significantly simplified. [SVN r10660] --- example/pickle3.cpp | 4 +- example/richcmp1.cpp | 18 +-- example/richcmp2.cpp | 2 +- example/richcmp3.cpp | 279 +++++++++++++++------------------------- example/tst_richcmp1.py | 2 + 5 files changed, 118 insertions(+), 187 deletions(-) diff --git a/example/pickle3.cpp b/example/pickle3.cpp index bfa7dc54..cb598ae6 100644 --- a/example/pickle3.cpp +++ b/example/pickle3.cpp @@ -106,7 +106,7 @@ namespace { { if(args.size() != 1 || keywords.size() != 0) { PyErr_SetString(PyExc_TypeError, "wrong number of arguments"); - throw boost::python::argument_error(); + throw boost::python::error_already_set(); } const world& w = from_python(args[0].get(), type()); ref mydict = getattr(args[0], "__dict__"); @@ -122,7 +122,7 @@ namespace { { if(args.size() != 2 || keywords.size() != 0) { PyErr_SetString(PyExc_TypeError, "wrong number of arguments"); - throw boost::python::argument_error(); + throw boost::python::error_already_set(); } world& w = from_python(args[0].get(), type()); ref mydict = getattr(args[0], "__dict__"); diff --git a/example/richcmp1.cpp b/example/richcmp1.cpp index 62997f10..4a9cafff 100644 --- a/example/richcmp1.cpp +++ b/example/richcmp1.cpp @@ -33,7 +33,10 @@ namespace vects { friend std::vector \ operator##oper(const dvect& lhs, const dvect& rhs) \ { \ - if (lhs.size()!=rhs.size()){throw boost::python::argument_error();} \ + if (lhs.size() != rhs.size()) { \ + PyErr_SetString(PyExc_ValueError, "vectors have different sizes"); \ + throw boost::python::error_already_set(); \ + } \ std::vector result(lhs.size()); \ for (std::size_t i=0; i py_dvect(this_module, "dvect"); py_dvect.def(boost::python::constructor()); py_dvect.def(&vects::dvect::as_tuple, "as_tuple"); - const long comp_operators = - ( boost::python::op_lt | boost::python::op_le - | boost::python::op_eq | boost::python::op_ne - | boost::python::op_gt | boost::python::op_ge); + const long + comp_operators = ( boost::python::op_lt | boost::python::op_le + | boost::python::op_eq | boost::python::op_ne + | boost::python::op_gt | boost::python::op_ge); py_dvect.def(boost::python::operators()); } @@ -81,5 +83,5 @@ BOOST_PYTHON_MODULE_INIT(richcmp1) // to suppress a bogus VC60 warning. init_module(this_module); } - catch(...){boost::python::handle_exception();} + catch (...) { boost::python::handle_exception(); } } diff --git a/example/richcmp2.cpp b/example/richcmp2.cpp index a4317bf6..a4d0005a 100644 --- a/example/richcmp2.cpp +++ b/example/richcmp2.cpp @@ -57,5 +57,5 @@ BOOST_PYTHON_MODULE_INIT(richcmp2) // to suppress a bogus VC60 warning. init_module(this_module); } - catch(...){boost::python::handle_exception();} + catch (...) { boost::python::handle_exception(); } } diff --git a/example/richcmp3.cpp b/example/richcmp3.cpp index bb4dbf11..169e7f00 100644 --- a/example/richcmp3.cpp +++ b/example/richcmp3.cpp @@ -1,200 +1,127 @@ -// Example by Nicholas K. Sauter & Ralf W. Grosse-Kunstleve. +// Example by Ralf W. Grosse-Kunstleve & Nicholas K. Sauter. // Comprehensive operator overloading for two vector types and scalars. +#include +#include "vector_wrapper.h" #include "dvect.h" #include "ivect.h" -#include -//------------------ Overload DVECT -------------------------------- - -#define DVECT_BINARY_OPERATORS( oper ) \ +#define VECT_VECT_OPERATORS(result_type, vect_type1, oper, vect_type2) \ namespace vects { \ - dvect operator##oper (const dvect& a, const dvect& b) { \ - if (a.size()!=b.size()){throw boost::python::argument_error();} \ - dvect result(a.size()); \ - dvect::const_iterator a_it = a.begin(); \ - dvect::const_iterator b_it = b.begin(); \ - dvect::iterator r_it = result.begin(); \ - for (int i=0; i ) -DVECT_BINARY_OPERATORS( >= ) -DVECT_BINARY_OPERATORS( < ) -DVECT_BINARY_OPERATORS( <= ) -DVECT_BINARY_OPERATORS( == ) -DVECT_BINARY_OPERATORS( != ) -#undef DVECT_BINARY_OPERATORS -#define DVECT_SCALAR_BINARY_OPERATORS( scalar_type, oper ) \ +#define VECT_SCALAR_OPERATORS(result_type, vect_type, oper, scalar_type) \ namespace vects { \ - dvect operator##oper (const dvect& a, const scalar_type& b) { \ - dvect result(a.size()); \ - dvect::const_iterator a_it = a.begin(); \ - dvect::iterator r_it = result.begin(); \ - for (int i=0; i ) -DVECT_SCALAR_BINARY_OPERATORS( double, >= ) -DVECT_SCALAR_BINARY_OPERATORS( double, < ) -DVECT_SCALAR_BINARY_OPERATORS( double, <= ) -DVECT_SCALAR_BINARY_OPERATORS( double, == ) -DVECT_SCALAR_BINARY_OPERATORS( double, != ) -#undef DVECT_SCALAR_BINARY_OPERATORS -#define SCALAR_DVECT_BINARY_OPERATORS( scalar_type, oper ) \ +#define SCALAR_VECT_OPERATORS(result_type, scalar_type, oper, vect_type) \ namespace vects { \ - dvect operator##oper (const scalar_type& a, const dvect& b) { \ - dvect result(b.size()); \ - dvect::const_iterator b_it = b.begin(); \ - dvect::iterator r_it = result.begin(); \ - for (int i=0; i ) -IVECT_BINARY_OPERATORS( >= ) -IVECT_BINARY_OPERATORS( < ) -IVECT_BINARY_OPERATORS( <= ) -IVECT_BINARY_OPERATORS( == ) -IVECT_BINARY_OPERATORS( != ) -#undef IVECT_BINARY_OPERATORS +#define COMP_VECT_VECT_OPERATORS(vect_type1, vect_type2) \ + VECT_VECT_OPERATORS(std::vector, vect_type1, <, vect_type2) \ + VECT_VECT_OPERATORS(std::vector, vect_type1, <=, vect_type2) \ + VECT_VECT_OPERATORS(std::vector, vect_type1, ==, vect_type2) \ + VECT_VECT_OPERATORS(std::vector, vect_type1, !=, vect_type2) \ + VECT_VECT_OPERATORS(std::vector, vect_type1, >, vect_type2) \ + VECT_VECT_OPERATORS(std::vector, vect_type1, >=, vect_type2) -#define IVECT_SCALAR_BINARY_OPERATORS( scalar_type, oper ) \ -namespace vects { \ - ivect operator##oper (const ivect& a, const scalar_type& b) { \ - ivect result(a.size()); \ - ivect::const_iterator a_it = a.begin(); \ - ivect::iterator r_it = result.begin(); \ - for (int i=0; i ) -IVECT_SCALAR_BINARY_OPERATORS( int, >= ) -IVECT_SCALAR_BINARY_OPERATORS( int, < ) -IVECT_SCALAR_BINARY_OPERATORS( int, <= ) -IVECT_SCALAR_BINARY_OPERATORS( int, == ) -IVECT_SCALAR_BINARY_OPERATORS( int, != ) -#undef IVECT_SCALAR_BINARY_OPERATORS +#define MATH_VECT_SCALAR_OPERATORS(result_type, vect_type, scalar_type) \ + VECT_SCALAR_OPERATORS(result_type, vect_type, +, scalar_type) \ + VECT_SCALAR_OPERATORS(result_type, vect_type, -, scalar_type) \ + VECT_SCALAR_OPERATORS(result_type, vect_type, *, scalar_type) \ + VECT_SCALAR_OPERATORS(result_type, vect_type, /, scalar_type) -#define SCALAR_IVECT_BINARY_OPERATORS( scalar_type, oper ) \ -namespace vects { \ - ivect operator##oper (const scalar_type& a, const ivect& b) { \ - ivect result(b.size()); \ - ivect::const_iterator b_it = b.begin(); \ - ivect::iterator r_it = result.begin(); \ - for (int i=0; i, vect_type, <, scalar_type) \ + VECT_SCALAR_OPERATORS(std::vector, vect_type, <=, scalar_type) \ + VECT_SCALAR_OPERATORS(std::vector, vect_type, ==, scalar_type) \ + VECT_SCALAR_OPERATORS(std::vector, vect_type, !=, scalar_type) \ + VECT_SCALAR_OPERATORS(std::vector, vect_type, >, scalar_type) \ + VECT_SCALAR_OPERATORS(std::vector, vect_type, >=, scalar_type) -//------------------ IVECT & DVECT Binary ops --------------------------- +#define MATH_SCALAR_VECT_OPERATORS(result_type, scalar_type, vect_type) \ + SCALAR_VECT_OPERATORS(result_type, scalar_type, +, vect_type) \ + SCALAR_VECT_OPERATORS(result_type, scalar_type, -, vect_type) \ + SCALAR_VECT_OPERATORS(result_type, scalar_type, *, vect_type) \ + SCALAR_VECT_OPERATORS(result_type, scalar_type, /, vect_type) -#define DI_BINARY_OPERATORS( oper ) \ -namespace vects { \ - dvect operator##oper (const dvect& a, const ivect& b) {\ - if (a.size()!=b.size()){throw boost::python::argument_error();} \ - dvect result(a.size()); \ - dvect::const_iterator a_it = a.begin(); \ - ivect::const_iterator b_it = b.begin(); \ - dvect::iterator r_it = result.begin(); \ - for (int i=0; i ) -DI_BINARY_OPERATORS( >= ) -DI_BINARY_OPERATORS( < ) -DI_BINARY_OPERATORS( <= ) -DI_BINARY_OPERATORS( == ) -DI_BINARY_OPERATORS( != ) -#undef DI_BINARY_OPERATORS +MATH_VECT_VECT_OPERATORS(dvect, dvect, dvect) +COMP_VECT_VECT_OPERATORS( dvect, dvect) +MATH_VECT_SCALAR_OPERATORS(dvect, dvect, double) +COMP_VECT_SCALAR_OPERATORS( dvect, double) +MATH_SCALAR_VECT_OPERATORS(dvect, double, dvect) +// comparison operators not needed since Python uses reflection -#define ID_BINARY_OPERATORS( oper ) \ -namespace vects { \ - dvect operator##oper (const ivect& a, const dvect& b) { \ - if (a.size()!=b.size()){throw boost::python::argument_error();} \ - dvect result(a.size()); \ - ivect::const_iterator a_it = a.begin(); \ - dvect::const_iterator b_it = b.begin(); \ - dvect::iterator r_it = result.begin(); \ - for (int i=0; i ) -ID_BINARY_OPERATORS( >= ) -ID_BINARY_OPERATORS( < ) -ID_BINARY_OPERATORS( <= ) -ID_BINARY_OPERATORS( == ) -ID_BINARY_OPERATORS( != ) -#undef ID_BINARY_OPERATORS +MATH_VECT_VECT_OPERATORS(ivect, ivect, ivect) +COMP_VECT_VECT_OPERATORS( ivect, ivect) +MATH_VECT_SCALAR_OPERATORS(ivect, ivect, int) +COMP_VECT_SCALAR_OPERATORS( ivect, int) +MATH_SCALAR_VECT_OPERATORS(ivect, int, ivect) +// comparison operators not needed since Python uses reflection -//-------------------- Module --------------------------------------- -#define all_operators (boost::python::op_mul | boost::python::op_add |\ - boost::python::op_div | boost::python::op_sub ) +MATH_VECT_VECT_OPERATORS(dvect, dvect, ivect) +COMP_VECT_VECT_OPERATORS( dvect, ivect) +MATH_VECT_VECT_OPERATORS(dvect, ivect, dvect) +COMP_VECT_VECT_OPERATORS( ivect, dvect) -#define comp_operators (boost::python::op_gt | boost::python::op_ge |\ - boost::python::op_lt | boost::python::op_le |\ - boost::python::op_eq | boost::python::op_ne) +#undef VECT_VECT_OPERATORS +#undef SCALAR_VECT_OPERATORS +#undef VECT_SCALAR_OPERATORS +#undef MATH_VECT_VECT_OPERATORS +#undef COMP_VECT_VECT_OPERATORS +#undef MATH_VECT_SCALAR_OPERATORS +#undef COMP_VECT_SCALAR_OPERATORS +#undef MATH_SCALAR_VECT_OPERATORS namespace { void init_module(boost::python::module_builder& this_module) { + (void) example::wrap_vector(this_module, "vector_of_bool", bool()); + + const long + math_operators ( boost::python::op_mul | boost::python::op_add + | boost::python::op_div | boost::python::op_sub); + const long + comp_operators = ( boost::python::op_lt | boost::python::op_le + | boost::python::op_eq | boost::python::op_ne + | boost::python::op_gt | boost::python::op_ge); + boost::python::class_builder dvect_class(this_module, "dvect"); boost::python::class_builder @@ -203,12 +130,12 @@ namespace { dvect_class.def(boost::python::constructor()); dvect_class.def(&vects::dvect::as_tuple,"as_tuple"); - dvect_class.def(boost::python::operators()); - dvect_class.def(boost::python::operators(), + dvect_class.def(boost::python::operators()); + dvect_class.def(boost::python::operators(), boost::python::right_operand() ); - dvect_class.def(boost::python::operators(), + dvect_class.def(boost::python::operators(), boost::python::left_operand() ); - dvect_class.def(boost::python::operators(), + dvect_class.def(boost::python::operators(), boost::python::right_operand() ); dvect_class.def(boost::python::operators()); @@ -221,12 +148,12 @@ namespace { ivect_class.def(boost::python::constructor()); ivect_class.def(&vects::ivect::as_tuple,"as_tuple"); - ivect_class.def(boost::python::operators()); - ivect_class.def(boost::python::operators(), + ivect_class.def(boost::python::operators()); + ivect_class.def(boost::python::operators(), boost::python::right_operand() ); - ivect_class.def(boost::python::operators(), + ivect_class.def(boost::python::operators(), boost::python::left_operand() ); - ivect_class.def(boost::python::operators(), + ivect_class.def(boost::python::operators(), boost::python::right_operand() ); ivect_class.def(boost::python::operators()); @@ -247,5 +174,5 @@ BOOST_PYTHON_MODULE_INIT(richcmp3) // to suppress a bogus VC60 warning. init_module(this_module); } - catch(...){boost::python::handle_exception();} + catch (...) { boost::python::handle_exception(); } } diff --git a/example/tst_richcmp1.py b/example/tst_richcmp1.py index b1227196..53264afe 100644 --- a/example/tst_richcmp1.py +++ b/example/tst_richcmp1.py @@ -9,3 +9,5 @@ print (d1 == d2).as_tuple() print (d1 != d2).as_tuple() print (d1 > d2).as_tuple() print (d1 >= d2).as_tuple() +try: d1 == richcmp1.dvect((1, 2, 3, 4, 5)) +except ValueError, e: print str(e)