From 70b82d1981c0de3c7b77670ff8abcfeb26815142 Mon Sep 17 00:00:00 2001 From: chai Date: Tue, 12 Mar 2019 00:39:26 +0800 Subject: *misc --- Source/Samples/LuaxTest/header.h | 60 +++++++++++++ Source/Samples/LuaxTest/main.cpp | 190 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 250 insertions(+) create mode 100644 Source/Samples/LuaxTest/header.h create mode 100644 Source/Samples/LuaxTest/main.cpp (limited to 'Source/Samples') diff --git a/Source/Samples/LuaxTest/header.h b/Source/Samples/LuaxTest/header.h new file mode 100644 index 0000000..386ae71 --- /dev/null +++ b/Source/Samples/LuaxTest/header.h @@ -0,0 +1,60 @@ + +#ifndef ASSERT +#ifdef NDEBUG +#define ASSERT(x) { false ? (void)(x) : (void)0; } +#else +#ifdef _WIN32 +#define ASURA_DEBUG_BREAK() __debugbreak() +#else +#define ASURA_DEBUG_BREAK() raise(SIGTRAP) +#endif +#define ASSERT(x) do { const volatile bool asura_assert_b____ = !(x); if(asura_assert_b____) ASURA_DEBUG_BREAK(); } while (false) +#endif +#endif + +/// +/// 继承Singleton的类在第一次实例化时保存实例,之后如果再次实例化会报错。 +/// +template +class Singleton +{ +public: + + static T* Get() + { + // 如果之前没有创建,在这里立即创建一个并保存 + if (!instance) instance = new T; + // 返回实例 + return instance; + } + + static void Destroy() + { + delete instance; + instance = nullptr; + } + +protected: + + Singleton() + { + // 如果有instance,意味着又创建了一个实例,这是错误的。 + ASSERT(!instance); + // 否则,将本实体作为实例 + instance = static_cast(this); + }; + + virtual ~Singleton() {}; + + static T* instance; + +private: + + Singleton(const Singleton& singleton); + + Singleton& operator = (const Singleton& singleton); + +}; + +template +T* Singleton::instance = nullptr; diff --git a/Source/Samples/LuaxTest/main.cpp b/Source/Samples/LuaxTest/main.cpp new file mode 100644 index 0000000..828b427 --- /dev/null +++ b/Source/Samples/LuaxTest/main.cpp @@ -0,0 +1,190 @@ +/// +/// Scripting with Lua. +/// +extern "C"{ +#include "Lua51/lua.h" +#include "Lua51/lauxlib.h" +} +#include "Luax/luax.h" +#include "header.h" + +#include +#include + +using namespace std; +using namespace Luax; + +//---------------------------------------------------------------------------------------------------------------- + +class School : Singleton +{ +public: + + School() : mName("Koko") {} + +private: + + const char* mName; + +public: + + LUAX_DECL_SINGLETON(School); + + LUAX_DECL_METHOD(l_GetName); + +}; + +int School::l_GetName(lua_State* L) +{ + LuaxState state(L); + + School* school = Get(); + + state.Push(school->mName); + return 1; +} + +void School::RegisterLuaxClass(LuaxState& state) +{ + state.SetField(-1, "Region", "Block 1"); // 101班 + + luaL_Reg regTable[] = { + { "GetName", l_GetName }, + { NULL, NULL } + }; + + state.Register(regTable); + +} + +//---------------------------------------------------------------------------------------------------------------- + +class Boy : public LuaxClass +{ +public: + + Boy(int age) : mAge(age) {} + +private: + + int mAge; + +public: + + LUAX_DECL_FACTORY(SimBoy); + + // member methods + LUAX_DECL_METHOD(l_New); + LUAX_DECL_METHOD(l_GetAge); + + // class method + LUAX_DECL_METHOD(l_GetGender); + +}; + +int Boy::l_New(lua_State* L) +{ + LuaxState state(L); + + int age = state.GetValue(1, 0); + + Boy* boy = new Boy(age); + boy->PushLuaUserdata(state); + + return 1; +} + +int Boy::l_GetAge(lua_State* L) +{ + LUAX_SETUP(L, "U"); + + return 1; +} + +int Boy::l_GetGender(lua_State* L) +{ + LUAX_SETUP(L, "*"); + state.Push("male student!"); + return 1; +} + +void Boy::RegisterLuaxClass(LuaxState& state) +{ + state.SetField(-1, "Class", 101); // 101班 + state.SetField(-1, "Gender", "Male"); // 101班 + + luaL_Reg regTable[] = { + {"GetGender", l_GetGender}, + {NULL, NULL} + }; + + state.Register(regTable); +} + +void Boy::RegisterLuaxInterface(LuaxState& state) +{ + luaL_Reg regTable[] = { + {"New", l_New}, + {NULL, NULL} + }; + + state.Register(regTable); +} + +//---------------------------------------------------------------------------------------------------------------- + +string script = R"( +function main() + local a = 19 + print(Asura.version) + print(Asura.author.name) + print("ok") + print(Asura.author.func()) +-- local boy = Asura.SimBoy.New("I am peter!", 19) +-- boy:Say() +-- print(Asura.SimSchool.GetName()) + print(Asura.SimBoy.Class) + print(Asura.SimBoy.Gender) + print(Asura.SimBoy.GetGender()) + print(Asura.SimBoy.GetClassName()) + print(Asura.School.GetName()) + print(Asura.School.Region) +end +function err(msg) + print(msg) +end +xpcall(main, err) +)"; + +//---------------------------------------------------------------------------------------------------------------- + +int func(lua_State* L) +{ + LUAX_SETUP(L, "*"); + state.Push("func ok"); + return 1; +} + +int main() +{ + lua_State* L = lua_open(); + Luax::LuaxState state(L); + state.OpenLibs(); + state.PushNamespace("Asura"); + state.PushNamespace("author"); + state.SetField(-1, "name", "chai"); + state.SetField(-1, "func", func); + state.PopNamespace(); // Asura.author + state.SetField(-1, "version", 100); + cout << state.GetField(-1, "version", 0); + + state.RegisterSingleton(); + state.RegisterFactory(); + + state.PopNamespace(); // Asura + state.DoString(script); + + lua_close(L); + + getchar(); +} \ No newline at end of file -- cgit v1.1-26-g67d0