Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/python/src/list.cpp @ 14

Last change on this file since 14 was 12, checked in by landauf, 18 years ago

added boost

File size: 2.9 KB
Line 
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
7namespace boost { namespace python { namespace detail {
8
9
10detail::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
19list_base::list_base()
20    : object(detail::new_reference(PyList_New(0)))
21{}
22
23list_base::list_base(object_cref sequence)
24    : object(list_base::call(sequence))
25{}
26
27void 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
42void list_base::extend(object_cref sequence)
43{
44    this->attr("extend")(sequence);
45}
46
47long 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
56void 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
69void 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
77object list_base::pop()
78{
79    return this->attr("pop")();
80}
81
82object list_base::pop(long index)
83{
84    return this->pop(object(index));
85}
86
87object list_base::pop(object const& index)
88{
89    return this->attr("pop")(index);
90}
91
92void list_base::remove(object_cref value)
93{
94    this->attr("remove")(value);
95}
96   
97void 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
110void 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
123void 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.
130long 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
Note: See TracBrowser for help on using the repository browser.