summaryrefslogtreecommitdiff
path: root/Source/3rdParty/Luax/luax_class.hpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2019-03-14 09:08:07 +0800
committerchai <chaifix@163.com>2019-03-14 09:08:07 +0800
commit8d8c4ff1664625e7428d0d31cd798d9321680cb2 (patch)
tree67af1dad8483ba1c886ae1bd00b9c664ee245385 /Source/3rdParty/Luax/luax_class.hpp
parent6016ece202eef94ed76bd20d4f7879ccc71cc2e6 (diff)
*luax
Diffstat (limited to 'Source/3rdParty/Luax/luax_class.hpp')
-rw-r--r--Source/3rdParty/Luax/luax_class.hpp150
1 files changed, 150 insertions, 0 deletions
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 <vector>
+
+#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; };
+
+ ///
+ /// Ҫ¶luaclassҪ̳дࡣͨluaʵҪȷüȷԣڶ߳Ҫȷͷš
+ ///
+ template<class T>
+ 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<T>͵ʵ
+ ///
+ 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