7

我正在尝试用展位 python 包装一个 C++ 模板类。我收到当前包装器的错误。该程序基本上是用于创建自定义矢量并在 python 中使用它。

#include <boost/python.hpp>

template <class T> class allocator{
  public:
  T* allocate(size_t);
  void deallocate(T* , size_t);
  void construct(T*,T);
  void destroy(T*);
};


template <class T> class vec{

  typedef T* iterator;
  typedef const T* const_iterator;
  typedef size_t size_type;
  typedef T value_type;


  vec(){ create();  }
  explicit vec(size_type n, const T& t =T()){ create (n,t);}
  vec(const vec& v){ create(v.begin(),v.end());}
  vec& operator= (const vec&);

  ~vec(){uncreate;}


  T& operator[](size_type i){return data[i];}
  const T& operator[]( size_type i) const { return data[i];}

  void push_back(const T& t){
    if (avail==limit)
      grow();
    unchecked_append(t);
  }
  size_type size() const{return avail-data;}

  iterator begin () {return data;}
  const_iterator begin () const {return data;}

  iterator end () {return avail;}
  const_iterator end () const {return avail;}

  iterator data;
  iterator limit;
  iterator avail;

  allocator<T> alloc;

  void uninitialized_fill(T*, T*, const T&);
  T* uninitialized_copy(T*,T*,T*);

  void create();
  void create(size_type,const T&);
  void create (const_iterator, const_iterator);
  void uncreate();

  void grow();
  void unchecked_append(const T&);
};

// create
template <class T> void vec<T>:: create(){
  data = avail = limit = 0;
}

template <class T> void vec<T>::create(size_type n, const T& val){
  data =alloc.allocate(n);
  limit = avail = data + n;
  uninitialized_fill(data, limit, val);
}

template <class T>
void vec<T>::create(const_iterator i, const_iterator j){
  data =  alloc.allocate(j - i);
  limit = avail = uninitialized_copy(i,j, data);
}

//uncreate
template < class T> void vec<T>::uncreate(){
  if (data){
    iterator it = avail;
    while (it != data)
      alloc.destroy(--it);

    alloc.deallocate(data, limit - data);
  }
  data = limit =avail = 0;
}

//grow
template <class T> void vec<T>::grow(){
  size_type new_size = max(2 * (limit-data), ptrdiff_t(1));

  iterator new_data = alloc.allocate(new_size);
  iterator new_avail = uninitialized_copy(data, avail, new_data);

  uncreate();

  data = new_data;
  avail = new_avail;
  limit = data + new_size;
}

template <class T> void vec<T>::unchecked_append(const T& val){
  alloc.construct(avail++,val);
}

Boost python 包装如下:

BOOST_PYTHON_MODULE(vbox_shmem_box_vec_ext)
{
  using namespace boost::python;
  class_<vec>("vec");
    .def("size",&std_item<vec>::size,
      return_value_policy<copy_non_const_reference>());
    .def("begin",&std_item<vec>::begin,
      return_value_policy<copy_non_const_reference>());
    .def("end",&std_item<vec>::end,
      return_value_policy<copy_non_const_reference>());
}

这是错误

vec_ext.cpp:113:13: error: type/value mismatch at argument 1 in template parameter list for 'template<class T, class X1, class X2, class X3> class boost::python::class_'
vec_ext.cpp:113:13: error:   expected a type, got 'vec'
vec_ext.cpp:114:5: error: expected primary-expression before '.' token
vec_ext.cpp:114:18: error: 'std_item' was not declared in this scope
vec_ext.cpp:114:30: error: missing template arguments before '>' token
vec_ext.cpp:114:31: error: '::size' has not been declared
vec_ext.cpp:114:31: note: suggested alternative:
size_fwd.hpp:20:38: note:   'boost::mpl::size'
vec_ext.cpp:116:5: error: expected primary-expression before '.' token
vec_ext.cpp:116:31: error: missing template arguments before '>' token
vec_ext.cpp:116:32: error: '::begin' has not been declared
vec_ext.cpp:116:32: note: suggested alternative:
begin_end_fwd.hpp:22:38: note:   'boost::mpl::begin'
vec_ext.cpp:118:5: error: expected primary-expression before '.' token
vec_ext.cpp:118:29: error: missing template arguments before '>' token
vec_ext.cpp:118:30: error: '::end' has not been declared
vec_ext.cpp:118:30: note: suggested alternative:
begin_end_fwd.hpp:23:38: note:   'boost::mpl::end'
4

2 回答 2

8

您的任务最简单的方法是从 STL 向量接口继承并使用 Boost Python vector_indexing_suite,否则您必须手动为 Python 接口实现切片操作,这不是那么快速和琐碎。

但是对于原始问题Wrap C++ template class那么我在包装模板时遇到了下一个问题:

template<typename LinksT>
class Base {
public:
  virtual ~Base()  {}
  virtual Base* x() = 0;
};

template<typename LinksT>
class BaseWrap : public Base<LinksT>, public wrapper<Base<LinksT>> {
public:
  virtual Base<LinksT>* x() { return this->get_override("x")(); }
};

BOOST_PYTHON_MODULE(xxx)
{
    class_<BaseWrap<LinksT>, noncopyable>("Base", no_init)
      .def("x", pure_virtual(&Base<LinksT>::x), return_internal_reference<>())
      ;
}

无法在 Linux 下的 GCC 4.8 上编译,认为直接类可以正常工作:

class Base {
public:
  virtual ~Base()  {}
  virtual Base* x() = 0;
};

class BaseWrap : public Base, public wrapper<Base> {
public:
  virtual Base* x() { return this->get_override("x")(); }
};

BOOST_PYTHON_MODULE(xxx)
{
    class_<BaseWrap, noncopyable>("Base", no_init)
      .def("x", pure_virtual(&Base::x), return_internal_reference<>())
      ;
}

有2个问题:

  1. 我必须指定-ftemplate-backtrace-limit =64(默认为 10)编译器标志以允许实例化更多内部模板
  2. 我必须在声明之前实例化模板:BOOST_PYTHON_MODULE(xxx)
template class BaseWrap<SimpleLinks>;
BOOST_PYTHON_MODULE(xxx)
{ ...

然后它工作正常。

于 2013-10-30T19:25:39.727 回答
1

您还可以为您的类添加另一个模板,这样您就不必为每个 int/float 类型编写/实例化。

template<typename LinksT>
class Base {
public:
  virtual ~Base()  {}
  virtual Base* x() = 0;
};

#include <boost/python.hpp>
using namespace boost::python;

template<typename LinksT>
class BaseWrap : public Base<LinksT>, public wrapper<Base<LinksT> > {
public:
  virtual Base<LinksT>* x() { return this->get_override("x")(); }
};

template<typename LinksT>
void export_virtualfunction()
{
    class_<BaseWrap<LinksT>, boost::noncopyable>("Base", no_init)
        .def("x", pure_virtual(&Base<LinksT>::x), return_internal_reference<>())
        ;
}

BOOST_PYTHON_MODULE(test_template_python)
{
    export_virtualfunction<int>();
}

注意导出类的不同类型。给它起不同的名字。参考链接

于 2016-09-09T10:54:42.340 回答