| 1 | // Copyright David Abrahams 2002. |
|---|
| 2 | // Distributed under the Boost Software License, Version 1.0. (See |
|---|
| 3 | // accompanying file LICENSE_1_0.txt or copy at |
|---|
| 4 | // http://www.boost.org/LICENSE_1_0.txt) |
|---|
| 5 | #ifndef LIST_DWA2002627_HPP |
|---|
| 6 | # define LIST_DWA2002627_HPP |
|---|
| 7 | |
|---|
| 8 | # include <boost/python/detail/prefix.hpp> |
|---|
| 9 | |
|---|
| 10 | # include <boost/python/object.hpp> |
|---|
| 11 | # include <boost/python/converter/pytype_object_mgr_traits.hpp> |
|---|
| 12 | # include <boost/python/ssize_t.hpp> |
|---|
| 13 | |
|---|
| 14 | namespace boost { namespace python { |
|---|
| 15 | |
|---|
| 16 | namespace detail |
|---|
| 17 | { |
|---|
| 18 | struct BOOST_PYTHON_DECL list_base : object |
|---|
| 19 | { |
|---|
| 20 | void append(object_cref); // append object to end |
|---|
| 21 | |
|---|
| 22 | long count(object_cref value) const; // return number of occurrences of value |
|---|
| 23 | |
|---|
| 24 | void extend(object_cref sequence); // extend list by appending sequence elements |
|---|
| 25 | |
|---|
| 26 | long index(object_cref value) const; // return index of first occurrence of value |
|---|
| 27 | |
|---|
| 28 | void insert(ssize_t index, object_cref); // insert object before index |
|---|
| 29 | void insert(object const& index, object_cref); |
|---|
| 30 | |
|---|
| 31 | object pop(); // remove and return item at index (default last) |
|---|
| 32 | object pop(ssize_t index); |
|---|
| 33 | object pop(object const& index); |
|---|
| 34 | |
|---|
| 35 | void remove(object_cref value); // remove first occurrence of value |
|---|
| 36 | |
|---|
| 37 | void reverse(); // reverse *IN PLACE* |
|---|
| 38 | |
|---|
| 39 | void sort(); // sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1 |
|---|
| 40 | void sort(object_cref cmpfunc); |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | protected: |
|---|
| 44 | list_base(); // new list |
|---|
| 45 | explicit list_base(object_cref sequence); // new list initialized from sequence's items |
|---|
| 46 | |
|---|
| 47 | BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(list_base, object) |
|---|
| 48 | private: |
|---|
| 49 | static detail::new_non_null_reference call(object const&); |
|---|
| 50 | }; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | class list : public detail::list_base |
|---|
| 54 | { |
|---|
| 55 | typedef detail::list_base base; |
|---|
| 56 | public: |
|---|
| 57 | list() {} // new list |
|---|
| 58 | |
|---|
| 59 | template <class T> |
|---|
| 60 | explicit list(T const& sequence) |
|---|
| 61 | : base(object(sequence)) |
|---|
| 62 | { |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | template <class T> |
|---|
| 66 | void append(T const& x) |
|---|
| 67 | { |
|---|
| 68 | base::append(object(x)); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | template <class T> |
|---|
| 72 | long count(T const& value) const |
|---|
| 73 | { |
|---|
| 74 | return base::count(object(value)); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | template <class T> |
|---|
| 78 | void extend(T const& x) |
|---|
| 79 | { |
|---|
| 80 | base::extend(object(x)); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | template <class T> |
|---|
| 84 | long index(T const& x) const |
|---|
| 85 | { |
|---|
| 86 | return base::index(object(x)); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | template <class T> |
|---|
| 90 | void insert(ssize_t index, T const& x) // insert object before index |
|---|
| 91 | { |
|---|
| 92 | base::insert(index, object(x)); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | template <class T> |
|---|
| 96 | void insert(object const& index, T const& x) // insert object before index |
|---|
| 97 | { |
|---|
| 98 | base::insert(index, object(x)); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | object pop() { return base::pop(); } |
|---|
| 102 | object pop(ssize_t index) { return base::pop(index); } |
|---|
| 103 | |
|---|
| 104 | template <class T> |
|---|
| 105 | object pop(T const& index) |
|---|
| 106 | { |
|---|
| 107 | return base::pop(object(index)); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | template <class T> |
|---|
| 111 | void remove(T const& value) |
|---|
| 112 | { |
|---|
| 113 | base::remove(object(value)); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | void sort() { base::sort(); } |
|---|
| 117 | |
|---|
| 118 | template <class T> |
|---|
| 119 | void sort(T const& value) |
|---|
| 120 | { |
|---|
| 121 | base::sort(object(value)); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | public: // implementation detail -- for internal use only |
|---|
| 125 | BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(list, base) |
|---|
| 126 | }; |
|---|
| 127 | |
|---|
| 128 | // |
|---|
| 129 | // Converter Specializations |
|---|
| 130 | // |
|---|
| 131 | namespace converter |
|---|
| 132 | { |
|---|
| 133 | template <> |
|---|
| 134 | struct object_manager_traits<list> |
|---|
| 135 | : pytype_object_manager_traits<&PyList_Type,list> |
|---|
| 136 | { |
|---|
| 137 | }; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | }} // namespace boost::python |
|---|
| 141 | |
|---|
| 142 | #endif // LIST_DWA2002627_HPP |
|---|