LuaYard/LuaVM.h

68 lines
1.2 KiB
C
Raw Normal View History

2019-08-06 20:36:04 +08:00
#pragma once
2019-08-10 21:34:36 +08:00
#include "lua/lua.h"
#include "lua/lualib.h"
#include "lua/lauxlib.h"
#include <string>
2019-08-06 20:36:04 +08:00
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);