summaryrefslogtreecommitdiff
path: root/source/3rd-party/Luax/luax_class.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/3rd-party/Luax/luax_class.hpp')
-rw-r--r--source/3rd-party/Luax/luax_class.hpp176
1 files changed, 176 insertions, 0 deletions
diff --git a/source/3rd-party/Luax/luax_class.hpp b/source/3rd-party/Luax/luax_class.hpp
new file mode 100644
index 0000000..ea1fab9
--- /dev/null
+++ b/source/3rd-party/Luax/luax_class.hpp
@@ -0,0 +1,176 @@
+#ifndef __LUAX_CLASS_H__
+#define __LUAX_CLASS_H__
+
+#include <vector>
+
+#include "luax_config.h"
+#include "luax_ref.h"
+#include "luax_memberref.h"
+#include "luax_cfunctions.h"
+
+namespace Luax
+{
+
+#define LUAX_DECL_METHOD(mtd) static int mtd(lua_State* L)
+
+ ///
+ /// RegisterLuaxClass עķͳԱö١ȵclass table
+ /// LuaxGetFactoryName ùͬʱעʱעΪsingletonͨʱ
+ ///
+#define LUAX_DECL_FACTORY(type) \
+ static void RegisterLuaxClass(Luax::LuaxState&);\
+ static void RegisterLuaxPostprocess(Luax::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(Luax::LuaxState&); \
+ static void RegisterLuaxPostprocess(Luax::LuaxState&); \
+ static const char* GetLuaxSingletonName() { return #type; }; \
+ static const char* GetLuaxClassName() { return #type; }; \
+ static bool IsLuaxClassSingleton() { return true; };
+
+#define LUAX_IMPL_METHOD(type, f) int type::f(lua_State* L)
+
+#define LUAX_REGISTRY(type) void type::RegisterLuaxClass(Luax::LuaxState& state)
+
+#define LUAX_POSTPROCESS(type) void type::RegisterLuaxPostprocess(Luax::LuaxState& state)
+
+#define LUAX_REGISTER_FACTORY(stt, type) stt.RegisterFactory<type>()
+
+#define LUAX_REGISTER_SINGLETON(stt, type) stt.RegisterSingleton<type>()
+
+ ///
+ /// Ҫ¶luanative classҪ̳дࡣͨluaʵҪȷüȷԣڶ߳Ҫȷͷš
+ ///
+ template<class T>
+ class LuaxNativeClass
+ {
+ public:
+
+ ///
+ /// userdataΪkeyref tableuserdataһãάuserdataڡ
+ ///
+ template<class U> void LuaRetain(LuaxState& state, U* userdata);
+
+ ///
+ /// userdataһref tableԳԻuserdata
+ ///
+ template<class U> void LuaRelease(LuaxState& state, U* userdata);
+
+ protected:
+
+ LuaxNativeClass();
+ virtual ~LuaxNativeClass();
+
+ ///
+ /// userdata pushջûгʼmUserdataʼúԪѳʼõuserdataջһá
+ ///
+ bool PushLuaxUserdata(LuaxState& state);
+ bool PushLuaxMemberTable(LuaxState& state);
+ bool PushLuaxRefTable(LuaxState& state);
+
+ ///
+ /// Աùʵref tableáȡ
+ ///
+ void SetMemberRef(LuaxState& state, LuaxMemberRef& memRef, int idx);
+ bool PushMemberRef(LuaxState& state, LuaxMemberRef& memRef);
+ void ClearMemberRef(LuaxState& state, LuaxMemberRef& memRef);
+
+ private:
+
+ friend class LuaxState;
+
+ static void RegisterLuaxClass(LuaxState& state);
+ static void RegisterLuaxFactoryClass(LuaxState& state);
+ static void RegisterLuaxSingletonClass(LuaxState& state);
+
+ static void SetLuaxClassTableRef(LuaxState& state, int idx);
+
+ static void PushLuaxClassTable(LuaxState& state);
+
+ ///
+ /// ȡַҪַֻͨڶϴʵõջϺ;̬ıȡַ֤ü׼ȷ
+ /// ҪãʹôݶǴݵַ
+ ///
+ void* operator &();
+
+ ///
+ /// userdataʵstate
+ ///
+ void BindToLua(LuaxState& state);
+
+ //------------------------------------------------------------------------------------------------------------
+
+ ///
+ /// LuaxNativeClass<T>͵ʵ
+ ///
+ static LuaxStrongRef mClassTable; // class table͵
+ static LuaxStrongRef mSingletonRefTable; // ǵsingletonijԱԱ֤ᱻͨ
+ // ref tableijԱȫڵģֱ_LUAX_STRONGREF_TABLE
+
+ ///
+ /// ͨuserdataõ:
+ /// 1: ref table
+ /// 2: member table
+ /// 3: class table
+ ///
+ LuaxWeakRef mUserdata;
+
+ public:
+
+ //------------------------------------------------------------------------------------------------------------
+ //
+
+ LUAX_DECL_METHOD( l___tostring );
+ LUAX_DECL_METHOD( l_GetClass );
+ LUAX_DECL_METHOD( l_GetClassName );
+
+ //------------------------------------------------------------------------------------------------------------
+ //
+
+ LUAX_DECL_METHOD( l___gc );
+#if LUAX_ENABLE_NATIVE_EXTEND
+ LUAX_DECL_METHOD( l_ExtendFactory );
+#endif
+ LUAX_DECL_METHOD( l_GetRefTable );
+ LUAX_DECL_METHOD( l_New );
+
+ //------------------------------------------------------------------------------------------------------------
+ //
+#if LUAX_ENABLE_NATIVE_EXTEND
+ LUAX_DECL_METHOD( l_ExtendSingleton );
+#endif
+ };
+
+ //--------------------------------------------------------------------------------------------------------------
+
+#if LUAX_ENABLE_PLAIN_CLASS
+ ///
+ /// lua
+ ///
+ class LuaxPlainClass
+ {
+ public:
+
+ ///
+ /// עںͨregistry()עࡣ
+ ///
+ static int registry(lua_State* L);
+
+ LUAX_DECL_METHOD( l___tostring );
+ LUAX_DECL_METHOD( l_Extend );
+ LUAX_DECL_METHOD( l_New );
+ LUAX_DECL_METHOD( l_TypeOf );
+
+ };
+#endif
+
+}
+
+#endif \ No newline at end of file