Add more objects types checks and casting utilities to ScriptingObject

This commit is contained in:
Wojtek Figat
2020-12-21 14:03:15 +01:00
parent 3a314d97eb
commit 585c752d72
2 changed files with 32 additions and 4 deletions

View File

@@ -131,12 +131,30 @@ public:
return obj ? obj->GetOrCreateManagedInstance() : nullptr;
}
/// <summary>
/// Checks if can cast one scripting object type into another type.
/// </summary>
/// <param name="from">The object type for the cast.</param>
/// <param name="to">The destination type to the cast.</param>
/// <returns>True if can, otherwise false.</returns>
static bool CanCast(const ScriptingTypeHandle& from, const ScriptingTypeHandle& to);
/// <summary>
/// Checks if can cast one scripting object type into another type.
/// </summary>
/// <param name="from">The object class for the cast.</param>
/// <param name="to">The destination class to the cast.</param>
/// <returns>True if can, otherwise false.</returns>
static bool CanCast(MClass* from, MClass* to);
template<typename T>
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:
/// </summary>
void UnregisterObject();
private:
static bool CanCast(MClass* from, MClass* to);
protected:
/// <summary>