为 C++ 重载函数创建 SWIG 类型映射
Creating a SWIG typemap for C++ overloaded functions
我想知道如何为重载函数创建 SWIG 类型映射。
MyBindings.h
static void test(t_string *s)
{
std::cout << "first : " << s->name << '\n');
}
static void test(t_string *s, t_string *s2)
{
std::cout << "first : " << s->name << '\n');
std::cout << "second : " << s2->name << '\n');
}
MyBindings.i
%module my
%{
#include "MyBindings.h"
%}
%include <stl.i>
%include <exception.i>
%include <typemaps.i>
/* convert the input lua_String to t_string* */
%typemap(in) t_string*
{
if (!lua_isstring(L, $input))
SWIG_exception(SWIG_RuntimeError, "argument mismatch: string expected");
= makestring(lua_tostring(L, $input));
}
如果我在 Lua 中调用 test()
,
my.test("abc", "def");
我收到以下错误:
Wrong arguments for overloaded function 'test'
Possible C/C++ prototypes are:
test(t_string *)
test(t_string *,t_string *)
我应该如何更正我的类型映射以使其工作?
这是RTFM的典型案例。见 11.5.2 "typecheck" typemap:
If you define new "in" typemaps and your program uses overloaded methods, you should also define a collection of "typecheck" typemaps. More details about this follow in the Typemaps and overloading section.
与您的问题一样,您的头文件中缺少 include guards。我自己做了 t_string.h
因为我不知道这是从哪里来的。函数 test
不能是静态的,因为毕竟你想从这个翻译单元外部引用它们,当它们有 internal linkage.
时这是不可能的
MyBindings.h
#pragma once
#include <iostream>
#include "t_string.h"
void test(t_string *s)
{
std::cout << "first : " << s->name << '\n';
}
void test(t_string *s, t_string *s2)
{
std::cout << "first : " << s->name << '\n';
std::cout << "second : " << s2->name << '\n';
}
MyBindings.i
%module my
%{
#include "MyBindings.h"
%}
/* convert the input lua_String to t_string* */
%typemap(typecheck) t_string* {
= lua_isstring(L, $input);
}
%typemap(in) t_string* {
= makestring(lua_tostring(L, $input));
}
%typemap(freearg) t_string* {
freestring();
}
%include "MyBindings.h"
test.lua
local my = require("my")
my.test("abc", "def")
调用示例:
$ swig -c++ -lua MyBindings.i
$ clang++ -Wall -Wextra -Wpedantic -I /usr/include/lua5.2 -shared -fPIC MyBindings_wrap.cxx -o my.so -llua5.2
$ lua5.2 test.lua
first : abc
second : def
我想知道如何为重载函数创建 SWIG 类型映射。
MyBindings.h
static void test(t_string *s)
{
std::cout << "first : " << s->name << '\n');
}
static void test(t_string *s, t_string *s2)
{
std::cout << "first : " << s->name << '\n');
std::cout << "second : " << s2->name << '\n');
}
MyBindings.i
%module my
%{
#include "MyBindings.h"
%}
%include <stl.i>
%include <exception.i>
%include <typemaps.i>
/* convert the input lua_String to t_string* */
%typemap(in) t_string*
{
if (!lua_isstring(L, $input))
SWIG_exception(SWIG_RuntimeError, "argument mismatch: string expected");
= makestring(lua_tostring(L, $input));
}
如果我在 Lua 中调用 test()
,
my.test("abc", "def");
我收到以下错误:
Wrong arguments for overloaded function 'test'
Possible C/C++ prototypes are:
test(t_string *)
test(t_string *,t_string *)
我应该如何更正我的类型映射以使其工作?
这是RTFM的典型案例。见 11.5.2 "typecheck" typemap:
If you define new "in" typemaps and your program uses overloaded methods, you should also define a collection of "typecheck" typemaps. More details about this follow in the Typemaps and overloading section.
与您的问题一样,您的头文件中缺少 include guards。我自己做了 t_string.h
因为我不知道这是从哪里来的。函数 test
不能是静态的,因为毕竟你想从这个翻译单元外部引用它们,当它们有 internal linkage.
MyBindings.h
#pragma once
#include <iostream>
#include "t_string.h"
void test(t_string *s)
{
std::cout << "first : " << s->name << '\n';
}
void test(t_string *s, t_string *s2)
{
std::cout << "first : " << s->name << '\n';
std::cout << "second : " << s2->name << '\n';
}
MyBindings.i
%module my
%{
#include "MyBindings.h"
%}
/* convert the input lua_String to t_string* */
%typemap(typecheck) t_string* {
= lua_isstring(L, $input);
}
%typemap(in) t_string* {
= makestring(lua_tostring(L, $input));
}
%typemap(freearg) t_string* {
freestring();
}
%include "MyBindings.h"
test.lua
local my = require("my")
my.test("abc", "def")
调用示例:
$ swig -c++ -lua MyBindings.i
$ clang++ -Wall -Wextra -Wpedantic -I /usr/include/lua5.2 -shared -fPIC MyBindings_wrap.cxx -o my.so -llua5.2
$ lua5.2 test.lua
first : abc
second : def