mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-26 07:02:11 +00:00
Refactoring vector_binder so it now a function
This commit is contained in:
parent
2de5af998d
commit
9ee4f92b06
@ -20,7 +20,7 @@
|
|||||||
|
|
||||||
|
|
||||||
NAMESPACE_BEGIN(pybind11)
|
NAMESPACE_BEGIN(pybind11)
|
||||||
|
NAMESPACE_BEGIN(detail)
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
constexpr auto has_equal_operator(int) -> decltype(std::declval<T>() == std::declval<T>(), bool()) { return true; }
|
constexpr auto has_equal_operator(int) -> decltype(std::declval<T>() == std::declval<T>(), bool()) { return true; }
|
||||||
@ -67,25 +67,12 @@ struct has_insertion_operator_s {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template<typename Vector, typename Class_, typename std::enable_if< std::is_default_constructible<typename Vector::value_type>::value >::type * = nullptr>
|
||||||
|
void vector_maybe_default_constructible(Class_ &cl) {
|
||||||
|
using size_type = typename Vector::size_type;
|
||||||
|
|
||||||
template <typename T, typename Allocator = std::allocator<T>, typename holder_type = std::unique_ptr< std::vector<T, Allocator> > >
|
cl.def(pybind11::init<size_type>());
|
||||||
class vector_binder {
|
cl.def("resize", (void (Vector::*)(size_type count)) &Vector::resize, "changes the number of elements stored");
|
||||||
using Vector = std::vector<T, Allocator>;
|
|
||||||
using SizeType = typename Vector::size_type;
|
|
||||||
|
|
||||||
using Class_ = pybind11::class_<Vector, holder_type >;
|
|
||||||
|
|
||||||
// template<typename U = T, typename std::enable_if< std::is_constructible<U>{} >::type * = nullptr>
|
|
||||||
// void maybe_constructible(Class_ &cl) {
|
|
||||||
// cl.def(pybind11::init<>());
|
|
||||||
// }
|
|
||||||
// template<typename U = T, typename std::enable_if< !std::is_constructible<U>{} >::type * = nullptr>
|
|
||||||
// void maybe_constructible(Class_ &cl) {}
|
|
||||||
|
|
||||||
template<typename U = T, typename std::enable_if< std::is_default_constructible<U>::value >::type * = nullptr>
|
|
||||||
void maybe_default_constructible() {
|
|
||||||
cl.def(pybind11::init<SizeType>());
|
|
||||||
cl.def("resize", (void (Vector::*)(SizeType count)) &Vector::resize, "changes the number of elements stored");
|
|
||||||
|
|
||||||
/// Slicing protocol
|
/// Slicing protocol
|
||||||
cl.def("__getitem__", [](Vector const &v, pybind11::slice slice) -> Vector * {
|
cl.def("__getitem__", [](Vector const &v, pybind11::slice slice) -> Vector * {
|
||||||
@ -99,20 +86,22 @@ class vector_binder {
|
|||||||
return seq;
|
return seq;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
template<typename U = T, typename std::enable_if< !std::is_default_constructible<U>::value >::type * = nullptr>
|
template<typename Vector, typename Class_, typename std::enable_if< !std::is_default_constructible<typename Vector::value_type>::value >::type * = nullptr>
|
||||||
void maybe_default_constructible() {}
|
void vector_maybe_default_constructible(Class_ &) {}
|
||||||
|
|
||||||
|
|
||||||
template<typename U = T, typename std::enable_if< std::is_copy_constructible<U>::value >::type * = nullptr>
|
template<typename Vector, typename Class_, typename std::enable_if< std::is_copy_constructible<typename Vector::value_type>::value >::type * = nullptr>
|
||||||
void maybe_copy_constructible() {
|
void vector_maybe_copy_constructible(Class_ &cl) {
|
||||||
cl.def(pybind11::init< Vector const &>());
|
cl.def(pybind11::init< Vector const &>());
|
||||||
}
|
}
|
||||||
template<typename U = T, typename std::enable_if< !std::is_copy_constructible<U>::value >::type * = nullptr>
|
template<typename Vector, typename Class_, typename std::enable_if< !std::is_copy_constructible<typename Vector::value_type>::value >::type * = nullptr>
|
||||||
void maybe_copy_constructible() {}
|
void vector_maybe_copy_constructible(Class_ &) {}
|
||||||
|
|
||||||
|
|
||||||
template<typename U = T, typename std::enable_if< has_equal_operator_s<U>::value >::type * = nullptr>
|
template<typename Vector, typename Class_, typename std::enable_if< has_equal_operator_s<typename Vector::value_type>::value >::type * = nullptr>
|
||||||
void maybe_has_equal_operator() {
|
void vector_maybe_has_equal_operator(Class_ &cl) {
|
||||||
|
using T = typename Vector::value_type;
|
||||||
|
|
||||||
cl.def(pybind11::self == pybind11::self);
|
cl.def(pybind11::self == pybind11::self);
|
||||||
cl.def(pybind11::self != pybind11::self);
|
cl.def(pybind11::self != pybind11::self);
|
||||||
|
|
||||||
@ -126,45 +115,52 @@ class vector_binder {
|
|||||||
|
|
||||||
cl.def("__contains__", [](Vector const &v, T const &t) { return std::find(v.begin(), v.end(), t) != v.end(); }, "return true if item in the container");
|
cl.def("__contains__", [](Vector const &v, T const &t) { return std::find(v.begin(), v.end(), t) != v.end(); }, "return true if item in the container");
|
||||||
}
|
}
|
||||||
template<typename U = T, typename std::enable_if< !has_equal_operator_s<U>::value >::type * = nullptr>
|
template<typename Vector, typename Class_, typename std::enable_if< !has_equal_operator_s<typename Vector::value_type>::value >::type * = nullptr>
|
||||||
void maybe_has_equal_operator() {}
|
void vector_maybe_has_equal_operator(Class_ &) {}
|
||||||
|
|
||||||
|
|
||||||
template<typename U = T, typename std::enable_if< has_not_equal_operator_s<U>::value >::type * = nullptr>
|
template<typename Vector, typename Class_, typename std::enable_if< has_not_equal_operator_s<typename Vector::value_type>::value >::type * = nullptr>
|
||||||
void maybe_has_not_equal_operator() {
|
void vector_maybe_has_not_equal_operator(Class_ &cl) {
|
||||||
cl.def(pybind11::self != pybind11::self);
|
cl.def(pybind11::self != pybind11::self);
|
||||||
}
|
}
|
||||||
template<typename U = T, typename std::enable_if< !has_not_equal_operator_s<U>::value >::type * = nullptr>
|
template<typename Vector, typename Class_, typename std::enable_if< !has_not_equal_operator_s<typename Vector::value_type>::value >::type * = nullptr>
|
||||||
void maybe_has_not_equal_operator() {}
|
void vector_maybe_has_not_equal_operator(Class_ &) {}
|
||||||
|
|
||||||
|
|
||||||
template<typename U = T, typename std::enable_if< has_insertion_operator_s<U>::value >::type * = nullptr>
|
template<typename Vector, typename Class_, typename std::enable_if< has_insertion_operator_s<typename Vector::value_type>::value >::type * = nullptr>
|
||||||
void maybe_has_insertion_operator(char const *name) {
|
void vector_maybe_has_insertion_operator(char const *name, Class_ &cl) {
|
||||||
cl.def("__repr__", [name](typename vector_binder<T>::Vector &v) {
|
using size_type = typename Vector::size_type;
|
||||||
|
|
||||||
|
cl.def("__repr__", [name](Vector &v) {
|
||||||
std::ostringstream s;
|
std::ostringstream s;
|
||||||
s << name << '[';
|
s << name << '[';
|
||||||
for(SizeType i=0; i<v.size(); ++i) {
|
for(size_type i=0; i<v.size(); ++i) {
|
||||||
s << v[i];
|
s << v[i];
|
||||||
if(i != v.size()-1) s << ", ";
|
if(i != v.size()-1) s << ", ";
|
||||||
}
|
}
|
||||||
s << ']';
|
s << ']';
|
||||||
return s.str();
|
return s.str();
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
template<typename U = T, typename std::enable_if< !has_insertion_operator_s<U>::value >::type * = nullptr>
|
template<typename Vector, typename Class_, typename std::enable_if< !has_insertion_operator_s<typename Vector::value_type>::value >::type * = nullptr>
|
||||||
void maybe_has_insertion_operator(char const *) {}
|
void vector_maybe_has_insertion_operator(char const *, Class_ &) {}
|
||||||
|
|
||||||
|
|
||||||
public:
|
NAMESPACE_END(detail)
|
||||||
Class_ cl;
|
|
||||||
|
|
||||||
|
template <typename T, typename Allocator = std::allocator<T>, typename holder_type = std::unique_ptr< std::vector<T, Allocator> > >
|
||||||
|
pybind11::class_<std::vector<T, Allocator>, holder_type > vector_binder(pybind11::module &m, char const *name, char const *doc=nullptr) {
|
||||||
|
using Vector = std::vector<T, Allocator>;
|
||||||
|
using SizeType = typename Vector::size_type;
|
||||||
|
using Class_ = pybind11::class_<Vector, holder_type >;
|
||||||
|
|
||||||
|
Class_ cl(m, name, doc);
|
||||||
|
|
||||||
vector_binder(pybind11::module &m, char const *name, char const *doc=nullptr) : cl(m, name, doc) {
|
|
||||||
cl.def(pybind11::init<>());
|
cl.def(pybind11::init<>());
|
||||||
|
|
||||||
//maybe_constructible(cl);
|
detail::vector_maybe_default_constructible<Vector>(cl);
|
||||||
maybe_default_constructible();
|
detail::vector_maybe_copy_constructible<Vector>(cl);
|
||||||
maybe_copy_constructible();
|
|
||||||
|
|
||||||
// Element access
|
// Element access
|
||||||
cl.def("front", [](Vector &v) {
|
cl.def("front", [](Vector &v) {
|
||||||
@ -177,7 +173,6 @@ public:
|
|||||||
}, "access the last element ");
|
}, "access the last element ");
|
||||||
// Not needed, the operator[] is already providing bounds checking cl.def("at", (T& (Vector::*)(SizeType i)) &Vector::at, "access specified element with bounds checking");
|
// Not needed, the operator[] is already providing bounds checking cl.def("at", (T& (Vector::*)(SizeType i)) &Vector::at, "access specified element with bounds checking");
|
||||||
|
|
||||||
|
|
||||||
// Capacity, C++ style
|
// Capacity, C++ style
|
||||||
cl.def("max_size", &Vector::max_size, "returns the maximum possible number of elements");
|
cl.def("max_size", &Vector::max_size, "returns the maximum possible number of elements");
|
||||||
cl.def("reserve", &Vector::reserve, "reserves storage");
|
cl.def("reserve", &Vector::reserve, "reserves storage");
|
||||||
@ -248,19 +243,20 @@ public:
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Comparisons
|
// Comparisons
|
||||||
maybe_has_equal_operator();
|
detail::vector_maybe_has_equal_operator<Vector>(cl);
|
||||||
maybe_has_not_equal_operator();
|
detail::vector_maybe_has_not_equal_operator<Vector>(cl);
|
||||||
|
|
||||||
// Printing
|
// Printing
|
||||||
maybe_has_insertion_operator(name);
|
detail::vector_maybe_has_insertion_operator<Vector>(name, cl);
|
||||||
|
|
||||||
// C++ style functions deprecated, leaving it here as an example
|
// C++ style functions deprecated, leaving it here as an example
|
||||||
//cl.def("empty", &Vector::empty, "checks whether the container is empty");
|
//cl.def("empty", &Vector::empty, "checks whether the container is empty");
|
||||||
//cl.def("size", &Vector::size, "returns the number of elements");
|
//cl.def("size", &Vector::size, "returns the number of elements");
|
||||||
//cl.def("push_back", (void (Vector::*)(const T&)) &Vector::push_back, "adds an element to the end");
|
//cl.def("push_back", (void (Vector::*)(const T&)) &Vector::push_back, "adds an element to the end");
|
||||||
//cl.def("pop_back", &Vector::pop_back, "removes the last element");
|
//cl.def("pop_back", &Vector::pop_back, "removes the last element");
|
||||||
|
|
||||||
|
return cl;
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
NAMESPACE_END(pybind11)
|
NAMESPACE_END(pybind11)
|
||||||
|
Loading…
Reference in New Issue
Block a user