1 2 // (C) Copyright Joel de Guzman 2003. 3 // Distributed under the Boost Software License, Version 1.0. (See 4 // accompanying file LICENSE_1_0.txt or copy at 5 // http://www.boost.org/LICENSE_1_0.txt) 6 7 #ifndef PY_CONTAINER_UTILS_JDG20038_HPP 8 # define PY_CONTAINER_UTILS_JDG20038_HPP 9 10 # include <utility> 11 # include <boost/foreach.hpp> 12 # include <boost/python/object.hpp> 13 # include <boost/python/handle.hpp> 14 # include <boost/python/extract.hpp> 15 # include <boost/python/stl_iterator.hpp> 16 17 namespace boost { namespace python { namespace container_utils { 18 19 template <typename Container> 20 void extend_container(Container & container,object l)21 extend_container(Container& container, object l) 22 { 23 typedef typename Container::value_type data_type; 24 25 // l must be iterable 26 BOOST_FOREACH(object elem, 27 std::make_pair( 28 boost::python::stl_input_iterator<object>(l), 29 boost::python::stl_input_iterator<object>() 30 )) 31 { 32 extract<data_type const&> x(elem); 33 // try if elem is an exact data_type type 34 if (x.check()) 35 { 36 container.push_back(x()); 37 } 38 else 39 { 40 // try to convert elem to data_type type 41 extract<data_type> x(elem); 42 if (x.check()) 43 { 44 container.push_back(x()); 45 } 46 else 47 { 48 PyErr_SetString(PyExc_TypeError, "Incompatible Data Type"); 49 throw_error_already_set(); 50 } 51 } 52 } 53 } 54 55 }}} // namespace boost::python::container_utils 56 57 #endif 58