• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
XX15     X():s("default") {}
XX16     X(std::string s):s(s) {}
reprX17     std::string repr() const { return s; }
resetX18     void reset() { s = "reset"; }
fooX19     void foo() { s = "foo"; }
operator ==X20     bool operator==(X const& x) const { return s == x.s; }
operator !=X21     bool operator!=(X const& x) const { return s != x.s; }
22 };
23 
x_value(X const & x)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 BOOST_PYTHON_MODULE(map_indexing_suite_ext)
31 {
32     class_<X>("X")
33         .def(init<>())
34         .def(init<X>())
35         .def(init<std::string>())
36         .def("__repr__", &X::repr)
37         .def("reset", &X::reset)
38         .def("foo", &X::foo)
39     ;
40 
41     def("x_value", x_value);
42     implicitly_convertible<std::string, X>();
43 
44     class_<std::map<std::string, X> >("XMap")
45         .def(map_indexing_suite<std::map<std::string, X> >())
46     ;
47 
48     void int_map_indexing_suite(); // moved to int_map_indexing_suite.cpp to
49     int_map_indexing_suite();      // avoid MSVC 6/7 internal structure overflow
50 
51 #if 0
52     // Compile check only...
53     class_<std::map<int, int> >("IntMap")
54         .def(map_indexing_suite<std::map<int, int> >())
55     ;
56 #endif
57 
58     // Some more..
59     class_<std::map<std::string, boost::shared_ptr<X> > >("TestMap")
60         .def(map_indexing_suite<std::map<std::string, boost::shared_ptr<X> >, true>())
61     ;
62 
63     void a_map_indexing_suite(); // moved to a_map_indexing_suite.cpp to
64     a_map_indexing_suite();      // avoid MSVC 6/7 internal structure overflow
65 
66 }
67 
68 #include "module_tail.cpp"
69