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