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)
7 #ifndef PY_CONTAINER_UTILS_JDG20038_HPP
8 # define PY_CONTAINER_UTILS_JDG20038_HPP
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>
17 namespace boost { namespace python { namespace container_utils {
19 template <typename Container>
21 extend_container(Container& container, object l)
23 typedef typename Container::value_type data_type;
26 BOOST_FOREACH(object elem,
28 boost::python::stl_input_iterator<object>(l),
29 boost::python::stl_input_iterator<object>()
32 extract<data_type const&> x(elem);
33 // try if elem is an exact data_type type
36 container.push_back(x());
40 // try to convert elem to data_type type
41 extract<data_type> x(elem);
44 container.push_back(x());
48 PyErr_SetString(PyExc_TypeError, "Incompatible Data Type");
49 throw_error_already_set();
55 }}} // namespace boost::python::container_utils