SWIG/Lua中使用Boost Array的类型映射

我想构建一个用于与C++ Boost scoped_arrays配合使用的类型映射(in)。 我有C++函数可以使用boost数组,但我想将它们传递给Lua列表。

我已经看过Python的示例,但它们似乎包含太多Python特定的代码。

是否有人可以帮忙或者指示一个示例,让我开始?

点赞
用户168175
用户168175

你可以用以下内容作为起点:

%{
#include <boost/scoped_array.hpp>
%}
    
namespace boost {
    
template<class T>
class scoped_array {
public:
    scoped_array();
   〜scoped_array();
    
    void重置();
    void交换(scoped_array& b);
    
    %extend
    {
        scoped_array(unsigned n)
        {
            返回new scoped_array<T>(new T [n]);
        }
        T __getitem__(unsigned int idx)
        {
            返回(* self)[idx];
        }
        void __setitem__(unsigned int idx,T val)
        {
            (* self)[idx] = val;
        }
    };
};
    
}

它将boost::scoped_array的重要部分暴露出来,并基于SWIG在其标准类型映射库中的std::vector实现而松散实现。

它添加了特殊成员函数和一个新的构造函数,同时还分配了一些存储空间。它没有向SWIG显示一些定义,因为我在您的目标语言中看不到它们的用途。

注意:我没有编译和检查它。SWIG对此很满意,生成的包装看起来很合理。

2012-09-17 10:39:55