From 8d8c4ff1664625e7428d0d31cd798d9321680cb2 Mon Sep 17 00:00:00 2001 From: chai Date: Thu, 14 Mar 2019 09:08:07 +0800 Subject: *luax --- Source/3rdParty/Luax/luax_class.hpp | 150 ++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 Source/3rdParty/Luax/luax_class.hpp (limited to 'Source/3rdParty/Luax/luax_class.hpp') diff --git a/Source/3rdParty/Luax/luax_class.hpp b/Source/3rdParty/Luax/luax_class.hpp new file mode 100644 index 0000000..6ce8d19 --- /dev/null +++ b/Source/3rdParty/Luax/luax_class.hpp @@ -0,0 +1,150 @@ +#ifndef __LUAX_CLASS_H__ +#define __LUAX_CLASS_H__ + +#include + +#include "luax_config.h" +#include "luax_ref.h" + +namespace Luax +{ + +#define LUAX_DECL_METHOD(mtd) static int mtd(lua_State* L) + + /// + /// 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管理的实例要确保引用计数的正确性,在多个线程中需要确定不会误释放。 + /// + template + class LuaxClass + { + public: + + void Retain(); + void Release(); + + protected: + + LuaxClass(); + virtual ~LuaxClass(); + + /// + /// 将userdata push到栈顶,如果没有初始化mUserdata,初始化设置好元表并把初始化好的userdata留在栈顶。并添加一个引用。 + /// + bool PushLuaUserdata(LuaxState& state); + + //------------------------------------------------------------------------------------------------------------ + + /// + /// 对类的reftable的管理 + /// + void Ref(); + + private: + + friend class LuaxState; + + static void RegisterLuaxClass(LuaxState& state); + static void RegisterLuaxFactoryClass(LuaxState& state); + static void RegisterLuaxSingletonClass(LuaxState& state); + static void RegisterLuaxInterface(LuaxState& state); + + static void SetInterfaceTableRef(LuaxState& state, int idx); + static void SetClassTableRef(LuaxState& state, int idx); + + static void PushInterfaceTable(LuaxState& state); + static void PushClassTable(LuaxState& state); + static void PushRefTable(LuaxState& state); + + /// + /// 屏蔽取地址运算符,如果需要地址,只能通过在堆上创建实例得到。在栈上和静态区的变量不能取地址。保证引用计数的准确。如 + /// 果需要穿引用,使用引用传递而不是传递地址。 + /// + void* operator &(); + + /// + /// 创建userdata,绑定实例到state,如果本类是工厂 + /// + void BindToLua(LuaxState& state); + + //------------------------------------------------------------------------------------------------------------ + + /// + /// 所有LuaxClass类型的实例共享的内容 + /// + static LuaxStrongRef mInterfaceTable; // interface table + static LuaxStrongRef mClassTable; // class table + static LuaxStrongRef mRefTable; // 单例 + + /// + /// 通过userdata可以拿到ref table\member table\interface table + /// + LuaxWeakRef mUserdata; + + /// + /// 引用计数,用来处理线程间共享 + /// + int mRC; + + /// + /// 确保析构函数只能通过Release调用的safer,这样只要继承了LuaxClass的类,如果使用delete直接析构,就会报错 + /// + bool mSafer; + + public: + + //------------------------------------------------------------------------------------------------------------ + // 公共内容 + + LUAX_DECL_METHOD( l_GetClassName ); + LUAX_DECL_METHOD( l_GetInterfaceTable ); + LUAX_DECL_METHOD(l_ToString); + + //------------------------------------------------------------------------------------------------------------ + // 工厂类相关 + + LUAX_DECL_METHOD( l_ExtendFactory ); + LUAX_DECL_METHOD( l_GC ); + + //------------------------------------------------------------------------------------------------------------ + // 单例类相关 + + LUAX_DECL_METHOD( l_ExtendSingleton ); + + }; + + /// + /// 在成员方法里创建LuaxState并对参数进行检查。 + /// +#define LUAX_SETUP(L, params) \ + LuaxRuntime& runtime = LuaxRuntime::Get(); \ + LuaxState& state = runtime[L].state; \ + if(!state.CheckParams(1, params)) return 0 + +#define LUAX_STATE(L) \ + LuaxState& state = LuaxRuntime::Get().GetLuaxState(L) + +} + +#endif \ No newline at end of file -- cgit v1.1-26-g67d0