CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/FWCore/PythonParameterSet/src/PythonWrapper.h

Go to the documentation of this file.
00001 #ifndef FWCore_PythonParameterSet_PythonWrapper_h
00002 #define FWCore_PythonParameterSet_PythonWrapper_h
00003 
00004 #include <vector>
00005 #include <string>
00006 #include "FWCore/PythonParameterSet/interface/BoostPython.h"
00007 
00008 namespace edm {
00009 void
00010 pythonToCppException(const std::string& iType);
00011 
00012 //  boost::python::list toPythonList(const std::vector<std::string> & v);
00013   // utility to translate from an STL vector of strings to
00014   // a Python list
00015   template<typename T>
00016   boost::python::list toPythonList(const std::vector<T> & v) {
00017     boost::python::list result;
00018     for(unsigned i = 0; i < v.size(); ++i) {
00019        result.append(v[i]);
00020     }
00021     return result;
00022   }
00023 
00024 
00025 
00026   // and back.  Destroys the input via pop()s
00027   template<typename T>
00028   std::vector<T> toVector(boost::python::list & l)
00029   {
00030     std::vector<T> result;
00031     unsigned n = PyList_Size(l.ptr());
00032     boost::python::object iter_obj(boost::python::handle<>(PyObject_GetIter(l.ptr())));
00033     for(unsigned i = 0; i < n; ++i)
00034     {
00035       boost::python::object obj = boost::python::extract<boost::python::object>(iter_obj.attr("next")());
00036       result.push_back(boost::python::extract<T>(obj)); 
00037     }
00038     return result;
00039   }
00040 }
00041 
00042 #endif