Adding Python-style modifiers for vector. Wrapping has_insertion_operator_implementation in _MSC_VER ifdef’s. Adding ‘!=0’ instead of bool cast’s.

This commit is contained in:
Sergey Lyskov 2016-05-07 18:50:26 -04:00
parent 587fc20b68
commit f13194c80a
4 changed files with 64 additions and 18 deletions

View File

@ -16,6 +16,9 @@ print( v_int != v_int2 )
v_int2.push_back(2) v_int2.push_back(2)
v_int2.push_back(3) v_int2.push_back(3)
v_int2.insert(0, 1)
v_int2.insert(0, 2)
v_int2.insert(0, 3)
print(v_int2) print(v_int2)
v_a = VectorA() v_a = VectorA()

View File

@ -2,4 +2,4 @@
True True
True True
True True
VectorInt[0, 1, 2, 3] VectorInt[3, 2, 1, 0, 1, 2, 3]

View File

@ -304,6 +304,7 @@ NAMESPACE_END(detail)
class error_already_set : public std::runtime_error { public: error_already_set() : std::runtime_error(detail::error_string()) {} }; class error_already_set : public std::runtime_error { public: error_already_set() : std::runtime_error(detail::error_string()) {} };
PYBIND11_RUNTIME_EXCEPTION(stop_iteration) PYBIND11_RUNTIME_EXCEPTION(stop_iteration)
PYBIND11_RUNTIME_EXCEPTION(index_error) PYBIND11_RUNTIME_EXCEPTION(index_error)
PYBIND11_RUNTIME_EXCEPTION(value_error)
PYBIND11_RUNTIME_EXCEPTION(cast_error) /// Thrown when pybind11::cast or handle::call fail due to a type casting error PYBIND11_RUNTIME_EXCEPTION(cast_error) /// Thrown when pybind11::cast or handle::call fail due to a type casting error
[[noreturn]] PYBIND11_NOINLINE inline void pybind11_fail(const char *reason) { throw std::runtime_error(reason); } [[noreturn]] PYBIND11_NOINLINE inline void pybind11_fail(const char *reason) { throw std::runtime_error(reason); }

View File

