LuaYard/LuaVM.h
Kiritow bc189b4951 完整重构
分离各SDL类的包装代码.
事件分发处理全部转移至Lua Init层. C层只提供获取事件的方法.
这样有助于提高性能,以及今后Coroutine scheduler的添加.
2019-08-25 00:09:28 +08:00

68 lines
1.2 KiB
C++

#pragma once
#include "lua/lua.h"
#include "lua/lualib.h"
#include "lua/lauxlib.h"
#include <string>
class LuaVM
{
public:
LuaVM() {
_L = luaL_newstate();
luaL_openlibs(_L);
_managed = true;
}
LuaVM(lua_State* L, bool managed=false) {
_L = L;
_managed = managed;
}
/// NonMoveable,NonCopyable
LuaVM(const LuaVM&) = delete;
LuaVM& operator = (const LuaVM&) = delete;
LuaVM(LuaVM&&) = delete;
LuaVM& operator = (LuaVM&&) = delete;
~LuaVM() {
if (_managed) {
lua_close(_L);
}
}
operator lua_State* () {
return _L;
}
private:
lua_State* _L;
bool _managed;
};
template<typename T>
T& getuserdata(lua_State* L, int index) // LUA_TUSERDATA
{
return *(T*)luaL_checkudata(L, index, T::tname);
}
template<typename T>
T getvalue(lua_State* L, int index);
template<typename T>
T& pushvalue(lua_State* L)
{
return *(T*)lua_newuserdata(L, sizeof(T));
}
void pushvalue(lua_State* L, int val);
void pushvalue(lua_State* L, double val);
void pushvalue(lua_State* L, const char* str);
void pushvalue(lua_State* L, const char* str, int len);
void pushvalue(lua_State* L, const std::string& str);
void pushvalue(lua_State* L, lua_CFunction fn);
void pushvalue(lua_State* L, lua_CFunction fn, int nup);