用boost.python包装结构列表

Rob*_*cus 5 c++ python list word-wrap boost-python

我有一个C++函数,它返回一个结构列表.在struct中,有更多的结构列表.

struct CameraInfo {
    CamName                     name;
    std::list<CamImageFormat>  lImgFormats;
    std::list<CamControls>      lCamControls;
};

std::list<CameraInfo> getCameraInfo()
{
    std::list<CameraInfo> lCamerasInfo;
    // fill lCamerasInfo
    return lCamerasInfo; 
}
Run Code Online (Sandbox Code Playgroud)

然后出口我我用的是:

class_<CameraNode....> >("CameraNode", no_init)
...
...
.def("listCameraInfo", make_function(&CameraNode::listCameraInfo))
        .staticmethod("listCameraInfo")
...
;
Run Code Online (Sandbox Code Playgroud)

这是好的,因为我使用cout在屏幕上打印数据...我想现在使用返回值和它的内容来自python类属性,这样:

cameras = []
cameras = CameraNode.getCameraInfo()
print cameras[0].name
print cameras[0].lImgFormats[0]
and so on...
Run Code Online (Sandbox Code Playgroud)

这有可能吗?我应该使用add_property吗?我不认为我可以为每个结构创建一个类.这个设计在我只使用C++时很有意义,但是现在我必须把它包起来,我越来越困惑了.

有关以一般方式使用boost.python包装std :: list的任何建议都将被广泛接受.

编辑:

我将在这里添加我发现有用的链接: 迭代器 StlContainers

Ale*_*kiy 13

它必须是std::list吗?如果您使用,std::vector您可以boost::python::vector_indexing_suite用来包装列表.有关详细信息,请参阅此帖

如果必须使用std::list,则需要创建一个std::list使用python list方法包装功能的帮助器类.这可能非常复杂,但可行.

std_item.hpp:

#include <list>
#include <algorithm>
#include <boost/python.hpp>

template<class T>
struct listwrap
{
    typedef typename T::value_type value_type;
    typedef typename T::iterator iter_type;

    static void add(T & x, value_type const& v)
    {
        x.push_back(v);
    }

    static bool in(T const& x, value_type const& v)
    {
        return std::find(x.begin(), x.end(), v) != x.end();
    }

    static int index(T const& x, value_type const& v)
    {
        int i = 0;
        for(T::const_iterator it=x.begin(); it!=x.end(); ++it,++i)
            if( *it == v ) return i;

        PyErr_SetString(PyExc_ValueError, "Value not in the list");
        throw boost::python::error_already_set();
    }

    static void del(T& x, int i)
    {
        if( i<0 ) 
            i += x.size();

        iter_type it = x.begin();
        for (int pos = 0; pos < i; ++pos)
            ++it;

        if( i >= 0 && i < (int)x.size() ) {
            x.erase(it);
        } else {
            PyErr_SetString(PyExc_IndexError, "Index out of range");
            boost::python::throw_error_already_set();
        }
    }

    static value_type& get(T& x, int i)
    {
        if( i < 0 ) 
            i += x.size();

        if( i >= 0 && i < (int)x.size() ) {
            iter_type it = x.begin(); 
            for(int pos = 0; pos < i; ++pos)
                ++it;
            return *it;                             
        } else {
            PyErr_SetString(PyExc_IndexError, "Index out of range");
            throw boost::python::error_already_set();
        }
    }

    static void set(T& x, int i, value_type const& v)
    {
        if( i < 0 ) 
            i += x.size();

        if( i >= 0 && i < (int)x.size() ) {
            iter_type it = x.begin(); 
            for(int pos = 0; pos < i; ++pos)
                ++it;
            *it = v;
        } else {
            PyErr_SetString(PyExc_IndexError, "Index out of range");
            boost::python::throw_error_already_set();
        }
    }
};


template<class T>
void export_STLList(const char* typeName)
{
    using namespace boost::python;

    class_<std::list<T> >(typeName)
        .def("__len__", &std::list<T>::size)
        .def("clear", &std::list<T>::clear)
        .def("append", &listwrap<T>::add,
            with_custodian_and_ward<1,2>()) // to let container keep value
        .def("__getitem__", &listwrap<T>::get,
            return_value_policy<copy_non_const_reference>())
        .def("__setitem__", &listwrap<T>::set,
            with_custodian_and_ward<1,2>()) // to let container keep value
        .def("__delitem__", &listwrap<T>::del)
        .def("__contains__", &listwrap<T>::in)
        .def("__iter__", iterator<std::list<T> >())
        .def("index", &listwrap<T>::index);
}
Run Code Online (Sandbox Code Playgroud)

用法:

typedef std::list<int> intlist;
export_STLList<int>("intlist");
Run Code Online (Sandbox Code Playgroud)