From 15740faf9fe9fe4be08965098bbf2947e096aeeb Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 14 Aug 2019 22:50:43 +0800 Subject: +Unity Runtime code --- Runtime/Scripting/ScriptingObjectOfType.h | 142 ++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 Runtime/Scripting/ScriptingObjectOfType.h (limited to 'Runtime/Scripting/ScriptingObjectOfType.h') 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 +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 (cachedPtr)->GetInstanceID() != GetInstanceID()); + return (T*)cachedPtr; + } + + T* temp = dynamic_instanceID_cast (GetInstanceID()); + return temp; + } + + bool IsValidObjectReference () const + { + if (IsNullPtr()) + return false; + + void* cachedPtr = GetCachedPtr(); + if (cachedPtr != NULL) + { + AssertIf(reinterpret_cast (cachedPtr)->GetInstanceID() != GetInstanceID()); + return (T*)cachedPtr; + } + + T* temp = dynamic_instanceID_cast (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 () const + { + if (IsNullPtr()) + return PPtr (); + + return PPtr (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& operator = (decltype(__nullptr)) + { + object = nullptr; + return *this; + } +#endif + + inline ScriptingObjectPtr GetScriptingObject() const + { + return object; + } +}; + + + + -- cgit v1.1-26-g67d0