From 70b82d1981c0de3c7b77670ff8abcfeb26815142 Mon Sep 17 00:00:00 2001 From: chai Date: Tue, 12 Mar 2019 00:39:26 +0800 Subject: *misc --- Source/3rdParty/Luax/luax_class.h | 124 ++++++++++++++++++++++++++++---------- 1 file changed, 92 insertions(+), 32 deletions(-) (limited to 'Source/3rdParty/Luax/luax_class.h') diff --git a/Source/3rdParty/Luax/luax_class.h b/Source/3rdParty/Luax/luax_class.h index 1721ec4..d241030 100644 --- a/Source/3rdParty/Luax/luax_class.h +++ b/Source/3rdParty/Luax/luax_class.h @@ -3,50 +3,110 @@ #include +#include "lua.hpp" +#include "luax_config.h" +#include "luax_state.h" + namespace Luax { -#define LUAX_DECL_METHOD(MTD) static int MTD(lua_State*) -#define LUAX_DECL_FACTORY(CLS) static int RegisterLuaClass(lua_State*); -#define LUAX_DECL_SINGLETON(CLS) +#define LUAX_DECL_METHOD(mtd) static int mtd(lua_State*) + + /// + /// RegisterLuaxClass 注册类的方法和成员,比如枚举、常量等到class table + /// LuaxRegisterInterface 注册接口方法到interface table + /// LuaxGetFactoryName 获得工厂的类名,同时用来避免注册时错误注册为了singleton,通过编译时报错避免 + /// +#define LUAX_DECL_FACTORY(type) \ + static void RegisterLuaxClass(LuaxState&);\ + static void RegisterLuaxInterface(LuaxState&);\ + static const char* GetLuaxFactoryName() { return #type; };\ + static const char* GetLuaxClassName() { return #type; };\ + static bool IsLuaxClassSingleton() { return false; }; + + + /// + /// RegisterLuaxClass 注册类的方法和成员,比如枚举、常量等到class table + /// LuaxGetSingletonName 获得单例的类名 + /// +#define LUAX_DECL_SINGLETON(type) \ + static void RegisterLuaxClass(LuaxState&); \ + static const char* GetLuaxSingletonName() { return #type; }; \ + static const char* GetLuaxClassName() { return #type; }; \ + static bool IsLuaxClassSingleton() { return true; }; + + + /// + /// 需要暴露给lua的class需要继承此类。通过lua管理的实例要确保引用计数的正确性,在多个线程中需要确定不会误释放。 + /// + class LuaxClass + { + public: + + void Retain(); + void Release(); + + //------------------------------------------------------------------------------------------------------------ + // 公共内容 + + template + static void RegisterLuaxClass(LuaxState& state); + + template + LUAX_DECL_METHOD(l_GetClassName); + + //------------------------------------------------------------------------------------------------------------ + // 工厂类相关 + + template + static void RegisterLuaxFactoryClass(LuaxState& state); + + template + static void RegisterLuaxInterface(LuaxState& state); + + LUAX_DECL_METHOD(l_GC); + + //------------------------------------------------------------------------------------------------------------ + // 单例类相关 + + template + static void RegisterLuaxSingletonClass(LuaxState& state); + + protected: + + LuaxClass(); + ~LuaxClass(); -#define LUAX_REGISTER_CLASS(CLS) CLS::RegisterLuaType() - /* - /// - /// 需要暴露给lua的class需要继承此类。 - /// - class LuaxClass - { - public: + /// + /// 将userdata push到栈顶,如果没有初始化mUserdata,初始化设置好元表并把初始化好的userdata留在栈顶。 + /// + bool PushLuaUserdata(LuaxState& state); - static void RegisterLuaType(); + private: - static void RegisterLuaFuncs(); + /// + /// 屏蔽取地址运算符,如果需要地址,只能通过在堆上创建实例得到。在栈上和静态区的变量不能取地址。保证引用计数的准确。如 + /// 果需要穿引用,使用引用传递而不是传递地址。 + /// + void* operator &(); - private: - - }; + void BindToLua(LuaxState& state); - /// - /// 需要作为工厂创建userdata的类继承此类。成为工厂的类,通过LUAX_DECL_FACTORY(CLS)宏指定类名。 - /// - class LuaxFactory : public LuaxClass - { - + int mRefCount; - }; + void* mUserdata; + + }; - /// - /// 注入给lua的单例类继承此类。 - /// - class LuaxSingleton : public LuaxClass - { - public: + /// + /// 在成员方法里创建LuaxState并对参数进行检查。 + /// +#define LUAX_SETUP(L, params) \ + LuaxState state(L);\ + if(!state.CheckParams(1, params)) return 0; - private: +#include "luax_class.inl" - }; - */ } #endif \ No newline at end of file -- cgit v1.1-26-g67d0