- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
比如有一个用C++写的类:
//Say.h
#pragma once
#include <iostream>
class Say
{
public:
Say() {}
virtual ~Say() {}
virtual void SaySomething() { std::cout << "It should not be show..\n"; };
};
inline void CallCppFun(Say& intf) {
intf.SaySomething();
}
然后我写了 Say.i:
//Say.i
%module Test
%{
#include "Say.h"
%}
%include "Say.h"
%inline %{
inline void CallCppFun(Say& intf);
%}
和 main.cpp:
//main.cpp
#include <iostream>
extern "C"
{
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
/* the SWIG wrappered library */
extern "C" int luaopen_Test(lua_State*L);
using namespace std;
int main()
{
lua_State *L;
L = luaL_newstate();
luaL_openlibs(L);
printf("[C] now loading the SWIG wrapped library\n");
luaopen_Test(L);
if (luaL_loadfile(L, "Test.lua") || lua_pcall(L, 0, 0, 0)) {
printf("[C] ERROR: cannot run lua file: %s", lua_tostring(L, -1));
exit(3);
}
return 0;
}
然后运行命令:
swig -c++ -lua say.i
我编译自动生成的文件 example_wrap.cxx 和其他 cpp 文件并成功链接。
我想在Test.lua中做的是在lua中继承C++的Say
类:
-- Test.lua
Test.Say.SaySomething = function(self)
print("Inherit from C++ in Lua")
end
my = Test.Say()
my:SaySomething() -- doesn't appear to inherit successfully in lua call
Test.CallCppFun(my) -- doesn't appear to inherit successfully in c++ call
打印的结果在lua调用和c++调用中都没有出现继承成功:
[C] now loading the SWIG wrapped library
It should not be show..
It should not be show..
我知道在 Java 中支持从 C++ 继承:generating-java-interface-with-swig
我知道这里有一个类似的问题,但没有给出我面临的具体问题的答案:implementing-and-inheriting-from-c-classes-in-lua-using-swig
Lua 是否支持使用 SWIG 从 lua 中的 C++ 类继承,或者甚至只使用纯 lua?请显示一些代码示例。如果 SWIG 无法完成这项工作,它是否有一些第三方库支持可以轻松完成?
最佳答案
似乎 SWIG with Lua doesn't actually support directors 是跨语言多态性工作所必需的。但这还不是世界末日,看起来您的 SaySomething
方法是 Say
类中唯一的虚拟方法,因此您可以替换为单个回调函数。 (如果是我,我会在 C++ 接口(interface)设计中使用 std::function
,这会稍微简化这项工作的其余部分)。
为了展示它是如何工作的(并在此过程中向我学习一些 Lua!)我整理了一个演示,它对问题中的测试用例进行了稍微简化。
本质上,我最终做的是修改 Callback
类的“in”类型映射,以使用 luaL_ref
保留对匿名函数的“引用”,当且仅当输入是given 不是回调类型。因此,我的类型映射检查它被赋予的类型,并构造一个临时的本地类型的实例,该实例继承自 Callback
类型,持有并使用对某些 Lua 定义代码的引用,直到我们用 lua_pcall
调用它.由于我们只在调用期间保留该实例,因此我们稍后会在 argfree 类型映射中清理它。 (我从 a Lua mailing list post 得到这个想法)
所以我的界面在一个地方演示了这一切,如下所示:
%module test
%{
#include <iostream>
%}
%typemap(in) const Callback& (Callback *tmp=NULL) %{
if(lua_isuserdata(L,$argnum)) {
if (!SWIG_IsOK(SWIG_ConvertPtr(L,$input,(void**)&$1,$descriptor,$disown))){
SWIG_fail_ptr("$symname",$argnum,$descriptor);
}
}
else if (lua_isfunction(L,$argnum)) {
struct Lua$basetype : $basetype {
Lua$basetype(lua_State* L, int idx) : L(L) {
lua_pushvalue(L, idx); // This gets popped by luaL_ref
// retain our argument
ref = luaL_ref(L, LUA_REGISTRYINDEX);
}
virtual ~Lua$basetype() {
// release our reference
luaL_unref(L, LUA_REGISTRYINDEX, ref);
}
virtual void run(const std::string& str) const {
// push our 'reference' onto the stack
lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
// manually prepare the function arguments
lua_pushstring(L, str.c_str());
// make the actual calll
lua_pcall(L, 1, 0, 0);
// TODO: catch error and throw as C++ exception?
}
private:
lua_State* L; // Uh, is keeping this around bad?
int ref;
};
tmp = new Lua$basetype(L,$input);
$1 = tmp;
}
else {
SWIG_fail_arg("$symname",$argnum,"$1_type");
}
%}
%typemap(freearg) const Callback& %{
delete tmp$argnum; // Fine, even if NULL remember
%}
%inline %{
struct Callback {
virtual ~Callback() {}
virtual void run(const std::string& str) const { std::cout << "Got string: " << str << "\n"; }
};
void call_me(const Callback& cb) {
std::cout << "Hello World:\n";
cb.run("DO IT NOW");
}
%}
这就足够了,我可以将它与以下 Lua 代码一起使用:
require("test")
cb=test.Callback()
test.call_me(cb)
cb=function(s) print("Lua got string: "..s) end
test.call_me(cb)
我可以编译和运行的:
swig3.0 -lua -c++ test.i
g++ -Wall -Wextra test_wrap.cxx -shared -o test.so -I /usr/include/lua5.1/
lua run.lua
Hello World:
Got string: DO IT NOW
Hello World:
Lua got string: DO IT NOW
请注意,这个示例几乎使我编写的 Lua 总量翻了一番,因此在用于生产之前,您真的应该检查我所做的是否合理。如果您更熟悉 Lua,可能可以使用类似这样的方法向 SWIG 添加适当的导向器支持,或者更好地模拟它(即,一种类型中有多个虚函数)。
关于c++ - 如何使用 SWIG 从 lua 中的 C++ 类继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39721608/
如何从 a.lua 传递值至 b.lua ? 让我们说在我的 a.lua我有这个变量代码。 local value = "Hello WOrld!" director:changeScene ("b"
我有一个使用命令行解释器运行的 lua 脚本,该脚本需要从文件加载表。 该表存储如下: create_object_action = { reflexive = true, which
我通过静态链接的方式在我的项目中嵌入了 Win32 上的 Lua(不,我不能切换到 DLL)。我想捆绑更多使用 native 代码的 Lua 扩展 - 而不仅仅是纯 .lua 文件。具体来说,我想捆绑
我需要一些帮助来解析 lua 文件的命令行。我正在执行一个 lua 文件,该 lua 文件有一个命令“dofile(2nd.lua-file)”,但是,我想通过第一个 lua 文件将一些参数传递给第二
这是我的代码示例: listOfPeople = {} listOfPeople["test"] = "hello" listOfPeople = nil “hello”字符串是否丢失并形成内存泄漏?
在一些源代码中,我看到了“Underscore.lua”模块的用法。 _ = require 'underscore' 描述如下: Underscore.lua is a Lua library th
在一些源代码中,我看到了“Underscore.lua”模块的用法。 _ = require 'underscore' 描述如下: Underscore.lua is a Lua library th
我一直在编程 io.write("How many languages do you speak?\n") answer = io.read() if (answer == 1) then io.wr
这个问题在这里已经有了答案: Getting multiple values from a function without creating a variables in LUA (2 个答案)
在阅读 Lua manual 时我遇到了这部分: 函数调用和赋值都可以以左括号开头。这种可能性导致了 Lua 语法中的歧义。考虑以下片段: a = b + c (print or io.write)(
假设我有以下循环: for name in poll() do if name == "quit" then return 0 end end "quit" 字符串是否
Pandoc 通过其 --lua-filter 参数原生支持 lua 过滤器。 但是,我想知道它使用的是什么版本的 lua,以及是否可以将 lua 模块(我相信它依赖于 C 代码)导入其中。 这是我调
这种语言是面向对象的语言吗? 它经常用作OO语言吗? 最佳答案 Lua 完全有能力 prototype-based类似于 JavaScript 的面向对象编程。 Prototype-based pro
我想从 C++ 传递一个 Lua 脚本(Lua 解释器可以处理的代码)并取回结果。 我在网上查看,但找不到任何可以帮助我的示例。我可以从 C++ 调用 Lua 函数,但这需要您使用 Lua 函数创建一
我正在阅读“在 Lua 中编程”,但我不明白这段代码中 Lua 中函数的行为: function newCounter () local i = 0 return function () --
我最近一直在查找 Lua 中的链表,并有一个简单的问题,到目前为止我还没有找到答案 local head = nil head = {next = head, value = "d"} head =
我知道有tonumber()函数,但是问题是我需要转换较大的数字,例如1000100110100011111010101001001001001100100101。我可以自己写,但是有没有办法将其集成
是否可以在 Lua 中对多个值执行算术运算。 我在 Windows 5.1.4 上使用 Lua。 目前我必须将多个值放入一个表中,然后解压缩它们,我希望能够跳过这一步。 是否可以。 这是我目前拥有的:
有什么区别吗 local splitPathFileExtension = function (res) end 和 function splitPathFileExtension(res) end
在下面的代码中,谁能解释一下 b,a = a,b 内部是如何工作的? -- Variable definition: local a, b -- Initialization a = 10 b = 3
我是一名优秀的程序员,十分优秀!