2011-03-15 24 views
7

Escribo aplicaciones con la biblioteca boost.python. Quiero pasar la función a python que devuelve std::vector. Tengo un poco de dificultad:Cómo exportar std :: vector

inline std::vector<std::string> getConfigListValue(const std::string &key) 
{ 
    return configManager().getListValue(key); 
} 

BOOST_PYTHON_MODULE(MyModule) 
{ 
    bp::def("getListValue", getListValue); 
} 

Cuando llamo a que la función de pitón me sale:

TypeError: No to_python (by-value) converter found for C++ type: std::vector<std::string, std::allocator<std::string> > 

Qué me he perdido?

Respuesta

8

Usted debe escribir un convertidor de la siguiente manera:

template<class T> 
struct VecToList 
{ 
    static PyObject* convert(const std::vector<T>& vec) 
    { 
     boost::python::list* l = new boost::python::list(); 
     for(size_t i = 0; i < vec.size(); i++) { 
      l->append(vec[i]); 
     } 

     return l->ptr(); 
    } 
}; 

Y luego registrarlo en su módulo:

BOOST_PYTHON_MODULE(MyModule) 
{ 
    boost::python::to_python_converter<std::vector<std::string, std::allocator<std::string> >, VecToList<std::string> >(); 
    boost::python::def("getListValue", getListValue); 
} 
+0

Muchas gracias, funciona. – Ockonal

+0

Por cierto, ¿por qué escribiste 'clase' antes de' std :: allocator'? – Ockonal

+1

.... ¡Realmente no lo sé! Lo eliminé y todavía funciona –

0

Es una vieja pregunta poco, pero me he dado cuenta que si explícitamente requerirá para volver por valor, de este modo:

namespace bp = boost::python 

BOOST_PYTHON_MODULE(MyModule) 
{ 
    bp::def("getListValue", getListValue, 
      bp::return_value_policy<bp::return_by_value>()); 
}  

en lugar de

BOOST_PYTHON_MODULE(MyModule) 
{ 
    bp::def("getListValue", getListValue); 
} 

Python hace la conversión por usted (estoy usando Python 2.7 en el momento de escribir esta respuesta) y no hay necesidad de declarar/definir el convertidor.

@Tryskele

0

I utilizar siguiente funciones de utilidad para convertir desde/hacia contenedores STL. La función de suma trivial ilustra cómo se usan. Espero que puedas usarlo.

#include <vector> 
#include <boost/python.hpp> 
#include <boost/python/object.hpp> 
#include <boost/python/stl_iterator.hpp> 

namespace bpy = boost::python; 

namespace fm { 

template <typename Container> 
bpy::list stl2py(const Container& vec) { 
    typedef typename Container::value_type T; 
    bpy::list lst; 
    std::for_each(vec.begin(), vec.end(), [&](const T& t) { lst.append(t); }); 
    return lst; 
} 

template <typename Container> 
void py2stl(const bpy::list& lst, Container& vec) { 
    typedef typename Container::value_type T; 
    bpy::stl_input_iterator<T> beg(lst), end; 
    std::for_each(beg, end, [&](const T& t) { vec.push_back(t); }); 
} 

bpy::list sum(const bpy::list& lhs, const bpy::list& rhs) { 
    std::vector<double> lhsv; 
    py2stl(lhs, lhsv); 

    std::vector<double> rhsv; 
    py2stl(rhs, rhsv); 

    std::vector<double> result(lhsv.size(), 0.0); 
    for (int i = 0; i < lhsv.size(); ++i) { 
    result[i] = lhsv[i] + rhsv[i]; 
    } 
    return stl2py(result); 
} 

} // namespace fm 

BOOST_PYTHON_MODULE(entry) 
{ 
    // intended to be fast math's fast sum :) 
    bpy::def("sum", &fm::sum); 
}