1

我正在使用Boost.Python来公开第 3 方 C++ API。

我来的头文件声明了一个可迭代类(hasbeginend方法),以及一个用于进行迭代的自定义迭代器类:-

// File: data.hpp

#include <utility> // for std::pair
#include <cstring> // for size_t

namespace notmylib {

    // forward declaration
    class DataIterator;

    // Storage for arbitrary data
    class Data
    {
        public:
            Data(void);
            virtual ~Data(void);
            // ...
            typedef DataIterator const_iterator;
            const_iterator begin(void) const;
            const_iterator end(void) const;

            typedef std::pair<int, int> TRange;
        private:
            TRange m_data;
    };

    // Data iterator class
    class DataIterator
    {
        public:
            // constructors
            DataIterator(void);
            DataIterator(const Data);
            ~DataIterator(void);

            // copy constructor
            DataIterator(const DataIterator& iter);
            // assignment operator
            DataIterator& operator=(const DataIterator& iter);

            // Comparison operators
            bool operator==(const DataIterator& iter) const;
            bool operator!=(const DataIterator& iter) const;

            // Data Range
            typedef TData::TRange TRange;
            TRange  GetRange(void) const;
            Data    GetRangeAsData(void) const;

            // Go backwards one step
            void   Rewind(void);
        private:
            Data   m_data;
            size_t m_index;
    };
}

我创建了从这些派生的最小包装类:-

// File: pydata.hpp
#include "data.hpp"

namespace mylib {
    class PyData
        : public notmylib::Data
    {
        public:
            PyData(void);
            virtual ~PyData(void);
    };

    class PyIterator
        : public notmylib::DataIterator
    {
        public:
            PyIterator(void);
            PyIterator(const notmylib::Data);
            PyIterator(const PyIterator& iter);

            ~PyIterator(void);
    };
}

还有 Boost.Python 声明:-

// File: pydata.cpp
#include "pydata.hpp"

#include <boost/python/class.hpp>
#include <boost/python/iterator.hpp>
#include <boost/python/module.hpp>

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

    class_<mylib::PyData, boost::noncopyable>("Data",
            "Iterable Data storage class.")
        .def("__iter__",
            iterator<const mylib::PyData>() /*, // */ )      ///< iterator / range / iterators ?
         //   boost::python::return_value_policy<            ///< This CallPolicy 
         //       boost::python::copy_const_reference> >() ) ///  doesn't work either
        ;

    class_<mylib::PyIterator>("DataIterator",
            "Iterator for Data class. Don't actually need to expose...")
        .def("__eq__", &mylib::PyIterator::operator==)
        .def("__neq__", &mylib::PyIterator::operator!=)
        // ...
        ;
}

这里唯一的奇怪之处似乎是const_iteratortypedef 的使用。这在 Boost.Python迭代器文档中有所描述,据我所知,它所需要的只是的模板参数const中的限定符。iteratorIE

boost::python::iterator<const mylib::PyData>()

但是,这会导致以下编译器错误,在 Linux x86_64 上使用 GCC 4.7.2:-

g++ -std=gnu++98 -c -g -O1  -fPIC -I/usr/include/python2.7  pydata.cpp -o pydata.o
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/stl_algobase.h:66:0,
                 from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/memory:64,
                 from /usr/include/boost/config/no_tr1/memory.hpp:21,
                 from /usr/include/boost/get_pointer.hpp:14,
                 from /usr/include/boost/python/object/pointer_holder.hpp:11,
                 from /usr/include/boost/python/to_python_indirect.hpp:10,
                 from /usr/include/boost/python/converter/arg_to_python.hpp:10,
                 from /usr/include/boost/python/call.hpp:15,
                 from /usr/include/boost/python/object_core.hpp:14,
                 from /usr/include/boost/python/object/class.hpp:9,
                 from /usr/include/boost/python/class.hpp:13,
                 from pydata.cpp:4:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/stl_iterator_base_types.h: In instantiation of 'struct std::iterator_traits<notmylib::DataIterator>':
