1 | // Copyright Joel de Guzman 2004. Distributed under the Boost |
---|
2 | // Software License, Version 1.0. (See accompanying |
---|
3 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
---|
4 | |
---|
5 | #include <boost/python/suite/indexing/map_indexing_suite.hpp> |
---|
6 | #include <boost/python/module.hpp> |
---|
7 | #include <boost/python/def.hpp> |
---|
8 | #include <boost/python/implicit.hpp> |
---|
9 | |
---|
10 | using namespace boost::python; |
---|
11 | |
---|
12 | struct X // a container element |
---|
13 | { |
---|
14 | std::string s; |
---|
15 | X():s("default") {} |
---|
16 | X(std::string s):s(s) {} |
---|
17 | std::string repr() const { return s; } |
---|
18 | void reset() { s = "reset"; } |
---|
19 | void foo() { s = "foo"; } |
---|
20 | bool operator==(X const& x) const { return s == x.s; } |
---|
21 | bool operator!=(X const& x) const { return s != x.s; } |
---|
22 | }; |
---|
23 | |
---|
24 | std::string x_value(X const& x) |
---|
25 | { |
---|
26 | return "gotya " + x.s; |
---|
27 | } |
---|
28 | |
---|
29 | BOOST_PYTHON_MODULE(map_indexing_suite_ext) |
---|
30 | { |
---|
31 | class_<X>("X") |
---|
32 | .def(init<>()) |
---|
33 | .def(init<X>()) |
---|
34 | .def(init<std::string>()) |
---|
35 | .def("__repr__", &X::repr) |
---|
36 | .def("reset", &X::reset) |
---|
37 | .def("foo", &X::foo) |
---|
38 | ; |
---|
39 | |
---|
40 | def("x_value", x_value); |
---|
41 | implicitly_convertible<std::string, X>(); |
---|
42 | |
---|
43 | class_<std::map<std::string, X> >("XMap") |
---|
44 | .def(map_indexing_suite<std::map<std::string, X> >()) |
---|
45 | ; |
---|
46 | |
---|
47 | void int_map_indexing_suite(); // moved to int_map_indexing_suite.cpp to |
---|
48 | int_map_indexing_suite(); // avoid MSVC 6/7 internal structure overflow |
---|
49 | |
---|
50 | #if 0 |
---|
51 | // Compile check only... |
---|
52 | class_<std::map<int, int> >("IntMap") |
---|
53 | .def(map_indexing_suite<std::map<int, int> >()) |
---|
54 | ; |
---|
55 | #endif |
---|
56 | |
---|
57 | // Some more.. |
---|
58 | class_<std::map<std::string, boost::shared_ptr<X> > >("TestMap") |
---|
59 | .def(map_indexing_suite<std::map<std::string, boost::shared_ptr<X> >, true>()) |
---|
60 | ; |
---|
61 | } |
---|
62 | |
---|
63 | #include "module_tail.cpp" |
---|