nim_duilib/examples/proto_debuger/lua_wraper.h
2021-11-21 13:47:19 +08:00

117 lines
2.6 KiB
C++

#ifndef LUA_WRAPER_H
#define LUA_WRAPER_H
#include <string.h>
#include <iostream>
#include<typeinfo>
#include <iostream>
#include <map>
extern "C" {
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
class LuaDelegate {
public:
LuaDelegate();
LuaDelegate(std::map<std::string, lua_CFunction>);
int DoFile(std::string);
void Stop();
int DoString(std::string);
int UpdateScript(std::string);
template <typename T>
void pushstack(T arg1);
void pushstack(lua_Number arg) {
lua_pushnumber(mVM, arg);
}
void pushstack(lua_Integer arg) {
lua_pushinteger(mVM, arg);
}
void pushstack(void* arg) {
lua_pushlightuserdata(mVM, arg);
}
void pushstack(const char* arg) {
lua_pushstring(mVM,arg);
}
void pushstack(bool arg) {
lua_pushboolean (mVM, arg);
}
void pushstack(std::string arg) {
lua_pushstring(mVM, arg.c_str());
}
void pushstack(lua_CFunction fn, int n) {
lua_pushcclosure(mVM, fn, n);
}
template <typename T, typename ... Types>
void pushstack(T arg1, Types... rest);
template <typename ... Types>
void pushstack(lua_Number arg1, Types... rest) {
lua_pushnumber(mVM, arg1);
pushstack(rest...);
}
template <typename ... Types>
void pushstack(lua_Integer arg1, Types... rest) {
lua_pushinteger (mVM, arg1);
pushstack(rest...);
}
template <typename ... Types>
void pushstack(bool arg1, Types... rest) {
lua_pushboolean(mVM, arg1);
pushstack(rest...);
}
template <typename ... Types>
void pushstack(std::string arg1, Types... rest) {
lua_pushstring(mVM, arg1.c_str());
pushstack(rest...);
}
template <typename ... Types>
void pushstack(void* arg1, Types... rest) {
lua_pushlightuserdata(mVM, arg1);
pushstack(rest...);
}
template <typename ... Types>
void pushstack(lua_CFunction fn, int n, Types... rest) {
lua_pushcclosure(mVM, fn,n);
pushstack(rest...);
}
template <typename ... Types>
void pushstack(const char* arg1, Types... rest) {
lua_pushstring(mVM, arg1);
pushstack(rest...);
}
template<typename... T>
void CallFuntion(std::string name,T... para){
int i = lua_getglobal(mVM,name.c_str());
if(i < 0){
return ;
}
pushstack(para...);
}
void PrintError(lua_State *L);
void OnSerialData(std::string);
void OnNetworkData(char*,char*,uint32_t port);
void OnNewTcpClient(std::string ip,uint32_t port,uint32_t sockptr);
void OnClientLeave(std::string ip,uint32_t port,uint32_t sockptr);
void DumpStack();
~LuaDelegate();
private:
lua_State *mVM;
std::string mFile;
std::map<std::string, lua_CFunction> mFunc;
};
#endif