2011-03-15 73 views
7

我正在用boost.python库编写应用程序。我想将函数传递给Python返回std::vector。我有一个小麻烦:如何导出std :: vector

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

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

当我调用该函数从蟒蛇,我得到:

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

有什么我错过了?

回答

8

你应该写这样的转换器:

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(); 
    } 
}; 

然后你的模块中进行注册:

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

非常感谢,它的工作原理。 – Ockonal 2011-03-15 16:27:41

+0

顺便说一句,你为什么在'std :: allocator'之前写'class'? – Ockonal 2011-03-15 16:30:09

+1

....我真的不知道!我删除它,它仍然有效 – 2011-03-15 16:33:58

0

这是一个有点老问题,但我发现,如果你明确需要由价值回归,就像这样:

namespace bp = boost::python 

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

而非

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

Python为您做了转换(我在编写此答案时使用了Python 2.7),并且不需要声明/定义转换器。

@Tryskele

0

我使用下面的实用函数来从/到STL容器转换。平凡的和函数说明了它们是如何使用的。我希望你能使用它。

#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); 
}