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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user