4

T がカスタム構造体であるリストを利用する既存の C++ ライブラリのラッパーを作成しています。リストの代わりにベクトルを使用するようにアドバイスされましたが、ライブラリを変更しないようにしています。

シナリオをよりよく理解するために、Python への変換を登録するためのプロキシとしてリストを使用して、単純なアプリケーションを作成しました (読み取り専用で問題ありません)。

私の現在の実装は正常にコンパイルされ、python は正常にインポートされ、オブジェクトを作成できますが、データ メンバーを呼び出すとエラーになります。

Python シェル出力:

In [1]: import my_list

In [2]: x = my_list.Bob()

In [3]: x.foos
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-2f015d13a87d> in <module>()
----> 1 x.foos

TypeError: No Python class registered for C++ class std::list<int, std::allocator<int> >

C++ ファイル:

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

#include <boost/foreach.hpp>
#ifndef FOREACH
    #define FOREACH BOOST_FOREACH
#endif

using namespace std;
using namespace boost::python;

template<typename T>
struct list_to_list
{
    static PyObject* convert(const std::list<T>& src)
    {
        boost::python::list result;
        FOREACH (const T& val, src)
        {
            result.append(val);
        }

        return incref(result.ptr());
    }
};

struct Bob
{
    std::list<int> foos;
};

BOOST_PYTHON_MODULE(my_list)
{
    using namespace boost::python;

    to_python_converter<std::list<int>, list_to_list<int> >();

    class_<Bob>("Bob")
        .def_readonly("foos", &Bob::foos)
    ;
}

何か不足していますか?

4

1 に答える 1