2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-25 06:22:15 +00:00

test_richcmp1.py now works with vc60.

[SVN r10657]
This commit is contained in:
Ralf W. Grosse-Kunstleve
2001-07-18 13:16:51 +00:00
parent fc9a6ac369
commit 5031479fa2
5 changed files with 67 additions and 45 deletions

View File

@@ -6,29 +6,51 @@
#include <boost/python/class_builder.hpp>
#include "vector_wrapper.h"
namespace std {
namespace vects {
# define VECTOR_BINARY_OPERATORS(oper) \
template <class T> \
std::vector<bool> \
operator##oper(const std::vector<T>& a, const std::vector<T>& b) \
{ \
if (a.size()!=b.size()){throw boost::python::argument_error();} \
std::vector<bool> result(a.size()); \
for (std::size_t i=0; i<a.size(); i++) { \
result[i] = (a[i] ##oper b[i]); \
} \
return result; \
}
VECTOR_BINARY_OPERATORS(<)
VECTOR_BINARY_OPERATORS(<=)
VECTOR_BINARY_OPERATORS(==)
VECTOR_BINARY_OPERATORS(!=)
VECTOR_BINARY_OPERATORS(>)
VECTOR_BINARY_OPERATORS(>=)
# undef VECTOR_BINARY_OPERATORS
struct dvect : public std::vector<double>
{
dvect() : std::vector<double>() {}
dvect(size_t n) : std::vector<double>(n) {}
dvect(boost::python::tuple tuple) : std::vector<double>(tuple.size())
{
std::vector<double>::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<double>());
}
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<bool> \
operator##oper(const dvect& lhs, const dvect& rhs) \
{ \
if (lhs.size()!=rhs.size()){throw boost::python::argument_error();} \
std::vector<bool> result(lhs.size()); \
for (std::size_t i=0; i<lhs.size(); i++) { \
result[i] = (lhs[i] ##oper rhs[i]); \
} \
return result; \
}
DVECT_BINARY_OPERATORS(<)
DVECT_BINARY_OPERATORS(<=)
DVECT_BINARY_OPERATORS(==)
DVECT_BINARY_OPERATORS(!=)
DVECT_BINARY_OPERATORS(>)
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<double>, example::vector_wrapper<double> >
py_vector_of_double =
example::wrap_vector(this_module, "vector_of_double", double());
boost::python::class_builder<vects::dvect> py_dvect(this_module, "dvect");
py_dvect.def(boost::python::constructor<boost::python::tuple>());
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<comp_operators>());
py_dvect.def(boost::python::operators<comp_operators>());
}
} // namespace <anonymous>
@@ -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);
}