ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
container_utils.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file container_utils.hpp
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 <boost/python/object.hpp>
11 # include <boost/python/handle.hpp>
12 # include <boost/python/extract.hpp>
13 
14 namespace boost { namespace python { namespace container_utils {
15 
16  template <typename Container>
17  void
18  extend_container(Container& container, object l)
19  {
20  typedef typename Container::value_type data_type;
21 
22  // l must be a list or some container
23 
24  for (int i = 0; i < l.attr("__len__")(); i++)
25  {
26  object elem(l[i]);
27  extract<data_type const&> x(elem);
28  // try if elem is an exact data_type type
29  if (x.check())
30  {
31  container.push_back(x());
32  }
33  else
34  {
35  // try to convert elem to data_type type
36  extract<data_type> x(elem);
37  if (x.check())
38  {
39  container.push_back(x());
40  }
41  else
42  {
43  PyErr_SetString(PyExc_TypeError, "Incompatible Data Type");
44  throw_error_already_set();
45  }
46  }
47  }
48  }
49 
50 }}} // namespace boost::python::container_utils
51 
52 #endif