diff --git a/Source/Engine/Scripting/ScriptingObject.cpp b/Source/Engine/Scripting/ScriptingObject.cpp index a065d6a0a..d7e8c3729 100644 --- a/Source/Engine/Scripting/ScriptingObject.cpp +++ b/Source/Engine/Scripting/ScriptingObject.cpp @@ -72,6 +72,12 @@ ScriptingObject* ScriptingObject::ToNative(MonoObject* obj) return ptr; } +bool ScriptingObject::Is(const ScriptingTypeHandle& type) const +{ + CHECK_RETURN(type, false); + return _type == type || CanCast(GetClass(), type.GetType().ManagedClass); +} + void ScriptingObject::ChangeID(const Guid& newId) { ASSERT(newId.IsValid() && newId != _id); @@ -206,6 +212,14 @@ void ScriptingObject::UnregisterObject() Scripting::UnregisterObject(this); } +bool ScriptingObject::CanCast(const ScriptingTypeHandle& from, const ScriptingTypeHandle& to) +{ + if (!from && !to) + return true; + CHECK_RETURN(from && to, false); + return CanCast(from.GetType().ManagedClass, to.GetType().ManagedClass); +} + bool ScriptingObject::CanCast(MClass* from, MClass* to) { if (!from && !to) diff --git a/Source/Engine/Scripting/ScriptingObject.h b/Source/Engine/Scripting/ScriptingObject.h index 0f8b26375..540e0d60b 100644 --- a/Source/Engine/Scripting/ScriptingObject.h +++ b/Source/Engine/Scripting/ScriptingObject.h @@ -131,12 +131,30 @@ public: return obj ? obj->GetOrCreateManagedInstance() : nullptr; } + /// + /// Checks if can cast one scripting object type into another type. + /// + /// The object type for the cast. + /// The destination type to the cast. + /// True if can, otherwise false. + static bool CanCast(const ScriptingTypeHandle& from, const ScriptingTypeHandle& to); + + /// + /// Checks if can cast one scripting object type into another type. + /// + /// The object class for the cast. + /// The destination class to the cast. + /// True if can, otherwise false. + static bool CanCast(MClass* from, MClass* to); + template static T* Cast(ScriptingObject* obj) { return obj && CanCast(obj->GetClass(), T::GetStaticClass()) ? (T*)obj : nullptr; } + bool Is(const ScriptingTypeHandle& type) const; + bool Is(MClass* type) const { return CanCast(GetClass(), type); @@ -185,10 +203,6 @@ public: /// void UnregisterObject(); -private: - - static bool CanCast(MClass* from, MClass* to); - protected: ///