map_indexing_suite.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. #include <boost/python/suite/indexing/map_indexing_suite.hpp>
  5. #include <boost/python/module.hpp>
  6. #include <boost/python/def.hpp>
  7. #include <boost/python/implicit.hpp>
  8. using namespace boost::python;
  9. struct X // a container element
  10. {
  11. std::string s;
  12. X():s("default") {}
  13. X(std::string s):s(s) {}
  14. std::string repr() const { return s; }
  15. void reset() { s = "reset"; }
  16. void foo() { s = "foo"; }
  17. bool operator==(X const& x) const { return s == x.s; }
  18. bool operator!=(X const& x) const { return s != x.s; }
  19. };
  20. std::string x_value(X const& x)
  21. {
  22. return "gotya " + x.s;
  23. }
  24. BOOST_PYTHON_MODULE(map_indexing_suite_ext)
  25. {
  26. class_<X>("X")
  27. .def(init<>())
  28. .def(init<X>())
  29. .def(init<std::string>())
  30. .def("__repr__", &X::repr)
  31. .def("reset", &X::reset)
  32. .def("foo", &X::foo)
  33. ;
  34. def("x_value", x_value);
  35. implicitly_convertible<std::string, X>();
  36. class_<std::map<std::string, X> >("XMap")
  37. .def(map_indexing_suite<std::map<std::string, X> >())
  38. ;
  39. void int_map_indexing_suite(); // moved to int_map_indexing_suite.cpp to
  40. int_map_indexing_suite(); // avoid MSVC 6/7 internal structure overflow
  41. #if 0
  42. // Compile check only...
  43. class_<std::map<int, int> >("IntMap")
  44. .def(map_indexing_suite<std::map<int, int> >())
  45. ;
  46. #endif
  47. // Some more..
  48. class_<std::map<std::string, boost::shared_ptr<X> > >("TestMap")
  49. .def(map_indexing_suite<std::map<std::string, boost::shared_ptr<X> >, true>())
  50. ;
  51. void a_map_indexing_suite(); // moved to a_map_indexing_suite.cpp to
  52. a_map_indexing_suite(); // avoid MSVC 6/7 internal structure overflow
  53. }
  54. #include "module_tail.cpp"