我使用模板类来处理C ++中的 smart 指针。这是模板的代码(简化)
#ifndef __IL_REF_PTR_H__
#define __IL_REF_PTR_H__
// (More stuff here...)
// This is for DLL creation
#ifdef OPENIL_EXPORTS
#define OPENIL_API __declspec(dllexport)
#else
#define OPENIL_API __declspec(dllimport)
#endif
namespace openil
{
/** Smart pointer for handling referenced counted objects.*/
template<class T> class OPENIL_API IL_ref_ptr
{
public:
typedef T element_type;
IL_ref_ptr() :m_ptr(0L) {}
IL_ref_ptr(T* t):m_ptr(t) { if (m_ptr) m_ptr->ref(); }
IL_ref_ptr(const IL_ref_ptr& rp):m_ptr(rp.m_ptr) { if (m_ptr) m_ptr->ref(); }
~IL_ref_ptr() { if (m_ptr) m_ptr->unref(); m_ptr=0; }
inline IL_ref_ptr& operator = (const IL_ref_ptr& rp)
{
if (m_ptr==rp.m_ptr)
return *this;
T* tmp_ptr = m_ptr;
m_ptr = rp.m_ptr;
if (m_ptr)
m_ptr->ref();
// unref second to prevent any deletion of any object which might
// be referenced by the other object. i.e rp is child of the
// original _ptr.
if (tmp_ptr)
tmp_ptr->unref();
return *this;
}
inline IL_ref_ptr& operator = (T* ptr)
{
if (m_ptr==ptr)
return *this;
T* tmp_ptr = m_ptr;
m_ptr = ptr;
if (m_ptr)
m_ptr->ref();
// unref second to prevent any deletion of any object which might
// be referenced by the other object. i.e rp is child of the
// original _ptr.
if (tmp_ptr)
tmp_ptr->unref();
return *this;
}
// comparison operators for IL_ref_ptr.
inline bool operator == (const IL_ref_ptr& rp) const { return (m_ptr==rp.m_ptr); }
inline bool operator != (const IL_ref_ptr& rp) const { return (m_ptr!=rp.m_ptr); }
inline bool operator < (const IL_ref_ptr& rp) const { return (m_ptr<rp.m_ptr); }
inline bool operator > (const IL_ref_ptr& rp) const { return (m_ptr>rp.m_ptr); }
// comparison operator for const T*.
inline bool operator == (const T* ptr) const { return (m_ptr==ptr); }
inline bool operator != (const T* ptr) const { return (m_ptr!=ptr); }
inline bool operator < (const T* ptr) const { return (m_ptr<ptr); }
inline bool operator > (const T* ptr) const { return (m_ptr>ptr); }
inline T& operator * () {return *m_ptr;}
inline const T& operator * () const {return *m_ptr;}
inline T *operator -> () {return m_ptr;}
inline const T *operator -> () const {return m_ptr;}
inline bool operator ! () const {return m_ptr == 0L;}
inline bool valid () const {return m_ptr != 0L;}
inline T *get () {return m_ptr;}
inline const T *get () const {return m_ptr;}
/** take control over the object pointed to by IL_ref_ptr, unreference but do not delete even if ref count goes to 0,
* return the pointer to the object.
* Note, do not use this unless you are 100% sure your code handles the deletion of the object correctly, and
* only use when absolutely required.*/
inline T* take () {return release();}
inline T* release ()
{
T *tmp = m_ptr;
if (m_ptr)
m_ptr->unref_nodelete();
m_ptr = 0;
return tmp;
}
private:
T* m_ptr;
};
}
#endif
此模板是复杂C ++ DLL的一部分,我需要在Java,Python,PHP和Ruby项目中使用它。所以,我尝试使用SWIG 3创建一个包装器。但是阅读documentation,我发现了这个:
目前不支持以下C ++功能:已重载 某些运算符的版本(new,delete等)
由于我在模板中基本上超载了一些操作符,我是否会遇到SWIG问题?我之前从未使用过它,所以我想在获得结果之前需要相当长的时间。我不想走所有路,然后发现它对我的目的没用(再次:使用我的Java库,Python PHP和Ruby库)。
我发现了this类似问题,但尚无答案。
非常感谢提前