/usr/include/boost/detail/iterator.hpp:81:8:   required from 'struct boost::detail::iterator_traits<notmylib::DataIterator>'
/usr/include/boost/python/object/iterator.hpp:58:17:   required from 'struct boost::python::objects::iterator_range<boost::python::return_value_policy<boost::python::return_by_value>, notmylib::DataIterator>::next'
/usr/include/boost/python/object/iterator.hpp:127:45:   required from 'boost::python::api::object boost::python::objects::detail::demand_iterator_class(const char*, Iterator*, const NextPolicies&) [with Iterator = notmylib::DataIterator; NextPolicies = boost::python::return_value_policy<boost::python::return_by_value>]'
/usr/include/boost/python/object/iterator.hpp:167:11:   required from 'boost::python::objects::iterator_range<NextPolicies, Iterator> boost::python::objects::detail::py_iter_<Target, Iterator, Accessor1, Accessor2, NextPolicies>::operator()(boost::python::back_reference<Target&>) const [with Target = const mylib::PyData; Iterator = notmylib::DataIterator; Accessor1 = boost::_bi::protected_bind_t<boost::_bi::bind_t<notmylib::DataIterator, notmylib::DataIterator (*)(const mylib::PyData&), boost::_bi::list1<boost::arg<1> > > >; Accessor2 = boost::_bi::protected_bind_t<boost::_bi::bind_t<notmylib::DataIterator, notmylib::DataIterator (*)(const mylib::PyData&), boost::_bi::list1<boost::arg<1> > > >; NextPolicies = boost::python::return_value_policy<boost::python::return_by_value>]'
/usr/include/boost/python/detail/invoke.hpp:75:82:   required from 'PyObject* boost::python::detail::invoke(boost::python::detail::invoke_tag_<false, false>, const RC&, F&, AC0&) [with RC = boost::python::to_python_value<const boost::python::objects::iterator_range<boost::python::return_value_policy<boost::python::return_by_value>, notmylib::DataIterator>&>; F = boost::python::objects::detail::py_iter_<const mylib::PyData, notmylib::DataIterator, boost::_bi::protected_bind_t<boost::_bi::bind_t<notmylib::DataIterator, notmylib::DataIterator (*)(const mylib::PyData&), boost::_bi::list1<boost::arg<1> > > >, boost::_bi::protected_bind_t<boost::_bi::bind_t<notmylib::DataIterator, notmylib::DataIterator (*)(const mylib::PyData&), boost::_bi::list1<boost::arg<1> > > >, boost::python::return_value_policy<boost::python::return_by_value> >; AC0 = boost::python::arg_from_python<boost::python::back_reference<const mylib::PyData&> >; PyObject = _object]'
/usr/include/boost/python/detail/caller.hpp:223:13:   required from 'PyObject* boost::python::detail::caller_arity<1u>::impl<F, Policies, Sig>::operator()(PyObject*, PyObject*) [with F = boost::python::objects::detail::py_iter_<const mylib::PyData, notmylib::DataIterator, boost::_bi::protected_bind_t<boost::_bi::bind_t<notmylib::DataIterator, notmylib::DataIterator (*)(const mylib::PyData&), boost::_bi::list1<boost::arg<1> > > >, boost::_bi::protected_bind_t<boost::_bi::bind_t<notmylib::DataIterator, notmylib::DataIterator (*)(const mylib::PyData&), boost::_bi::list1<boost::arg<1> > > >, boost::python::return_value_policy<boost::python::return_by_value> >; Policies = boost::python::default_call_policies; Sig = boost::mpl::vector2<boost::python::objects::iterator_range<boost::python::return_value_policy<boost::python::return_by_value>, notmylib::DataIterator>, boost::python::back_reference<const mylib::PyData&> >; PyObject = _object]'
/usr/include/boost/python/object/py_function.hpp:38:33:   required from 'PyObject* boost::python::objects::caller_py_function_impl<Caller>::operator()(PyObject*, PyObject*) [with Caller = boost::python::detail::caller<boost::python::objects::detail::py_iter_<const mylib::PyData, notmylib::DataIterator, boost::_bi::protected_bind_t<boost::_bi::bind_t<notmylib::DataIterator, notmylib::DataIterator (*)(const mylib::PyData&), boost::_bi::list1<boost::arg<1> > > >, boost::_bi::protected_bind_t<boost::_bi::bind_t<notmylib::DataIterator, notmylib::DataIterator (*)(const mylib::PyData&), boost::_bi::list1<boost::arg<1> > > >, boost::python::return_value_policy<boost::python::return_by_value> >, boost::python::default_call_policies, boost::mpl::vector2<boost::python::objects::iterator_range<boost::python::return_value_policy<boost::python::return_by_value>, notmylib::DataIterator>, boost::python::back_reference<const mylib::PyData&> > >; PyObject = _object]'
pydata.cpp:26:1:   required from here
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/stl_iterator_base_types.h:166:53: error: no type named 'iterator_category' in 'class notmylib::DataIterator'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/stl_iterator_base_types.h:167:53: error: no type named 'value_type' in 'class notmylib::DataIterator'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/stl_iterator_base_types.h:168:53: error: no type named 'difference_type' in 'class notmylib::DataIterator'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/stl_iterator_base_types.h:169:53: error: no type named 'pointer' in 'class notmylib::DataIterator'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/stl_iterator_base_types.h:170:53: error: no type named 'reference' in 'class notmylib::DataIterator'

应该如何暴露这样一个可迭代的类?

干杯,亚历克斯

4

1 回答 1

4

我怀疑 Boost.Python 依赖于DataIterator成为符合标准的 C++ 迭代器才能工作。

您可以尝试使用iterator_facadeBoost 中的机制来包装迭代器:

http://www.boost.org/doc/libs/release/libs/iterator/doc/iterator_facade.html

于 2013-03-27T16:09:06.307 回答