ewo*_*wok 7 c++ lua event-handling
我有一个用C++实现的基本事件处理程序.我的应用程序中还有一个嵌入式Lua解释器,我需要与事件管理器进行交互.最终目标是能够拥有一个事件处理程序,它将在触发事件时执行c ++和Lua函数.
我的问题是我无法想出一种简单的方法来存储我的C++代码中的lua函数的引用.我知道如何从c(使用lua_getglobal和lua_pcall)执行Lua函数,但我更喜欢存储对函数本身的引用,以便我可以直接将Lua函数传递给registerListener
注意可以假设userdata NULL适用于所有Lua Listener.
这是我的代码:
EventManager.h
#include <string>
#include <map>
#include <vector>
using namespace std;
typedef void (*fptr)(const void* userdata, va_list args);
typedef pair<fptr, void*> Listener;
typedef map<string, vector<Listener> > CallbackMap;
class EventManager {
private:
friend ostream& operator<<(ostream& out, const EventManager& r);
CallbackMap callbacks;
static EventManager* emInstance;
EventManager() {
callbacks = CallbackMap();
}
~EventManager() {
}
public:
static EventManager* Instance();
bool RegisterEvent(string const& name);
void RegisterListener(string const &event_name, fptr callback,
void* userdata);
bool FireEvent(string name, ...);
};
inline ostream& operator<<(ostream& out, const EventManager& em) {
return out << "EventManager: " << em.callbacks.size() << " registered event"
<< (em.callbacks.size() == 1 ? "" : "s");
}
Run Code Online (Sandbox Code Playgroud)
EventManager.cpp
#include <cstdarg>
#include <iostream>
#include <string>
#include "EventManager.h"
using namespace std;
EventManager* EventManager::emInstance = NULL;
EventManager* EventManager::Instance() {
if (!emInstance) {
emInstance = new EventManager;
}
return emInstance;
}
bool EventManager::RegisterEvent(string const& name) {
if (!callbacks.count(name)) {
callbacks[name] = vector<Listener>();
return true;
}
return false;
}
void EventManager::RegisterListener(string const &event_name, fptr callback,
void* userdata) {
RegisterEvent(event_name);
callbacks[event_name].push_back(Listener(callback, userdata));
}
bool EventManager::FireEvent(string name, ...) {
map<string, vector<Listener> >::iterator event_callbacks =
callbacks.find(name);
if (event_callbacks == callbacks.end()) {
return false;
}
for (vector<Listener>::iterator cb =
event_callbacks->second.begin();
cb != event_callbacks->second.end(); ++cb) {
va_list args;
va_start(args, NULL);
(*cb->first)(cb->second, args);
va_end(args);
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
luaw_eventmanager.h
#pragma once
#ifndef LUAW_EVENT_H
#define LUAW_EVENT_H
#include "EventManager.h"
extern "C" {
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
void luaw_eventmanager_push(lua_State* L, EventManager* em);
int luaopen_weventmanager(lua_State* L);
}
#endif
Run Code Online (Sandbox Code Playgroud)
luaw_eventmanager.cpp
#include <assert.h>
#include <stdio.h>
#include <sstream>
#include <iostream>
#include "luaw_eventmanager.h"
using namespace std;
static int
luaw_eventmanager_registerevent(lua_State* L)
{
int nargs = lua_gettop(L);
if (nargs != 2) {
return 0;
}
stringstream ss;
ss << luaL_checkstring(L, 2);
EventManager::Instance()->RegisterEvent(ss.str());
return 1;
}
static int
luaw_eventmanager_registerlistener(lua_State* L)
{
return 1;
}
static int
luaw_eventmanager_fireevent(lua_State* L)
{
return 1;
}
static int
luaw_eventmanager_tostring(lua_State* L)
{
stringstream ss;
ss << *EventManager::Instance();
lua_pushstring(L, &ss.str()[0]);
return 1;
}
static const struct luaL_Reg luaw_eventmanager_m [] = {
{"registerEvent", luaw_eventmanager_registerevent},
{"registerListener", luaw_eventmanager_registerlistener},
{"fireEvent", luaw_eventmanager_fireevent},
{"__tostring", luaw_eventmanager_tostring},
{NULL, NULL}
};
void
luaw_eventmanager_push(lua_State* L, EventManager* em)
{
EventManager** emUserdata = (EventManager**)lua_newuserdata(L, sizeof(EventManager*));
*emUserdata = em;
luaL_getmetatable(L, "WEAVE.mEventManager");
lua_setmetatable(L, -2);
}
int
luaopen_weventmanager(lua_State* L)
{
luaL_newmetatable(L, "WEAVE.mEventManager");
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
luaL_register(L, NULL, luaw_eventmanager_m);
assert(!lua_isnil(L, -1));
return 1;
}
Run Code Online (Sandbox Code Playgroud)
Nic*_*las 16
所有Lua拥有的对象都是垃圾收集的.这包括功能.因此,即使您可以获得对Lua函数的引用,Lua仍然会拥有它,因此每当Lua检测到它不再被引用时它将受GC控制.
外部代码不能拥有Lua引用.但外部代码可以将该引用存储在Lua代码无法访问的位置(因此无法破解):Lua注册表.
Lua注册表是一个Lua表(它位于堆栈伪索引LUA_REGISTRYINDEX,因此可以从堆栈访问)Lua代码无法(直接)访问.因此,它是一个安全的地方,您可以存储您需要的任何东西.由于它是一个Lua表,你可以像任何其他Lua表一样操作它(添加值,键等).
但是,注册表是全局的,如果你使用其他C模块,它们完全有可能开始踩到彼此的东西.因此,为每个模块选择一个特定的注册表项并在该注册表项中构建一个表是个好主意.
第一步:初始化C接口代码时,创建一个表并将其粘贴到注册表中的已知密钥中.只是一张空桌子.
当Lua代码传递一个Lua函数用作回调时,从特殊键加载该表并在那里粘贴Lua函数.当然,要做到这一点,您需要为每个注册函数提供一个唯一的键(您将其存储为Lua函数的void*数据),稍后您可以使用它来检索该函数.
Lua有一个简单的机制来做到这一点:luaL_ref.此函数将使用给定的表在堆栈顶部注册对象.保证此注册过程为每个注册对象返回唯一的整数键(只要您不手动修改系统后面的表).luaL_unref发布参考资料,allo
由于引用是整数键,因此您可以从中执行int转换void*并将其作为数据.我可能会使用一个显式对象(mallocing int),但你可以随意存储它.
第二步:注册Lua函数时,使用luaL_ref将其添加到步骤1中创建的注册表中.将此函数返回的密钥存储在void*已注册函数的参数中.
第三步:当需要调用该函数时,使用存储在void*参数中的整数键来访问在步骤1中创建的注册表.这将为您提供函数,然后可以使用常用的Lua方法调用该函数.
第四步:当您取消注册Lua函数时,使用luaL_unref释放函数(这样可以避免泄漏Lua的内存).如果您使用malloc内存存储整数键,free则在此处.
我建议你的功能存储到注册表,并使用该功能提供的参考机制luaL_ref和luaL_unref.
这些函数使用C int值来访问值.例如,很容易将这样的整数值存储在C++类成员中.
@Nicolas Bolas 提供了很好的说明,但对于新手(包括我自己)来说太模糊了。通过反复试验,我想出了可行的示例:
lua_newtable(L); // create table for functions
int tab_idx = luaL_ref(L,LUA_REGISTRYINDEX); // store said table in pseudo-registry
lua_rawgeti(L,LUA_REGISTRYINDEX,tab_idx); // retrieve table for functions
lua_getglobal(L, "f"); // retrieve function named "f" to store
int t = luaL_ref(L,-2); // store a function in the function table
// table is two places up the current stack counter
lua_pop(L,1); // we are done with the function table, so pop it
Run Code Online (Sandbox Code Playgroud)
lua_rawgeti(L,LUA_REGISTRYINDEX,tab_idx); // retrieve function table
lua_rawgeti(L,-1,t); // retreive function
//use function
//don't forget to pop returned value and function table from the stack
lua_pop(L,2);
Run Code Online (Sandbox Code Playgroud)