gpt4 book ai didi

c++ - 用 boost python 包装 C++ 模板类

转载 作者:可可西里 更新时间:2023-11-01 18:28:40 26 4
gpt4 key购买 nike

我正在尝试用 booth python 包装一个 C++ 模板类。当前包装器出现错误。该程序基本上用于创建自定义 vector 并在 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 wrap 如下:

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 vector 接口(interface)并使用 Boost Python vector_indexing_suite,否则您必须手动为 Python 接口(interface)实现切片操作,这不是那么快速和琐碎。

但是对于最初的问题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<>())
;
}

有两个问题:

  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)
{ ...

然后它工作正常。

关于c++ - 用 boost python 包装 C++ 模板类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10096914/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com