diff options
Diffstat (limited to 'Source/external/Luax/luax_class.hpp')
-rw-r--r-- | Source/external/Luax/luax_class.hpp | 207 |
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 + //{ + //} + + /// + /// ��Ҫ��¶��lua��native class��Ҫ�̳д��ࡣͨ��lua������ʵ��Ҫȷ�����ü�������ȷ�ԣ��ڶ���߳�����Ҫȷ + /// ���������ͷš� + /// + template<class TYPE, class BASE = LuaxObject> + class LuaxNativeClass : public BASE + { + public: + + /// + /// ��userdata��Ϊkey����ref table���userdata����һ�����ã���ά��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_TABLE��������userdata���� + /// LuaxRetain\LuaxRelease��member 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 |