具有模板化类的SWIG_SHARED_PTR宏

时间:2011-04-05 22:06:18

标签: c++ python templates swig shared-ptr

我正在使用SWIG和boost共享指针来创建python扩展。我目前的问题是SWIG_SHARED_PTR宏似乎与模板化的类不同。我将举两个例子,一个没有模板(例子),一个带模板(例子2)。

首先,我将包含代码,最后显示python中两个扩展的行为差异。

基本问题是没有模板,共享指针在python中显示为

<example.Derived; proxy of <Swig Object of type 'derived_sptr *' at 0xb772c4b8> >

而当包装类是实例化模板时,显示为

<Swig Object of type 'derived_int_sptr *' at 0xb787e4b8>

我是C ++模板的新手,所以很有可能我遇到的问题与SWIG无关。

没有模板(工作正常)

example.h文件

#include <boost/shared_ptr.hpp>                                                                                                

#ifndef EXAMPLE_H                                                                                                              
#define EXAMPLE_H                                                                                                              

class Base {                                                                                                                   
 public:                                                                                                                       
  Base(int number1);                                                                                                           
  int get_number1();                                                                                                           
  virtual int get_number2() = 0;                                                                                               
 protected:                                                                                                                    
  int number1;                                                                                                                 
};                                                                                                                             

class Derived : public Base {                                                                                                  
 public:                                                                                                                       
  Derived(int number1, int number2);                                                                                           
  int get_number2();                                                                                                           
 private:                                                                                                                      
  int number2;                                                                                                                 
};                                                                                                                             

typedef boost::shared_ptr<Base> base_sptr;                                                                                     
typedef boost::shared_ptr<Derived> derived_sptr;                                                                               

derived_sptr make_derived(int number1, int number2);                                                                           

int get_number1(base_sptr b);                                                                                                  

#endif

example.cc

#include "example.h"                                                                                                       

Base::Base(int number1) :                                                                                                  
  number1(number1)                                                                                                         
{}                                                                                                                         

int Base::get_number1() {                                                                                                  
  return number1;                                                                                                          
}                                                                                                                          

Derived::Derived(int number1, int number2) :                                                                               
  Base(number1),                                                                                                           
  number2(number2)                                                                                                         
{}                                                                                                                         

int Derived::get_number2() {                                                                                               
  return number2;                                                                                                          
}                                                                                                                          

derived_sptr make_derived(int number1, int number2) {                                                                      
  return derived_sptr(new Derived(number1, number2));                                                                      
}                                                                                                                          

int get_number1(base_sptr b) {                                                                                             
  return b->get_number1();                                                                                                 
}

example.i

%module example                                                                                                            
%{                                                                                                                         
  #include "example.h"                                                                                                     
%}                                                                                                                         

%include boost_shared_ptr.i                                                                                                

SWIG_SHARED_PTR(base_sptr, Base)                                                                                           
SWIG_SHARED_PTR_DERIVED(derived_sptr, Base, Derived)                                                                       

%include example.h                                                                                                         

使用模板(不起作用)

example2.h

#include <boost/shared_ptr.hpp>                                                                                            

#ifndef EXAMPLE2_H                                                                                                          
#define EXAMPLE2_H                                                                                                          

template <typename T>                                                                                                      
class Base {                                                                                                               
 public:                                                                                                                   
  Base(T number1);                                                                                                         
  T get_number1();                                                                                                         
  virtual T get_number2() = 0;                                                                                             
 protected:                                                                                                                
  T number1;                                                                                                               
};                                                                                                                         

template <typename T>                                                                                                      
class Derived : public Base<T> {                                                                                           
 public:                                                                                                                   
  Derived(T number1, T number2);                                                                                           
  T get_number2();                                                                                                         
 private:                                                                                                                  
  T number2;                                                                                                               
};                                                                                                                         

typedef Base<int> base_int;                                                                                                
typedef Derived<int> derived_int;                                                                                          

typedef boost::shared_ptr<base_int> base_int_sptr;                                                                         
typedef boost::shared_ptr<derived_int> derived_int_sptr;                                                                   

derived_int_sptr make_derived_int(int number1, int number2);                                                               

int get_number1_int(base_int_sptr b);          

#endif

example2.cc

#include "example2.h"                                                                                                       

template <typename T>                                                                                                      
Base<T>::Base(T number1) :                                                                                                 
  number1(number1)                                                                                                         
{}                                                                                                                         

template <typename T>                                                                                                      
T Base<T>::get_number1() {                                                                                                 
  return number1;                                                                                                          
}                                                                                                                          

template <typename T>                                                                                                      
Derived<T>::Derived(T number1, T number2) :                                                                                
  Base<T>(number1),                                                                                                        
  number2(number2)                                                                                                         
{}                                                                                                                         

template <typename T>                                                                                                      
T Derived<T>::get_number2() {                                                                                              
  return number2;                                                                                                          
}                                                                                                                          

template class Base<int>;                                                                                                  
template class Derived<int>;                                                                                               

derived_int_sptr make_derived_int(int number1, int number2) {                                                              
  return derived_int_sptr(new Derived<int>(number1, number2));                                                             
}                                                                                                                          

int get_number1_int(base_int_sptr b) {                                                                                     
  return b->get_number1();                                                                                                 
} 

example2.i

%module example2                                                                                                            
%{                                                                                                                         
  #include "example2.h"                                                                                                     
%}                                                                                                                         

%include boost_shared_ptr.i                                                                                                

SWIG_SHARED_PTR(base_int_sptr, base_int)                                                                                   
SWIG_SHARED_PTR_DERIVED(derived_int_sptr, base_int, derived_int)                                                           

%include example.h                                                                                                         

%template(base_int) Base<int>;                                                                                             
%template(derived_int) Derived<int>;                                                                                       

差异示例:

> import example
> d = example.make_derived(4, 5)
> d
> <example.Derived; proxy of <Swig Object of type 'derived_sptr *' at 0xb77327a0> >
> d.get_number1()
4
> example.get_number1(d)
4
> import example2
> d = example2.make_derived_int(4, 5)
> d
<Swig Object of type 'derived_int_sptr *' at 0xb787e4b8>
> d.get_number1()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'SwigPyObject' object has no attribute 'get_number1'
> example.get_number1_int(d)
4

2 个答案:

答案 0 :(得分:1)

SWIG对内部类型系统对象暴露于它的顺序非常敏感。

尝试移动%template语句,使其位于SWIG_SHARED_PTR语句之前。

答案 1 :(得分:0)

在example2.i中的行

SWIG_SHARED_PTR(base_int_sptr, base_int) 
SWIG_SHARED_PTR_DERIVED(derived_int_sptr, base_int, derived_int)

应替换为

SWIG_SHARED_PTR(base_int_sptr, Base<int>)                              
SWIG_SHARED_PTR_DERIVED(derived_int_sptr, Base<int>, Derived<int>)