2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-27 07:02:15 +00:00

boost.python.numpy - added dtype template invoker

This commit is contained in:
Jim Bosch
2010-06-24 22:20:55 +00:00
parent 40bd0326f3
commit f3aecdf2f4
4 changed files with 111 additions and 4 deletions

View File

@@ -9,6 +9,9 @@
#include <boost/python.hpp>
#include <boost/python/numpy/numpy_object_mgr_traits.hpp>
#include <boost/mpl/for_each.hpp>
#include <boost/type_traits/add_pointer.hpp>
namespace boost { namespace python {
namespace numpy {
@@ -43,8 +46,57 @@ public:
BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(dtype, object);
template <typename Sequence, typename Function>
void invoke_matching_template(Function f);
};
namespace detail {
struct add_pointer_meta {
template <typename T>
struct apply {
typedef typename boost::add_pointer<T>::type type;
};
};
struct dtype_template_match_found {};
template <typename Function>
struct dtype_template_invoker {
template <typename T>
void operator()(T * x) const {
if (dtype::get_builtin<T>() == m_dtype) {
m_func.template apply<T>();
throw dtype_template_match_found();
}
}
dtype_template_invoker(dtype const & dtype_, Function func) :
m_dtype(dtype_), m_func(func) {}
private:
dtype const & m_dtype;
Function m_func;
};
} // namespace boost::python::numpy::detail
template <typename Sequence, typename Function>
void dtype::invoke_matching_template(Function f) {
detail::dtype_template_invoker<Function> invoker(*this, f);
try {
boost::mpl::for_each< Sequence, detail::add_pointer_meta >(invoker);
} catch (detail::dtype_template_match_found &) {
return;
}
PyErr_SetString(PyExc_TypeError, "numpy.dtype not found in template list.");
throw_error_already_set();
}
} // namespace boost::python::numpy
namespace converter {