diff options
Diffstat (limited to 'source/3rd-party/Luax/luax_class.hpp')
| -rw-r--r-- | source/3rd-party/Luax/luax_class.hpp | 176 |
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>() + + /// + /// ÐèÒª±©Â¶¸øluaµÄnative classÐèÒª¼Ì³Ð´ËÀࡣͨ¹ýlua¹ÜÀíµÄʵÀýҪȷ±£ÒýÓüÆÊýµÄÕýÈ·ÐÔ£¬ÔÚ¶à¸öÏß³ÌÖÐÐèҪȷ¶¨²»»áÎóÊÍ·Å¡£ + /// + template<class T> + class LuaxNativeClass + { + public: + + /// + /// ½«userdata×÷Ϊkey£¬ÔÚref tableÀï¶ÔuserdataÌí¼ÓÒ»¸öÒýÓã¬ÒÔά³Ö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; // Èç¹ûÀàÊǵ¥Àý£¬Õâ¸öÓÃÀ´±£´æsingletonµÄ³ÉÔ±£¬ÒÔ±£Ö¤²»»á±»»ØÊÕÀàËÆÆÕͨÀàµÄ + // ref table¡£µ¥ÀýµÄ³ÉÔ±ÊÇÈ«ÉúÃüÖÜÆÚµÄ£¬ËùÒÔÖ±½ÓÔÚ_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 |
