| 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 | #include <boost/python/list.hpp> |
|---|
| 6 | |
|---|
| 7 | namespace boost { namespace python { namespace detail { |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | detail::new_non_null_reference list_base::call(object const& arg_) |
|---|
| 11 | { |
|---|
| 12 | return (detail::new_non_null_reference) |
|---|
| 13 | (expect_non_null)( |
|---|
| 14 | PyObject_CallFunction( |
|---|
| 15 | (PyObject*)&PyList_Type, "(O)", |
|---|
| 16 | arg_.ptr())); |
|---|
| 17 | } |
|---|
| 18 | |
|---|
| 19 | list_base::list_base() |
|---|
| 20 | : object(detail::new_reference(PyList_New(0))) |
|---|
| 21 | {} |
|---|
| 22 | |
|---|
| 23 | list_base::list_base(object_cref sequence) |
|---|
| 24 | : object(list_base::call(sequence)) |
|---|
| 25 | {} |
|---|
| 26 | |
|---|
| 27 | void list_base::append(object_cref x) |
|---|
| 28 | { |
|---|
| 29 | if (PyList_CheckExact(this->ptr())) |
|---|
| 30 | { |
|---|
| 31 | if (PyList_Append(this->ptr(), x.ptr()) == -1) |
|---|
| 32 | throw_error_already_set(); |
|---|
| 33 | } |
|---|
| 34 | else |
|---|
| 35 | { |
|---|
| 36 | this->attr("append")(x); |
|---|
| 37 | } |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | //long list_base::count(object_cref value) const; |
|---|
| 41 | |
|---|
| 42 | void list_base::extend(object_cref sequence) |
|---|
| 43 | { |
|---|
| 44 | this->attr("extend")(sequence); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | long list_base::index(object_cref value) const |
|---|
| 48 | { |
|---|
| 49 | object result_obj(this->attr("index")(value)); |
|---|
| 50 | long result = PyInt_AsLong(result_obj.ptr()); |
|---|
| 51 | if (result == -1) |
|---|
| 52 | throw_error_already_set(); |
|---|
| 53 | return result; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | void list_base::insert(int index, object_cref item) |
|---|
| 57 | { |
|---|
| 58 | if (PyList_CheckExact(this->ptr())) |
|---|
| 59 | { |
|---|
| 60 | if (PyList_Insert(this->ptr(), index, item.ptr()) == -1) |
|---|
| 61 | throw_error_already_set(); |
|---|
| 62 | } |
|---|
| 63 | else |
|---|
| 64 | { |
|---|
| 65 | this->attr("insert")(index, item); |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | void list_base::insert(object const& index, object_cref x) |
|---|
| 70 | { |
|---|
| 71 | long index_ = PyInt_AsLong(index.ptr()); |
|---|
| 72 | if (index_ == -1 && PyErr_Occurred()) |
|---|
| 73 | throw_error_already_set(); |
|---|
| 74 | this->insert(index_, x); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | object list_base::pop() |
|---|
| 78 | { |
|---|
| 79 | return this->attr("pop")(); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | object list_base::pop(long index) |
|---|
| 83 | { |
|---|
| 84 | return this->pop(object(index)); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | object list_base::pop(object const& index) |
|---|
| 88 | { |
|---|
| 89 | return this->attr("pop")(index); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | void list_base::remove(object_cref value) |
|---|
| 93 | { |
|---|
| 94 | this->attr("remove")(value); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | void list_base::reverse() |
|---|
| 98 | { |
|---|
| 99 | if (PyList_CheckExact(this->ptr())) |
|---|
| 100 | { |
|---|
| 101 | if (PyList_Reverse(this->ptr()) == -1) |
|---|
| 102 | throw_error_already_set(); |
|---|
| 103 | } |
|---|
| 104 | else |
|---|
| 105 | { |
|---|
| 106 | this->attr("reverse")(); |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | void list_base::sort() |
|---|
| 111 | { |
|---|
| 112 | if (PyList_CheckExact(this->ptr())) |
|---|
| 113 | { |
|---|
| 114 | if (PyList_Sort(this->ptr()) == -1) |
|---|
| 115 | throw_error_already_set(); |
|---|
| 116 | } |
|---|
| 117 | else |
|---|
| 118 | { |
|---|
| 119 | this->attr("sort")(); |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | void list_base::sort(object_cref cmpfunc) |
|---|
| 124 | { |
|---|
| 125 | this->attr("sort")(cmpfunc); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | // For some reason, moving this to the end of the TU suppresses an ICE |
|---|
| 129 | // with vc6. |
|---|
| 130 | long list_base::count(object_cref value) const |
|---|
| 131 | { |
|---|
| 132 | object result_obj(this->attr("count")(value)); |
|---|
| 133 | long result = PyInt_AsLong(result_obj.ptr()); |
|---|
| 134 | if (result == -1) |
|---|
| 135 | throw_error_already_set(); |
|---|
| 136 | return result; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | }}} // namespace boost::python |
|---|