diff options
Diffstat (limited to 'src/lua/common')
-rw-r--r-- | src/lua/common/Proxy.h | 79 | ||||
-rw-r--r-- | src/lua/common/Reference.hpp | 144 | ||||
-rw-r--r-- | src/lua/common/common.h | 1 | ||||
-rw-r--r-- | src/lua/common/error.h | 28 |
4 files changed, 155 insertions, 97 deletions
diff --git a/src/lua/common/Proxy.h b/src/lua/common/Proxy.h index 8323ead..5ebb5b2 100644 --- a/src/lua/common/Proxy.h +++ b/src/lua/common/Proxy.h @@ -3,46 +3,65 @@ #include "Reference.hpp" -namespace jin +namespace JinEngine { -namespace lua -{ - - class Proxy + namespace Lua { - public: - void bind(RefBase* ref) - { - if (ref == nullptr) - return; - reference = ref; - } - void release() + class Proxy { - if (reference != nullptr) + public: + void bind(RefBase* ref) { - reference->release(); - reference = nullptr; + if (ref == nullptr) + return; + reference = ref; } - } - template<class T> - Ref<T>& getRef() - { - return *(Ref<T>*) reference; - } + void release() + { + if (reference != nullptr) + { + reference->release(); + reference = nullptr; + } + } - const char* getObjectType() - { - return reference->type; - } + void retain() + { + if (reference != nullptr) + reference->retain(); + } + + void setUserdata(void* data) + { + if (reference != nullptr) + reference->setUserdata(data); + } + + template<class T> + Ref<T>& getRef() + { + return *(Ref<T>*) reference; + } + + template<class T> + T* getObject() + { + Ref<T>& ref = getRef<T>(); + return ref.getObject(); + } + + const char* getObjectType() + { + return reference->type; + } - RefBase* reference; + RefBase* reference; - }; + }; -} // lua -} // jin + } // namespace Lua +} // namespace JinEngine #endif // __JIN_COMMON_PROXY_H
\ No newline at end of file diff --git a/src/lua/common/Reference.hpp b/src/lua/common/Reference.hpp index feb96bb..ba918bb 100644 --- a/src/lua/common/Reference.hpp +++ b/src/lua/common/Reference.hpp @@ -1,78 +1,88 @@ #ifndef __JIN_COMMON_REFERENCE_H #define __JIN_COMMON_REFERENCE_H -namespace jin +namespace JinEngine { -namespace lua -{ - - /*abstract*/class RefBase + namespace Lua { - public: - void retain() - { - ++count; - } - void release() + /*abstract*/class RefBase { - if (--count <= 0) - delete this; - } - - // object type string - const char* const type; - - protected: - RefBase(void* obj, const char* t) - : count(1) - , object(obj) - , type(t) - { - } - - RefBase(const RefBase&); - - virtual ~RefBase() - { - } - - void* object; - int count; - - }; - - template<class T> - class Ref : public RefBase - { - public: - Ref(T* obj, const char* type) - : RefBase(obj, type) + public: + void retain() + { + ++count; + } + + void release() + { + if (--count <= 0) + delete this; + } + + // object type string + const char* const type; + + void setUserdata(void* data) + { + userdata = data; + } + + void* getUserdata() + { + return userdata; + } + + protected: + RefBase(void* obj, const char* t) + : count(1) + , object(obj) + , type(t) + { + } + + RefBase(const RefBase&); + + virtual ~RefBase() + { + } + + void* object; + int count; + void* userdata; + }; + + template<class T> + class Ref : public RefBase { - } - - ~Ref() - { - T* obj = (T*)object; - delete obj; - } - - T* operator->() - { - return (T*)object; - } - - T* getObject() - { - return (T*)object; - } - - private: - Ref(const Ref<T>& ref); - - }; - -} + public: + Ref(T* obj, const char* type) + : RefBase(obj, type) + { + } + + ~Ref() + { + T* obj = static_cast<T*>(object); + delete obj; + } + + T* operator->() + { + return (T*)object; + } + + T* getObject() + { + return (T*)object; + } + + private: + Ref(const Ref<T>& ref); + + }; + + } } #endif
\ No newline at end of file diff --git a/src/lua/common/common.h b/src/lua/common/common.h index 536b897..0ee72cc 100644 --- a/src/lua/common/common.h +++ b/src/lua/common/common.h @@ -3,5 +3,6 @@ #include "Proxy.h" #include "Reference.hpp" +#include "error.h" #endif
\ No newline at end of file diff --git a/src/lua/common/error.h b/src/lua/common/error.h new file mode 100644 index 0000000..c254486 --- /dev/null +++ b/src/lua/common/error.h @@ -0,0 +1,28 @@ +#ifndef __JIN_ERROR_H +#define __JIN_ERROR_H +#include "../../luax.h" +#include "../jin.h" +#include <string.h> + +namespace JinEngine +{ +namespace Lua +{ + + static const int FORMAT_MSG_BUFFER_SIZE = 2048; + + inline void error(lua_State* L, const char* fmt, ...) + { + char err[FORMAT_MSG_BUFFER_SIZE + 1] = { 0 }; + va_list args; + va_start(args, fmt); + vsnprintf(err + strlen(err), FORMAT_MSG_BUFFER_SIZE, fmt, args); + va_end(args); + luax_getglobal(L, MODULE_NAME); + luax_setfieldstring(L, "error", err); + } + +} +} + +#endif
\ No newline at end of file |