@ -21,6 +21,11 @@
NAMESPACE_BEGIN(pybind11) NAMESPACE_BEGIN(pybind11)
#if (!defined _MSC_VER) || (_MSC_VER > 1900)
#define INSERTION_OPERATOR_IMPLEMENTATION
#endif
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; }
template<typename T> template<typename T>
@ -34,7 +39,7 @@ template<typename T>
constexpr bool has_not_equal_operator(...) { return false; } constexpr bool has_not_equal_operator(...) { return false; }
#ifdef INSERTION_OPERATOR_IMPLEMENTATION
namespace has_insertion_operator_implementation { namespace has_insertion_operator_implementation {
enum class False {}; enum class False {};
struct any_type { struct any_type {
@ -48,6 +53,7 @@ constexpr bool has_insertion_operator()
using namespace has_insertion_operator_implementation; using namespace has_insertion_operator_implementation;
return std::is_same< decltype( std::declval<std::ostringstream&>() << std::declval<T>() ), std::ostream & >::value; return std::is_same< decltype( std::declval<std::ostringstream&>() << std::declval<T>() ), std::ostream & >::value;
} }
#endif
template <typename T, typename Allocator = std::allocator<T>, typename holder_type = std::unique_ptr< std::vector<T, Allocator> > > template <typename T, typename Allocator = std::allocator<T>, typename holder_type = std::unique_ptr< std::vector<T, Allocator> > >
@ -88,6 +94,12 @@ class vector_binder
cl.def(pybind11::self != pybind11::self); cl.def(pybind11::self != pybind11::self);
cl.def("count", [](Vector const &v, T const & value) { return std::count(v.begin(), v.end(), value); }, "counts the elements that are equal to value"); cl.def("count", [](Vector const &v, T const & value) { return std::count(v.begin(), v.end(), value); }, "counts the elements that are equal to value");
cl.def("remove", [](Vector &v, const T&t) {
auto p = std::find(v.begin(), v.end(), t);
if( p != v.end() ) v.erase(p);
else throw pybind11::value_error();
}, "Remove the first item from the list whose value is x. It is an error if there is no such item.");
} }
template<typename U = T, typename std::enable_if< !has_equal_operator<U>(0) >::type * = nullptr> template<typename U = T, typename std::enable_if< !has_equal_operator<U>(0) >::type * = nullptr>
void maybe_has_equal_operator(Class_ &) {} void maybe_has_equal_operator(Class_ &) {}
@ -101,6 +113,7 @@ class vector_binder
void maybe_has_not_equal_operator(Class_ &) {} void maybe_has_not_equal_operator(Class_ &) {}
#ifdef INSERTION_OPERATOR_IMPLEMENTATION
template<typename U = T, typename std::enable_if< has_insertion_operator<U>() >::type * = nullptr> template<typename U = T, typename std::enable_if< has_insertion_operator<U>() >::type * = nullptr>
void maybe_has_insertion_operator(Class_ &cl, std::string name) { void maybe_has_insertion_operator(Class_ &cl, std::string name) {
cl.def("__repr__", [name](typename vector_binder<T>::Vector &v) { cl.def("__repr__", [name](typename vector_binder<T>::Vector &v) {
@ -117,7 +130,7 @@ class vector_binder
} }
template<typename U = T, typename std::enable_if< !has_insertion_operator<U>() >::type * = nullptr> template<typename U = T, typename std::enable_if< !has_insertion_operator<U>() >::type * = nullptr>
void maybe_has_insertion_operator(Class_ &, char const *) {} void maybe_has_insertion_operator(Class_ &, char const *) {}
#endif
@ -131,6 +144,18 @@ public:
maybe_default_constructible(cl); maybe_default_constructible(cl);
maybe_copy_constructible(cl); maybe_copy_constructible(cl);
// Element access
cl.def("front", [](Vector &v) {
if( v.size() ) return v.front();
else throw pybind11::index_error();
}, "access the first element");
cl.def("back", [](Vector &v) {
if( v.size() ) return v.back();
else throw pybind11::index_error();
}, "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");
// Capacity // Capacity
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");
@ -139,31 +164,46 @@ public:
cl.def("capacity", &Vector::capacity, "returns the number of elements that can be held in currently allocated storage"); cl.def("capacity", &Vector::capacity, "returns the number of elements that can be held in currently allocated storage");
cl.def("shrink_to_fit", &Vector::shrink_to_fit, "reduces memory usage by freeing unused memory"); cl.def("shrink_to_fit", &Vector::shrink_to_fit, "reduces memory usage by freeing unused memory");
// Modifiers // Modifiers, C++ style
cl.def("clear", &Vector::clear, "clears the contents"); cl.def("clear", &Vector::clear, "clears the contents");
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");
cl.def("swap", &Vector::swap, "swaps the contents"); cl.def("swap", &Vector::swap, "swaps the contents");
// Modifiers, Python style
cl.def("append", (void (Vector::*)(const T&)) &Vector::push_back, "adds an element to the end");
cl.def("insert", [](Vector &v, SizeType i, const T&t) {v.insert(v.begin()+i, t);}, "insert an item at a given position");
cl.def("pop", [](Vector &v) {
if( v.size() ) {
T t = v.back();
v.pop_back();
return t;
}
else throw pybind11::index_error();
}, "insert an item at a given position");
cl.def("erase", [](Vector &v, SizeType i) { cl.def("erase", [](Vector &v, SizeType i) {
if (i >= v.size()) throw pybind11::index_error(); if( i >= v.size() ) throw pybind11::index_error();
v.erase( v.begin() + i ); v.erase( v.begin() + i );
}, "erases element at index"); }, "erases element at index");
// Python friendly bindings // Python friendly bindings
#ifdef PYTHON_ABI_VERSION // Python 3+ #ifdef PYTHON_ABI_VERSION // Python 3+
cl.def("__bool__", [](Vector &v) -> bool { return v.size(); }); // checks whether the container has any elements in it cl.def("__bool__", [](Vector &v) -> bool { return v.size() != 0; }); // checks whether the container has any elements in it
#else #else
cl.def("__nonzero__", [](Vector &v) -> bool { return v.size(); }); // checks whether the container has any elements in it cl.def("__nonzero__", [](Vector &v) -> bool { return v.size() != 0; }); // checks whether the container has any elements in it
#endif #endif
cl.def("__getitem__", [](Vector const &v, SizeType i) { cl.def("__getitem__", [](Vector const &v, SizeType i) {
if (i >= v.size()) throw pybind11::index_error(); if( i >= v.size() ) throw pybind11::index_error();
return v[i]; return v[i];
}); });
cl.def("__setitem__", [](Vector &v, SizeType i, T const & t) { cl.def("__setitem__", [](Vector &v, SizeType i, T const & t) {
if (i >= v.size()) throw pybind11::index_error(); if( i >= v.size() ) throw pybind11::index_error();
v[i] = t; v[i] = t;
}); });
@ -174,7 +214,9 @@ public:
maybe_has_not_equal_operator(cl); maybe_has_not_equal_operator(cl);
// Printing // Printing
#ifdef INSERTION_OPERATOR_IMPLEMENTATION
maybe_has_insertion_operator(cl, name); maybe_has_insertion_operator(cl, name);
#endif
} }
}; };