Swig - 绑定一个方法,返回指向一个导演指针的引用 class
Swig - binding a method returning a reference to a pointer of a director class
好吧,标题有点长,但我想不出一个更短的:)所以让我解释一下。
我有一个 C++ 代码库,其中有一些容器 类。那些 类 具有通过引用或 const 引用返回项目的访问方法。然后在代码库的另一部分,我分配了 object
的堆容器,它们使用了 director 特性。
而且我不知道如何为 object
专门化我们的容器 类:对于返回对项目的引用的所有容器方法(在本例中是对 [=15= 的引用) ] 的指针),Swig 生成一个包装代码,由于 dynamic_cast 无效而无法编译(基本上它会尝试将 object **
转换为 Swig::Director *
)
我设法用以下代码重现了这个问题。
Test.h
#ifndef TEST_H
#define TEST_H
template< typename T >
class Vector
{
public:
inline Vector(void)
: m_Data(nullptr)
, m_Size(0)
{
}
inline ~Vector(void)
{
delete [] m_Data;
}
inline void add(const T & item)
{
T * data = new T [m_Size + 1];
for (int i = 0; i < m_Size; ++i)
{
data[i] = std::move(m_Data[i]);
}
delete [] m_Data;
m_Data = data;
m_Data[m_Size++] = item;
}
inline const T& item(int index) const
{
return m_Data[index];
}
inline int count(void) const
{
return m_Size;
}
private:
T * m_Data;
int m_Size;
};
class Foo
{
public:
Foo(void) = default;
virtual ~Foo(void) = default;
virtual const char * method(void) const
{
return "Foo::method";
}
};
class Cache
{
public:
static void add(Foo * item = nullptr)
{
m_Cache.add(item == nullptr ? new Foo() : item);
}
static const Vector< Foo * > & get(void)
{
return m_Cache;
}
static Foo * get(int index)
{
return m_Cache.item(index);
}
private:
static Vector< Foo * > m_Cache;
};
Vector< Foo * > Cache::m_Cache;
#endif // TEST_H
core.i
%module(directors="1") core
// we want to be able to inherit Foo in Python
%feature("director") Foo;
// generate wrappers
%include "Test.h"
// specialize Vector for Foo
%template(FooVector) Vector<Foo*>;
// when compiling the wrapper code, include those
%{
#include "Test.h"
%}
如果您生成 Python 模块 (swig.exe -python -c++ core.i
) 它工作正常,但是生成的 core_wrap.cxx
文件无法构建,因为为 Vector::item
生成的包装器代码包含从 Foo **
到 Swig::Director *
的无效 dynamic_cast
有问题的行是(结果的类型是Foo **
)
director = SWIG_DIRECTOR_CAST(result);
如果我像这样手动修复它:
director = SWIG_DIRECTOR_CAST(*result);
然后模块编译正确,一切正常。
所以基本上我的问题是:这是 Swig 中的错误吗?我做错了吗?是否有解决方法告诉 Swig 在转换为 Swig::Director *
之前正确取消引用我的 Foo **
结果?
感谢任何帮助:)
我找到了解决您问题的方法。
在某些情况下,使用导向器功能和本机 RTTI 会生成无法立即编译的代码,例如当使用来自 Python.
的 distutils
时
适用于您的示例的解决方案是在编译代码时定义 SWIG_DIRECTOR_NORTTI
,例如对于 GNU 编译器,将 -DSWIG_DIRECTOR_NORTTI
添加到 CXXFLAGS
好吧,标题有点长,但我想不出一个更短的:)所以让我解释一下。
我有一个 C++ 代码库,其中有一些容器 类。那些 类 具有通过引用或 const 引用返回项目的访问方法。然后在代码库的另一部分,我分配了 object
的堆容器,它们使用了 director 特性。
而且我不知道如何为 object
专门化我们的容器 类:对于返回对项目的引用的所有容器方法(在本例中是对 [=15= 的引用) ] 的指针),Swig 生成一个包装代码,由于 dynamic_cast 无效而无法编译(基本上它会尝试将 object **
转换为 Swig::Director *
)
我设法用以下代码重现了这个问题。
Test.h
#ifndef TEST_H
#define TEST_H
template< typename T >
class Vector
{
public:
inline Vector(void)
: m_Data(nullptr)
, m_Size(0)
{
}
inline ~Vector(void)
{
delete [] m_Data;
}
inline void add(const T & item)
{
T * data = new T [m_Size + 1];
for (int i = 0; i < m_Size; ++i)
{
data[i] = std::move(m_Data[i]);
}
delete [] m_Data;
m_Data = data;
m_Data[m_Size++] = item;
}
inline const T& item(int index) const
{
return m_Data[index];
}
inline int count(void) const
{
return m_Size;
}
private:
T * m_Data;
int m_Size;
};
class Foo
{
public:
Foo(void) = default;
virtual ~Foo(void) = default;
virtual const char * method(void) const
{
return "Foo::method";
}
};
class Cache
{
public:
static void add(Foo * item = nullptr)
{
m_Cache.add(item == nullptr ? new Foo() : item);
}
static const Vector< Foo * > & get(void)
{
return m_Cache;
}
static Foo * get(int index)
{
return m_Cache.item(index);
}
private:
static Vector< Foo * > m_Cache;
};
Vector< Foo * > Cache::m_Cache;
#endif // TEST_H
core.i
%module(directors="1") core
// we want to be able to inherit Foo in Python
%feature("director") Foo;
// generate wrappers
%include "Test.h"
// specialize Vector for Foo
%template(FooVector) Vector<Foo*>;
// when compiling the wrapper code, include those
%{
#include "Test.h"
%}
如果您生成 Python 模块 (swig.exe -python -c++ core.i
) 它工作正常,但是生成的 core_wrap.cxx
文件无法构建,因为为 Vector::item
生成的包装器代码包含从 Foo **
到 Swig::Director *
有问题的行是(结果的类型是Foo **
)
director = SWIG_DIRECTOR_CAST(result);
如果我像这样手动修复它:
director = SWIG_DIRECTOR_CAST(*result);
然后模块编译正确,一切正常。
所以基本上我的问题是:这是 Swig 中的错误吗?我做错了吗?是否有解决方法告诉 Swig 在转换为 Swig::Director *
之前正确取消引用我的 Foo **
结果?
感谢任何帮助:)
我找到了解决您问题的方法。
在某些情况下,使用导向器功能和本机 RTTI 会生成无法立即编译的代码,例如当使用来自 Python.
的distutils
时
适用于您的示例的解决方案是在编译代码时定义 SWIG_DIRECTOR_NORTTI
,例如对于 GNU 编译器,将 -DSWIG_DIRECTOR_NORTTI
添加到 CXXFLAGS