diff --git a/Moose Development/Moose/Core/Base.lua b/Moose Development/Moose/Core/Base.lua index 101204c1f..72d00d7e6 100644 --- a/Moose Development/Moose/Core/Base.lua +++ b/Moose Development/Moose/Core/Base.lua @@ -287,6 +287,63 @@ function BASE:GetParent( Child ) return Parent end +--- This is the worker method to check if an object is an (sub)instance of a class. +-- +-- ### Examples: +-- +-- * ZONE:New( 'some zone' ):IsInstanceOf( ZONE ) will return true +-- * ZONE:New( 'some zone' ):IsInstanceOf( 'ZONE' ) will return true +-- * ZONE:New( 'some zone' ):IsInstanceOf( 'zone' ) will return true +-- * ZONE:New( 'some zone' ):IsInstanceOf( 'BASE' ) will return true +-- +-- * ZONE:New( 'some zone' ):IsInstanceOf( 'GROUP' ) will return false +-- +-- @param ClassName is the name of the class or the class itself to run the check against +-- @return #boolean +function BASE:IsInstanceOf( className ) + + -- Is className NOT a string ? + if type( className ) ~= 'string' then + + -- Is className a Moose class ? + if type( className ) == 'table' and className.ClassName ~= nil then + + -- Get the name of the Moose class as a string + className = className.ClassName + + -- className is neither a string nor a Moose class, throw an error + else + + -- I'm not sure if this should take advantage of MOOSE logging function, or throw an error for pcall + local err_str = 'className parameter should be a string; parameter received: '..type( className ) + self:E( err_str ) + -- error( err_str ) + return false + + end + end + + className = string.upper( className ) + + if string.upper( self.ClassName ) == className then + return true + end + + local Parent = self:GetParent(self) + + while Parent do + + if string.upper( Parent.ClassName ) == className then + return true + end + + Parent = Parent:GetParent(Parent) + + end + + return false + +end --- Get the ClassName + ClassID of the class instance. -- The ClassName + ClassID is formatted as '%s#%09d'. -- @param #BASE self diff --git a/docs/Documentation/Base.html b/docs/Documentation/Base.html index e79a06992..032f52796 100644 --- a/docs/Documentation/Base.html +++ b/docs/Documentation/Base.html @@ -257,6 +257,12 @@ BASE:Inherit(Child, Parent)

This is the worker method to inherit from a parent class.

+ + + + BASE.IsInstanceOf(ClassName, self, className) + +

This is the worker method to check if an object is an (sub)instance of a class.

@@ -1220,6 +1226,56 @@ is the Parent class that the Child inherits from.

#BASE: Child

+ + +
+
+ + +BASE.IsInstanceOf(ClassName, self, className) + +
+
+ +

This is the worker method to check if an object is an (sub)instance of a class.

+ + + +

Examples:

+ + + + +

Parameters

+ +

Return value

+ +

#boolean:

+ +