用Boost python包装C ++模板类 [英] Wrap C++ template class with boost python

查看:63
本文介绍了用Boost python包装C ++模板类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用展位python包装一个C ++模板类.我在使用当前包装器时遇到了错误.该程序基本上是用于创建自定义矢量并在python中使用.

I am try to wrap a C++ template class with booth python. I get errors with the current wrapper. The program is basically for creating customized vector's and use it in 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包装如下:

the Boost python wrap is as follows:

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'

推荐答案

最简单的方法是从STL向量接口继承并使用Boost Python vector_indexing_suite ,否则,您必须手动实现切片对于Python界面的操作不是那么快速而琐碎.

The easiest way for your task is to inherit from STL vector interface and use Boost Python vector_indexing_suite, otherwise you had to manually implement slice operations for Python interface that is not so fast and trivial.

但是对于最初的问题包装C ++模板类,然后在包装模板时我遇到了下一个问题:

But as for the original question Wrap C++ template class then I faced with the next problem when wrapped templates:

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上编译,认为对于直接类来说,它可以正常工作:

fails to compile on GCC 4.8 under Linux, thought for direct classes it works fine:

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) 声明之前,我必须实例化模板:
  1. I had to specify -ftemplate-backtrace-limit=64 (default it's 10) compiler flag to allow instantiate more inner templates
  2. I had to instantiate template before the BOOST_PYTHON_MODULE(xxx) declaration:

template class BaseWrap<SimpleLinks>;
BOOST_PYTHON_MODULE(xxx)
{ ...

然后正常工作.

这篇关于用Boost python包装C ++模板类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