From 5031479fa24d908319e1bf6cdb124045c0f6c388 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Wed, 18 Jul 2001 13:16:51 +0000 Subject: [PATCH] test_richcmp1.py now works with vc60. [SVN r10657] --- example/richcmp1.cpp | 76 ++++++++++++++++++++++++++-------------- example/richcmp2.cpp | 2 +- example/richcmp3.cpp | 2 +- example/tst_richcmp1.py | 4 +-- example/vector_wrapper.h | 28 +++++++-------- 5 files changed, 67 insertions(+), 45 deletions(-) diff --git a/example/richcmp1.cpp b/example/richcmp1.cpp index c24f25a1..62997f10 100644 --- a/example/richcmp1.cpp +++ b/example/richcmp1.cpp @@ -6,29 +6,51 @@ #include #include "vector_wrapper.h" -namespace std { +namespace vects { -# define VECTOR_BINARY_OPERATORS(oper) \ - template \ - std::vector \ - operator##oper(const std::vector& a, const std::vector& b) \ - { \ - if (a.size()!=b.size()){throw boost::python::argument_error();} \ - std::vector result(a.size()); \ - for (std::size_t i=0; i) - VECTOR_BINARY_OPERATORS(>=) -# undef VECTOR_BINARY_OPERATORS + struct dvect : public std::vector + { + dvect() : std::vector() {} + dvect(size_t n) : std::vector(n) {} + dvect(boost::python::tuple tuple) : std::vector(tuple.size()) + { + std::vector::iterator v_it = begin(); + for (std::size_t i = 0; i < tuple.size(); i++) + v_it[i] = BOOST_PYTHON_CONVERSION::from_python(tuple[i].get(), + boost::python::type()); + } + + boost::python::tuple as_tuple() const + { + boost::python::tuple t(size()); + for (std::size_t i = 0; i < size(); i++) + t.set_item(i, + boost::python::ref(BOOST_PYTHON_CONVERSION::to_python((*this)[i]))); + return t; + } + +# define DVECT_BINARY_OPERATORS(oper) \ + friend std::vector \ + operator##oper(const dvect& lhs, const dvect& rhs) \ + { \ + if (lhs.size()!=rhs.size()){throw boost::python::argument_error();} \ + std::vector result(lhs.size()); \ + for (std::size_t i=0; i) + DVECT_BINARY_OPERATORS(>=) +# undef VECTOR_BINARY_OPERATORS + }; + +} // namespace vects -} namespace { @@ -37,16 +59,16 @@ namespace { (void) example::wrap_vector(this_module, "vector_of_bool", bool()); - boost::python::class_builder< - std::vector, example::vector_wrapper > - py_vector_of_double = - example::wrap_vector(this_module, "vector_of_double", double()); + boost::python::class_builder 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); - py_vector_of_double.def(boost::python::operators()); + py_dvect.def(boost::python::operators()); } } // namespace @@ -55,7 +77,7 @@ BOOST_PYTHON_MODULE_INIT(richcmp1) { try { boost::python::module_builder this_module("richcmp1"); - // The actual work is done in separate function in order + // The actual work is done in a separate function in order // to suppress a bogus VC60 warning. init_module(this_module); } diff --git a/example/richcmp2.cpp b/example/richcmp2.cpp index ff91a595..a4317bf6 100644 --- a/example/richcmp2.cpp +++ b/example/richcmp2.cpp @@ -53,7 +53,7 @@ BOOST_PYTHON_MODULE_INIT(richcmp2) { try { boost::python::module_builder this_module("richcmp2"); - // The actual work is done in separate function in order + // The actual work is done in a separate function in order // to suppress a bogus VC60 warning. init_module(this_module); } diff --git a/example/richcmp3.cpp b/example/richcmp3.cpp index c0b454c5..bb4dbf11 100644 --- a/example/richcmp3.cpp +++ b/example/richcmp3.cpp @@ -243,7 +243,7 @@ BOOST_PYTHON_MODULE_INIT(richcmp3) { try { boost::python::module_builder this_module("richcmp3"); - // The actual work is done in separate function in order + // The actual work is done in a separate function in order // to suppress a bogus VC60 warning. init_module(this_module); } diff --git a/example/tst_richcmp1.py b/example/tst_richcmp1.py index 4d1635c0..b1227196 100644 --- a/example/tst_richcmp1.py +++ b/example/tst_richcmp1.py @@ -1,6 +1,6 @@ import richcmp1 -d1 = richcmp1.vector_of_double((0, 1, 3, 3, 6, 7)) -d2 = richcmp1.vector_of_double((1, 2, 3, 4, 5, 6)) +d1 = richcmp1.dvect((0, 1, 3, 3, 6, 7)) +d2 = richcmp1.dvect((1, 2, 3, 4, 5, 6)) print d1.as_tuple() print d2.as_tuple() print (d1 < d2).as_tuple() diff --git a/example/vector_wrapper.h b/example/vector_wrapper.h index 902e5e33..d5bd7215 100644 --- a/example/vector_wrapper.h +++ b/example/vector_wrapper.h @@ -1,11 +1,11 @@ // Based on wrapVector.hh by Mike Owen and Jeff Johnson. // http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/spheral/src/src/BPLWraps/CXXWraps/ - + #ifndef BOOST_PYTHON_EXAMPLE_VECTOR_WRAPPER_H #define BOOST_PYTHON_EXAMPLE_VECTOR_WRAPPER_H - + #include - + namespace example { // A wrapper is used to define additional constructors. This wrapper @@ -18,10 +18,10 @@ namespace example { vector_wrapper(PyObject*, const std::vector& vec): std::vector(vec) {} - + vector_wrapper(PyObject* self): std::vector() {} - + vector_wrapper(PyObject* self, std::size_t n): std::vector(n) {} @@ -46,18 +46,18 @@ namespace example { struct vector_access { static - const T + T getitem(const std::vector& vec, - const std::size_t key) + std::size_t key) { if (key >= vec.size()) raise_vector_IndexError(); return vec[key]; } static - void + void setitem(std::vector& vec, - const std::size_t key, + std::size_t key, const T &value) { if (key >= vec.size()) raise_vector_IndexError(); @@ -65,9 +65,9 @@ namespace example { } static - void + void delitem(std::vector& vec, - const std::size_t key) + std::size_t key) { if (key >= vec.size()) raise_vector_IndexError(); vec.erase(vec.begin() + key); @@ -75,7 +75,7 @@ namespace example { // Convert vector to a regular Python tuple. static - boost::python::tuple + boost::python::tuple as_tuple(const std::vector& vec) { // Create a python type of size vec.size(). @@ -87,7 +87,7 @@ namespace example { return t; } }; - + // This function will build a vector and add it to the given // module with the given name. template @@ -113,5 +113,5 @@ namespace example { return py_vector; } } - + #endif // BOOST_PYTHON_EXAMPLE_VECTOR_WRAPPER_H