c++ - Lua Wrapper 类 - 通过 DLL 向 Lua 公开 C++ 静态方法

标签 c++ dll methods lua

我终于遇到了一个问题,我在这里找不到解决方案。我正在使用在这里找到的 Lua 包装器类 http://lua-users.org/wiki/CppConvenientLuaWrapperClass .我们已经能够公开完整的 API 以及更多其他功能,如串行通信等。

这个 Lua Wrapper 背后的概念是你在编译之前公开每个方法,所以当你运行你的程序时,所有方法都将被添加到 Lua Stack 中,这样你就可以执行它们。现在的想法是构建一个 Dll 来完成这个暴露方法的过程。这样您就不需要发布一个包含所有公开方法的版本,而是通过多个 dll 文件加载它们。

我尝试创建另一个表并在该表中注册其他方法,但是这样一来,之前公开的方法就停止工作了。

我能想到的另一种方法是创建一个包含所有所需方法的 C 语言的 dll,然后将其直接加载到 Lua。但我认为另一种方式会更好。

你做过类似的事情吗?我有什么错误的概念吗?

谢谢

嗯...我真的不想在这个时候改变我们的包装。我想我可以设法做到这一点。我没有为插件函数添加一个新表,而是添加了一个新的子表,它将包含要从 Lua 调用的函数名称和 cClosures。 所以最后我们应该有:

application.functionName()
application.plugin.functionName()

即使它以这种方式工作,它也能正常工作。 现在我想知道在公开要添加到 application[plugin][pluginFunction] 而不是 aplication[pluginFunction] 的函数时我们如何引用 lua_settable?! 这是公开功能的方式:

//mState is a pointer to a Lua_State
lua_pushstring( mState, functionName );

//methodDesc is a pointer to an object that describes the function arguments/returns 
lua_pushlightuserdata( mState, methodDesc );

//exposeMethodProxy is the method that is responsible for conneting lua c-calls to the c-functions
lua_pushcclosure( mState, exposedMethodProxy, 1 );

//mMethodTableIndex is a member variable that contains the index of the table tha hold all exposed functions
lua_settable( mState, mMethodTableIndex );

关于如何实现不将闭包添加到主表(在 mMethodTableIndex 处)作为 mainTable[functionName] 而在 maintable[plugin][functionNane] 处添加闭包的任何想法?

最佳答案

我不确定,你很清楚你想做什么。扩展 lua 的典型方法是使用单一方法编写 DLL,该方法使用 Lua API 来注册您的 C++ 类型和 C 函数。为了方便地绑定(bind) C++ 函数和类,您可以使用 LuaBridge .此类绑定(bind)的示例如下:https://github.com/d-led/xerceslua

xerceslua 模块的 DLL 头文件只包含一个函数:

#include <lua.hpp>
void register_xerceslua (lua_State* L);

在实现内部,LuaBridge 用于绑定(bind)到 C++:

#include "xerceslua_lib.h"

#include <lua.hpp>
#include <LuaBridge.h>

void register_xerceslua (lua_State* L) {
...
luabridge::getGlobalNamespace(L)
    .beginNamespace("xerces")
    .addVariable("version",&version,false)
...

然后在 Lua 中您可以访问公开的 C++ API:

assert(require 'xerceslua')

local parser=xerces.XercesDOMParser()
parser:loadGrammar("Employee.dtd",xerces.GrammarType.DTDGrammarType)

您可以将 Lua 用作嵌入式脚本语言,您可以在其中执行 lua from within your software ,或者您可以将其用作可扩展的脚本语言,使用上面显示的方法对其进行扩展。两者都有效,但您必须考虑,您到底想做什么。

关于c++ - Lua Wrapper 类 - 通过 DLL 向 Lua 公开 C++ 静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14854555/

相关文章:

c++ - 在 Visual Studio 中使用命令行参数进行调试

c++ - 添加新行后未获取数据表行

c++ - 这个按升序打印按行和按列排序的矩阵的程序的零在哪里?

c - 在 win32/C/C++ 中共享内存和 IPC 的最快进程技术

c++ - 在 Linux 中获取主板 deviceID 和 BIOS 序列号

javascript - 如何阻止函数被视为 javascript 对象中的属性?

ios - 数据被检索为project.model而不是swift ios中的元素

php - Woocommerce 临时购物车税未根据第一个请求正确计算

c++ - 将 BSTR 转换为 char * 时出现问题

c++ - 是否可以在 C++ 中调用导出的 "private"方法