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