vector_indexing_suite.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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/vector_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(vector_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::vector<X> >("XVec")
  37. .def(vector_indexing_suite<std::vector<X> >())
  38. ;
  39. // Compile check only...
  40. class_<std::vector<float> >("FloatVec")
  41. .def(vector_indexing_suite<std::vector<float> >())
  42. ;
  43. // Compile check only...
  44. class_<std::vector<bool> >("BoolVec")
  45. .def(vector_indexing_suite<std::vector<bool> >())
  46. ;
  47. // vector of strings
  48. class_<std::vector<std::string> >("StringVec")
  49. .def(vector_indexing_suite<std::vector<std::string> >())
  50. ;
  51. }