template<class t> T MethodName();
template<class T> T MethodName();
int main() { return 0; }
#include "Script.h"
#ifndef _SCRIPT_H_ #define _SCRIPT_H_ #endif
#pragma comment(lib,"lua.lib") extern "C" { #include <lua.h> #include <lualib.h> #include <lauxlib.h> }
#include <stdio.h> #include <iostream> #include <sstream> using namespace std;
class Script {
private: lua_State *lua_state;
public: void Create();
void Script::Create() { lua_state = luaL_newstate(); static const luaL_Reg lualibs[] = { {"base", luaopen_base}, {"io", luaopen_io}, {NULL, NULL} }; for(const luaL_Reg *lib = lualibs; lib->func != NULL; lib++) { luaL_requiref(lua_state, lib->name, lib->func, 1); lua_settop(lua_state, 0); } }
void Close();
void Script::Close() { lua_close(lua_state); }
int DoFile(char* ScriptFileName);
int Script::DoFile(char* ScriptFileName) { luaL_dofile(lua_state,ScriptFileName); return lua_tointeger(lua_state, lua_gettop(lua_state)); }
template<class t> void RegisterConstant(T value, char* constantname);
RegisterConstant<int>(13,"goodvalue");
template<> void Script::RegisterConstant<int>(int value, char* constantname) { lua_pushinteger(lua_state, value); lua_setglobal(lua_state,constantname); } template<> void Script::RegisterConstant<double>(double value, char* constantname) { lua_pushnumber(lua_state, value); lua_setglobal(lua_state,constantname); } template<> void Script::RegisterConstant<char>(char* value, char* constantname) { lua_pushstring(lua_state, value); lua_setglobal(lua_state,constantname); } template<> void Script::RegisterConstant<bool>(bool value, char* constantname) { lua_pushboolean(lua_state, value); lua_setglobal(lua_state,constantname); } template<> void Script::RegisterConstant<lua_cfunction>(lua_CFunction value, char* constantname) { lua_pushcfunction(lua_state, value); lua_setglobal(lua_state,constantname); }
int Foo(lua_State*) { // ... return n; }
a, b = Foo()
void Array();
void Script::Array() { lua_createtable(lua_state, 2, 0); }
template<class t> void RegisterConstantArray(T value, int index);
template void Script::RegisterConstantArray<int>(int value, int index) { lua_pushnumber(lua_state, index); lua_pushinteger(lua_state, value); lua_settable(lua_state, -3); } template void Script::RegisterConstantArray<double>(double value, int index) { lua_pushnumber(lua_state, index); lua_pushnumber(lua_state, value); lua_settable(lua_state, -3); } template void Script::RegisterConstantArray<char>(char* value, int index) { lua_pushnumber(lua_state, index); lua_pushstring(lua_state, value); lua_settable(lua_state, -3); } template void Script::RegisterConstantArray<bool>(bool value, int index) { lua_pushnumber(lua_state, index); lua_pushboolean(lua_state, value); lua_settable(lua_state, -3); } template void Script::RegisterConstantArray<lua_cfunction>(lua_CFunction value, int index) { lua_pushnumber(lua_state, index); lua_pushcfunction(lua_state, value); lua_settable(lua_state, -3); }
void RegisterArray(char* arrayname);
void Script::RegisterArray(char* arrayname) { lua_setglobal(lua_state, arrayname); }
int GetArgumentCount();
int Script::GetArgumentCount() { return lua_gettop(lua_state); }
template<class t> T GetArgument(int index);
template int Script::GetArgument<int>(int index) { return lua_tointeger(lua_state,index); } template double Script::GetArgument<double>(int index) { return lua_tonumber(lua_state,index); } template char* Script::GetArgument<char>(int index) { return (char*)lua_tostring(lua_state,index); } template bool Script::GetArgument<bool>(int index) { return lua_toboolean(lua_state,index); }
template<class t> void Return(T value);
template<> void Script::Return<int>(int value) { lua_pushinteger(lua_state,value); } template<> void Script::Return<double>(double value) { lua_pushnumber(lua_state,value); } template<> void Script::Return<char>(char* value) { lua_pushstring(lua_state,value); } template<> void Script::Return<bool>(bool value) { lua_pushboolean(lua_state,value); }
#include "Script.h" int main() { return 0; }
#include "Script.h" // _getch() #include <conio.h> // Script script; // Write int Write(lua_State*) { // for(int i = 1; i < script.GetArgumentCount()+1; i++) cout << script.GetArgument<char*>(i); // _getch(); return 0; } int main() { script.Create(); // , script.RegisterConstant<lua_cfunction>(Write,"Write"); script.DoFile("script.lua"); script.Close(); }
Write(1,2,3,4)
for i = 1, 4 do Write(i, "\n", "Hier kommt die Sonne", "\n") end
#include "Script.h" #include <conio.h> #include <Windows.h> #include <time.h> Script script; int Write(lua_State*) { // for(int i = 1; i < script.GetArgumentCount()+1; i++) cout << script.GetArgument<char*>(i); cout << "\n"; return 0; } int GetString(lua_State*) { // cin , Script char* str = ""; cin >> str; script.Return<char*>(str); // ! 1 -> return 1 return 1; } int Message(lua_State*) { // MessageBox Windows.h // , - :) char* msg = script.GetArgument<char*>(1); MessageBox(0,msg,"",MB_OK); return 0; } int GetTwoRandomNumbers(lua_State*) { // 1000 srand(time(NULL)); for(int i = 0; i < 2; i++) script.Return<int>(rand()%1000); // 2 return 2; } int GetLotOfRandomNumbers(lua_State*) { // 1000 srand(time(NULL)); for(int i = 0; i < script.GetArgument<int>(1); i++) script.Return<int>(rand()%1000); // , return script.GetArgument<int>(1); } int main() { script.Create(); script.RegisterConstant<lua_CFunction>(Write,"Write"); script.RegisterConstant<lua_CFunction>(GetString,"GetString"); script.RegisterConstant<lua_CFunction>(Message,"Message"); script.RegisterConstant<lua_CFunction>(GetTwoRandomNumbers,"Rand1"); script.RegisterConstant<lua_CFunction>(GetLotOfRandomNumbers,"Rand2"); script.Array(); script.RegisterConstantArray<int>(1,1); script.RegisterConstantArray<int>(2,2); script.RegisterConstantArray<int>(3,3); script.RegisterConstantArray<int>(4,4); script.RegisterArray("mass"); script.DoFile("script.lua"); script.Close(); // _getch(); }
for i = 1, 4 do Write(i, "\n", "Hier kommt die Sonne", "\n") end Write(2*100-1) Message("!") a, b = Rand1() Write(a, "\n", b, "\n") Write(Rand1(), "\n") a, b, c, d = Rand2(4) Write(a, "\n", b, "\n", c, "\n", d, "\n") return 1
#ifndef _SCRIPT_H_ #define _SCRIPT_H_ #pragma comment(lib,"lua.lib") extern "C" { #include <lua.h> #include <lualib.h> #include <lauxlib.h> } class Script { private: lua_State *lua_state; public: void Create(); void Close(); int DoFile(char* ScriptFileName); template<class t> void RegisterConstant(T value, char* constantname); void Array(); template<class t> void RegisterConstantArray(T value, int index); void RegisterArray(char* arrayname); int GetArgumentCount(); template<class t> T GetArgument(int index); template<class t> void Return(T value); }; #endif
#include "Script.h" void Script::Create() { lua_state = luaL_newstate(); static const luaL_Reg lualibs[] = { {"base", luaopen_base}, {"io", luaopen_io}, {NULL, NULL} }; for(const luaL_Reg *lib = lualibs; lib->func != NULL; lib++) { luaL_requiref(lua_state, lib->name, lib->func, 1); lua_settop(lua_state, 0); } } void Script::Close() { lua_close(lua_state); } int Script::DoFile(char* ScriptFileName) { luaL_dofile(lua_state,ScriptFileName); return lua_tointeger(lua_state, lua_gettop(lua_state)); } template<> void Script::RegisterConstant<int>(int value, char* constantname) { lua_pushinteger(lua_state, value); lua_setglobal(lua_state,constantname); } template<> void Script::RegisterConstant<double>(double value, char* constantname) { lua_pushnumber(lua_state, value); lua_setglobal(lua_state,constantname); } template<> void Script::RegisterConstant<char>(char* value, char* constantname) { lua_pushstring(lua_state, value); lua_setglobal(lua_state,constantname); } template<> void Script::RegisterConstant<bool>(bool value, char* constantname) { lua_pushboolean(lua_state, value); lua_setglobal(lua_state,constantname); } template<> void Script::RegisterConstant<lua_cfunction>(int(*value)(lua_State*), char* constantname) { lua_pushcfunction(lua_state, value); lua_setglobal(lua_state,constantname); } void Script::Array() { lua_createtable(lua_state, 2, 0); } template<> void Script::RegisterConstantArray<int>(int value, int index) { lua_pushnumber(lua_state, index); lua_pushinteger(lua_state, value); lua_settable(lua_state, -3); } template<> void Script::RegisterConstantArray<double>(double value, int index) { lua_pushnumber(lua_state, index); lua_pushnumber(lua_state, value); lua_settable(lua_state, -3); } template<> void Script::RegisterConstantArray<char>(char* value, int index) { lua_pushnumber(lua_state, index); lua_pushstring(lua_state, value); lua_settable(lua_state, -3); } template<> void Script::RegisterConstantArray<bool>(bool value, int index) { lua_pushnumber(lua_state, index); lua_pushboolean(lua_state, value); lua_settable(lua_state, -3); } template<> void Script::RegisterConstantArray<lua_cfunction>(lua_CFunction value, int index) { lua_pushnumber(lua_state, index); lua_pushcfunction(lua_state, value); lua_settable(lua_state, -3); } void Script::RegisterArray(char* arrayname) { lua_setglobal(lua_state, arrayname); } int Script::GetArgumentCount() { return lua_gettop(lua_state); } template<> int Script::GetArgument<int>(int index) { return lua_tointeger(lua_state,index); } template<> double Script::GetArgument<double>(int index) { return lua_tonumber(lua_state,index); } template<> char* Script::GetArgument<char>(int index) { return (char*)lua_tostring(lua_state,index); } template<> bool Script::GetArgument<bool>(int index) { return lua_toboolean(lua_state,index); } template<> void Script::Return<int>(int value) { lua_pushinteger(lua_state,value); } template<> void Script::Return<double>(double value) { lua_pushnumber(lua_state,value); } template<> void Script::Return<char>(char* value) { lua_pushstring(lua_state,value); } template<> void Script::Return<bool>(bool value) { lua_pushboolean(lua_state,value); }
Source: https://habr.com/ru/post/196272/
All Articles