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 ssize_t 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 #if PY_VERSION_HEX >= 0x03000000 41 void sort(args_proxy const &args, 42 kwds_proxy const &kwds); 43 #else 44 void sort(object_cref cmpfunc); 45 #endif 46 47 protected: 48 list_base(); // new list 49 explicit list_base(object_cref sequence); // new list initialized from sequence's items 50 51 BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(list_base, object) 52 private: 53 static detail::new_non_null_reference call(object const&); 54 }; 55 } 56 57 class list : public detail::list_base 58 { 59 typedef detail::list_base base; 60 public: list()61 list() {} // new list 62 63 template <class T> list(T const & sequence)64 explicit list(T const& sequence) 65 : base(object(sequence)) 66 { 67 } 68 69 template <class T> append(T const & x)70 void append(T const& x) 71 { 72 base::append(object(x)); 73 } 74 75 template <class T> count(T const & value) const76 ssize_t count(T const& value) const 77 { 78 return base::count(object(value)); 79 } 80 81 template <class T> extend(T const & x)82 void extend(T const& x) 83 { 84 base::extend(object(x)); 85 } 86 87 template <class T> index(T const & x) const88 long index(T const& x) const 89 { 90 return base::index(object(x)); 91 } 92 93 template <class T> insert(ssize_t index,T const & x)94 void insert(ssize_t index, T const& x) // insert object before index 95 { 96 base::insert(index, object(x)); 97 } 98 99 template <class T> insert(object const & index,T const & x)100 void insert(object const& index, T const& x) // insert object before index 101 { 102 base::insert(index, object(x)); 103 } 104 pop()105 object pop() { return base::pop(); } pop(ssize_t index)106 object pop(ssize_t index) { return base::pop(index); } 107 108 template <class T> pop(T const & index)109 object pop(T const& index) 110 { 111 return base::pop(object(index)); 112 } 113 114 template <class T> remove(T const & value)115 void remove(T const& value) 116 { 117 base::remove(object(value)); 118 } 119 120 #if PY_VERSION_HEX <= 0x03000000 sort()121 void sort() { base::sort(); } 122 123 template <class T> sort(T const & value)124 void sort(T const& value) 125 { 126 base::sort(object(value)); 127 } 128 #endif 129 130 public: // implementation detail -- for internal use only 131 BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(list, base) 132 }; 133 134 // 135 // Converter Specializations 136 // 137 namespace converter 138 { 139 template <> 140 struct object_manager_traits<list> 141 : pytype_object_manager_traits<&PyList_Type,list> 142 { 143 }; 144 } 145 146 }} // namespace boost::python 147 148 #endif // LIST_DWA2002627_HPP 149