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 | |
---|
6 | #include <boost/python/module.hpp> |
---|
7 | #include <boost/python/def.hpp> |
---|
8 | #include <boost/python/class.hpp> |
---|
9 | #include <boost/python/list.hpp> |
---|
10 | #include <boost/python/make_function.hpp> |
---|
11 | #include <boost/lexical_cast.hpp> |
---|
12 | #include <boost/assert.hpp> |
---|
13 | #include "test_class.hpp" |
---|
14 | |
---|
15 | using namespace boost::python; |
---|
16 | |
---|
17 | object new_list() |
---|
18 | { |
---|
19 | return list(); |
---|
20 | } |
---|
21 | |
---|
22 | list listify(object x) |
---|
23 | { |
---|
24 | return list(x); |
---|
25 | } |
---|
26 | |
---|
27 | object listify_string(char const* s) |
---|
28 | { |
---|
29 | return list(s); |
---|
30 | } |
---|
31 | |
---|
32 | std::string x_rep(test_class<> const& x) |
---|
33 | { |
---|
34 | return "X(" + boost::lexical_cast<std::string>(x.value()) + ")"; |
---|
35 | } |
---|
36 | |
---|
37 | object apply_object_list(object f, list x) |
---|
38 | { |
---|
39 | return f(x); |
---|
40 | } |
---|
41 | |
---|
42 | list apply_list_list(object f, list x) |
---|
43 | { |
---|
44 | return call<list>(f.ptr(), x); |
---|
45 | } |
---|
46 | |
---|
47 | void append_object(list& x, object y) |
---|
48 | { |
---|
49 | x.append(y); |
---|
50 | } |
---|
51 | |
---|
52 | void append_list(list& x, list const& y) |
---|
53 | { |
---|
54 | x.append(y); |
---|
55 | } |
---|
56 | |
---|
57 | typedef test_class<> X; |
---|
58 | |
---|
59 | int notcmp(object const& x, object const& y) |
---|
60 | { |
---|
61 | return y < x ? -1 : y > x ? 1 : 0; |
---|
62 | } |
---|
63 | |
---|
64 | void exercise(list x, object y, object print) |
---|
65 | { |
---|
66 | x.append(y); |
---|
67 | x.append(5); |
---|
68 | x.append(X(3)); |
---|
69 | |
---|
70 | print("after append:"); |
---|
71 | print(x); |
---|
72 | |
---|
73 | print("number of", y, "instances:", x.count(y)); |
---|
74 | |
---|
75 | print("number of 5s:", x.count(5)); |
---|
76 | |
---|
77 | x.extend("xyz"); |
---|
78 | print("after extend:"); |
---|
79 | print(x); |
---|
80 | print("index of", y, "is:", x.index(y)); |
---|
81 | print("index of 'l' is:", x.index("l")); |
---|
82 | |
---|
83 | x.insert(4, 666); |
---|
84 | print("after inserting 666:"); |
---|
85 | print(x); |
---|
86 | print("inserting with object as index:"); |
---|
87 | x.insert(x[x.index(5)], "---"); |
---|
88 | print(x); |
---|
89 | |
---|
90 | print("popping..."); |
---|
91 | x.pop(); |
---|
92 | print(x); |
---|
93 | x.pop(x[x.index(5)]); |
---|
94 | print(x); |
---|
95 | x.pop(x.index(5)); |
---|
96 | print(x); |
---|
97 | |
---|
98 | print("removing", y); |
---|
99 | x.remove(y); |
---|
100 | print(x); |
---|
101 | print("removing", 666); |
---|
102 | x.remove(666); |
---|
103 | print(x); |
---|
104 | |
---|
105 | print("reversing..."); |
---|
106 | x.reverse(); |
---|
107 | print(x); |
---|
108 | |
---|
109 | print("sorted:"); |
---|
110 | x.pop(2); // make sorting predictable |
---|
111 | x.sort(); |
---|
112 | print(x); |
---|
113 | |
---|
114 | print("reverse sorted:"); |
---|
115 | x.sort(¬cmp); |
---|
116 | print(x); |
---|
117 | |
---|
118 | list w; |
---|
119 | w.append(5); |
---|
120 | w.append(6); |
---|
121 | w += "hi"; |
---|
122 | BOOST_ASSERT(w[0] == 5); |
---|
123 | BOOST_ASSERT(w[1] == 6); |
---|
124 | BOOST_ASSERT(w[2] == 'h'); |
---|
125 | BOOST_ASSERT(w[3] == 'i'); |
---|
126 | } |
---|
127 | |
---|
128 | BOOST_PYTHON_MODULE(list_ext) |
---|
129 | { |
---|
130 | def("new_list", new_list); |
---|
131 | def("listify", listify); |
---|
132 | def("listify_string", listify_string); |
---|
133 | def("apply_object_list", apply_object_list); |
---|
134 | def("apply_list_list", apply_list_list); |
---|
135 | |
---|
136 | def("append_object", append_object); |
---|
137 | def("append_list", append_list); |
---|
138 | |
---|
139 | def("exercise", exercise); |
---|
140 | |
---|
141 | class_<X>("X", init<int>()) |
---|
142 | .def( "__repr__", x_rep) |
---|
143 | ; |
---|
144 | } |
---|
145 | |
---|