summaryrefslogtreecommitdiff
path: root/Source/external/Luax/luax_class.hpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2019-08-07 21:08:47 +0800
committerchai <chaifix@163.com>2019-08-07 21:08:47 +0800
commit0c391fdbce5a079cf03e483eb6174dd47806163d (patch)
treeb06cd7a9d0ae0d9bb9e82f3dcb786dfce11f8628 /Source/external/Luax/luax_class.hpp
parent9686368e58e25cbd6dc37d686bdd2be3f80486d6 (diff)
*misc
Diffstat (limited to 'Source/external/Luax/luax_class.hpp')
-rw-r--r--Source/external/Luax/luax_class.hpp207
1 files changed, 207 insertions, 0 deletions
diff --git a/Source/external/Luax/luax_class.hpp b/Source/external/Luax/luax_class.hpp
new file mode 100644
index 0000000..12eb268
--- /dev/null
+++ b/Source/external/Luax/luax_class.hpp
@@ -0,0 +1,207 @@
+#ifndef __LUAX_CLASS_H__
+#define __LUAX_CLASS_H__
+
+#include "luax_config.h"
+
+#if LUAX_PROFILER
+#include <unordered_set>
+#endif
+
+#include <vector>
+
+#include "luax_ref.h"
+#include "luax_memberref.h"
+#include "luax_cfunctions.h"
+#include "luax_watchdog.h"
+#include "luax_utility.h"
+
+namespace Luax
+{
+
+ class LuaxVM;
+
+ ///
+ /// ࣬Ϊʵֶ̬ҪЩӿڵⲿҪ̳д֮࣬оͻ
+ /// öӦʵķע̳дʱʵķʵLuaxNativeClassУʵֻ
+ /// ¶ԡ
+ ///
+ /// Effective C++40ڱʹvirtual base£Ӧþܱз
+ /// ݳԱݳԱʼɵһЩ⡣һ㣬vpbӽӽC#Java
+ /// InterfaceԣIͷʶһӿڡ
+ ///
+ class LuaxObject
+ {
+ public:
+ LuaxObject() {};
+ virtual ~LuaxObject() {};
+
+ ///
+ /// Աùʵref tableáȡ
+ ///
+ virtual bool PushLuaxMemberRef(LuaxState& state, int refID) = 0;
+ virtual bool PushLuaxUserdata(LuaxState& state) = 0;
+ virtual bool PushLuaxMemberTable(LuaxState& state) = 0;
+ virtual bool PushLuaxRefTable(LuaxState& state) = 0;
+
+ ///
+ /// LuaxNativeClassʵֺ֡ͷnativeԴ
+ ///
+ virtual void Retain() = 0;
+ virtual void Release() = 0;
+
+ };
+
+ // TODO: ȡҪظɴ
+ //class LuaxNativeClassBase
+ //{
+ //}
+
+ ///
+ /// Ҫ¶luanative classҪ̳дࡣͨluaʵҪȷüȷԣڶ߳Ҫȷ
+ /// ͷš
+ ///
+ template<class TYPE, class BASE = LuaxObject>
+ class LuaxNativeClass : public BASE
+ {
+ public:
+
+ ///
+ /// userdataΪkeyref tableuserdataһãάuserdataڡ
+ /// Ƚmember refʵᱻαͬʵõƵЩʵ壬
+ /// luaƵĵgc⡣
+ ///
+ template<class DATATYPE> void LuaxRetain(LuaxState& state, DATATYPE* userdata);
+
+ ///
+ /// userdataһref tableԳԻuserdata
+ ///
+ template<class DATATYPE> void LuaxRelease(LuaxState& state, DATATYPE* userdata);
+
+ ///
+ /// userdata pushջûгʼmUserdataʼúԪѳʼõ
+ /// userdataջһáһnativeȨƽluaƵķ
+ ///
+ bool PushLuaxMemberRef(LuaxState& state, int refID) override;
+ bool PushLuaxUserdata(LuaxState& state) override;
+ bool PushLuaxMemberTable(LuaxState& state) override;
+ bool PushLuaxRefTable(LuaxState& state) override;
+
+ ///
+ /// Watch dog һnativeáluaVMòṩⲿӿڡ̳д಻ֱʹ
+ /// deleteӦʹReleaseͷšһ__gcУû
+ /// nativeиͷţҪʹRelease
+ ///
+ /// nativeӿڡ
+ ///
+ void Retain() override final;
+ void Release() override final;
+
+#if LUAX_PROFILER
+ // Զϴʵdeleteռ
+ static void operator delete(void* pdead, size_t size);
+#endif
+
+ protected:
+
+ LuaxNativeClass();
+ virtual ~LuaxNativeClass();
+
+ ///
+ /// Աùʵref tableáȡ
+ ///
+ void SetLuaxMemberRef(LuaxState& state, LuaxMemberRef& memRef, int idx);
+ bool PushLuaxMemberRef(LuaxState& state, LuaxMemberRef& memRef);
+ void ClearLuaxMemberRef(LuaxState& state, LuaxMemberRef& memRef);
+
+ private:
+
+ friend class LuaxState;
+
+ static void RegisterLuaxClassShared(LuaxState& state);
+ static void RegisterLuaxFactoryClass(LuaxState& state);
+ static void RegisterLuaxSingletonClass(LuaxState& state);
+
+ static void SetLuaxClassTableRef(LuaxState& state, int idx);
+ static void PushLuaxClassTable(LuaxState& state);
+
+ ///
+ /// userdataʵstate
+ ///
+ void BindToLua(LuaxState& state);
+
+ //------------------------------------------------------------------------------//
+
+ //
+ static int __tostring (lua_State*);
+ static int _GetClass (lua_State*);
+ static int _GetClassName (lua_State*);
+
+ //
+ static int __gc (lua_State*);
+ static int _GetRefTable (lua_State*);
+ static int _New (lua_State*);
+
+#if LUAX_ENABLE_NATIVE_EXTEND
+ static int _ExtendFactory (lua_State*);
+ static int _ExtendSingleton (lua_State*);
+#endif
+
+ //--------------------------------------------------------------------------------//
+
+ ///
+ /// class table͵С
+ ///
+ static LuaxStrongRef mClassTable;
+
+ ///
+ /// ǵsingletonùϵԱ֤ᱻͨref table
+ /// ijԱȫڵģֱ_LUAX_STRONGREF_TABLEuserdata
+ /// LuaxRetain\LuaxReleasemember refʱ͹ʵͬǴref table
+ /// ģtable_LUAX_STRONGREF_TABLE
+ ///
+ static LuaxStrongRef mSingletonRefTable;
+
+ ///
+ /// ͨuserdataõ:
+ /// 1: ref table
+ /// 2: member table
+ /// 3: class table
+ ///
+ LuaxWeakRef mUserdata;
+
+ /// ͨɾ
+ LuaxWatchDog mWatchDog;
+
+#if LUAX_PROFILER
+ // йܴ˶
+ std::unordered_set<LuaxVM*> mRefVMs;
+ // գ಻ⲿʹdeleteֱɾӦʹRelease
+ bool mSafer;
+#endif
+
+ };
+
+#if LUAX_ENABLE_PLAIN_CLASS
+ ///
+ /// lua
+ ///
+ class LuaxPlainClass
+ {
+ public:
+
+ ///
+ /// עںͨregistry()עࡣ
+ ///
+ static int registry(lua_State* L);
+
+ LUAX_DECL_METHOD(__tostring);
+ LUAX_DECL_METHOD(_Extend);
+ LUAX_DECL_METHOD(_New);
+ LUAX_DECL_METHOD(_TypeOf);
+
+ };
+#endif
+
+}
+
+#endif \ No newline at end of file