diff options
Diffstat (limited to 'Source/Samples')
-rw-r--r-- | Source/Samples/LuaxTest/header.h | 60 | ||||
-rw-r--r-- | Source/Samples/LuaxTest/main.cpp | 190 |
2 files changed, 250 insertions, 0 deletions
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 T> +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<T*>(this); + }; + + virtual ~Singleton() {}; + + static T* instance; + +private: + + Singleton(const Singleton& singleton); + + Singleton& operator = (const Singleton& singleton); + +}; + +template<class T> +T* Singleton<T>::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 <iostream> +#include <string> + +using namespace std; +using namespace Luax; + +//---------------------------------------------------------------------------------------------------------------- + +class School : Singleton<School> +{ +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<float>(-1, "version", 0); + + state.RegisterSingleton<School>(); + state.RegisterFactory<Boy>(); + + state.PopNamespace(); // Asura + state.DoString(script); + + lua_close(L); + + getchar(); +}
\ No newline at end of file |