diff options
author | chai <chaifix@163.com> | 2019-08-14 22:50:43 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2019-08-14 22:50:43 +0800 |
commit | 15740faf9fe9fe4be08965098bbf2947e096aeeb (patch) | |
tree | a730ec236656cc8cab5b13f088adfaed6bb218fb /Runtime/Scripting/ScriptingObjectOfType.h |
Diffstat (limited to 'Runtime/Scripting/ScriptingObjectOfType.h')
-rw-r--r-- | Runtime/Scripting/ScriptingObjectOfType.h | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/Runtime/Scripting/ScriptingObjectOfType.h b/Runtime/Scripting/ScriptingObjectOfType.h new file mode 100644 index 0000000..cca6bde --- /dev/null +++ b/Runtime/Scripting/ScriptingObjectOfType.h @@ -0,0 +1,142 @@ +#pragma once + +#include "Runtime/Scripting/Backend/ScriptingTypes.h" +#include "Scripting.h" +#include "ScriptingUtility.h"//We need to go back and remove this. + +template<class T> +struct ScriptingObjectOfType +{ +private: + ScriptingObjectPtr object; +public: + ScriptingObjectOfType(ScriptingObjectPtr object) + { + this->object = object; + } + + T& GetReference () const + { + T* ptr = GetPtr(); + + if (ptr != NULL) + return *ptr; + + Scripting::RaiseNullExceptionObject (object); + return *(T*)NULL; + } + + T* GetPtr () const + { + if (IsNullPtr()) + return NULL; + + void* cachedPtr = GetCachedPtr(); + if (cachedPtr != NULL) + { + AssertIf(reinterpret_cast<Object*> (cachedPtr)->GetInstanceID() != GetInstanceID()); + return (T*)cachedPtr; + } + + T* temp = dynamic_instanceID_cast<T*> (GetInstanceID()); + return temp; + } + + bool IsValidObjectReference () const + { + if (IsNullPtr()) + return false; + + void* cachedPtr = GetCachedPtr(); + if (cachedPtr != NULL) + { + AssertIf(reinterpret_cast<Object*> (cachedPtr)->GetInstanceID() != GetInstanceID()); + return (T*)cachedPtr; + } + + T* temp = dynamic_instanceID_cast<T*> (GetInstanceID()); + if (temp == NULL) + return false; + + // MonoBheaviours are only allowed to be the instance themselves (cached pointer) + // otherwise they are dangling references who happen to have the same instanceID. + // Thus not a valid reference. + if (temp->GetClassID() == ClassID(MonoBehaviour)) + return false; + + return true; + } + + + operator T* () const + { + return GetPtr (); + } + + operator PPtr<T> () const + { + if (IsNullPtr()) + return PPtr<T> (); + + return PPtr<T> (GetInstanceID()); + } + + T& operator * () const + { + return GetReference (); + } + + T* operator -> () const + { + return &GetReference (); + } + + bool IsNullPtr() const + { + return object == SCRIPTING_NULL; + } + + inline int GetInstanceID() const + { + return Scripting::GetInstanceIDFromScriptingWrapper(object); + } + + inline void SetCachedPtr (void* cachedPtr) + { + Scripting::SetCachedPtrOnScriptingWrapper(object, cachedPtr); + } + + inline void SetInstanceID (int instanceID) + { + Scripting::SetInstanceIDOnScriptingWrapper(object, instanceID); + } + + inline void* GetCachedPtr() const + { + return Scripting::GetCachedPtrFromScriptingWrapper(object); + } + + #if MONO_QUALITY_ERRORS + inline void SetError (ScriptingStringPtr error) + { + Scripting::SetErrorOnScriptingWrapper(object, error); + } + #endif + +#if UNITY_WINRT + ScriptingObjectOfType<T>& operator = (decltype(__nullptr)) + { + object = nullptr; + return *this; + } +#endif + + inline ScriptingObjectPtr GetScriptingObject() const + { + return object; + } +}; + + + + |