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/vector_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)24std::string x_value(X const& x) 25 { 26 return "gotya " + x.s; 27 } 28 BOOST_PYTHON_MODULE(vector_indexing_suite_ext)29BOOST_PYTHON_MODULE(vector_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::vector<X> >("XVec") 44 .def(vector_indexing_suite<std::vector<X> >()) 45 ; 46 47 // Compile check only... 48 class_<std::vector<float> >("FloatVec") 49 .def(vector_indexing_suite<std::vector<float> >()) 50 ; 51 52 // Compile check only... 53 class_<std::vector<bool> >("BoolVec") 54 .def(vector_indexing_suite<std::vector<bool> >()) 55 ; 56 57 // vector of strings 58 class_<std::vector<std::string> >("StringVec") 59 .def(vector_indexing_suite<std::vector<std::string> >()) 60 ; 61 } 62 63