// Example by Ralf W. Grosse-Kunstleve & Nicholas K. Sauter // This example shows how to use rich comparisons for a vector type. // It also shows how to template the entire wrapping of a std::vector. // See vector_wrapper.h. #include #include "vector_wrapper.h" namespace vects { 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()) { \ 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) DVECT_BINARY_OPERATORS(>=) # undef VECTOR_BINARY_OPERATORS }; } // namespace namespace { void init_module(boost::python::module_builder& this_module) { (void) example::wrap_vector(this_module, "vector_of_bool", bool()); 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_dvect.def(boost::python::operators()); } } // namespace BOOST_PYTHON_MODULE_INIT(richcmp1) { try { boost::python::module_builder this_module("richcmp1"); // The actual work is done in a separate function in order // to suppress a bogus VC60 warning. init_module(this_module); } catch (...) { boost::python::handle_exception(); } }