From 2f416ea98ec8ac73d4cd7eab627692bd9e25a1d5 Mon Sep 17 00:00:00 2001 From: 132nd-etcher <132nd-etcher@daribouca.net> Date: Wed, 12 Jul 2017 14:55:01 +0200 Subject: [PATCH 1/3] Update BASE:GetParent( Child ) so that it returns nil when called on BASE class We need to know if that the BASE class has no parent. --- Moose Development/Moose/Core/Base.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Moose Development/Moose/Core/Base.lua b/Moose Development/Moose/Core/Base.lua index b9e218703..101204c1f 100644 --- a/Moose Development/Moose/Core/Base.lua +++ b/Moose Development/Moose/Core/Base.lua @@ -276,7 +276,10 @@ end -- @return #BASE function BASE:GetParent( Child ) local Parent - if rawget( Child, "__" ) then + -- BASE class has no parent + if Child.ClassName == 'BASE' then + Parent = nil + elseif rawget( Child, "__" ) then Parent = getmetatable( Child.__ ).__index else Parent = getmetatable( Child ).__index From 82fd08521f250902aad3ecee8aea539d23890c00 Mon Sep 17 00:00:00 2001 From: 132nd-etcher <132nd-etcher@daribouca.net> Date: Wed, 12 Jul 2017 14:55:15 +0200 Subject: [PATCH 2/3] Add UTILS.IsInstanceOf = function( object, className ) This function takes any object and check if it is an instance of className. The object can be either a MOOSE class or a basic lua type. --- Moose Development/Moose/Utilities/Utils.lua | 63 ++++++++++++ docs/Documentation/Utils.html | 100 ++++++++++++++++++++ 2 files changed, 163 insertions(+) diff --git a/Moose Development/Moose/Utilities/Utils.lua b/Moose Development/Moose/Utilities/Utils.lua index b1f481ed6..6d9a733da 100644 --- a/Moose Development/Moose/Utilities/Utils.lua +++ b/Moose Development/Moose/Utilities/Utils.lua @@ -33,6 +33,69 @@ FLARECOLOR = trigger.flareColor -- #FLARECOLOR -- @type UTILS UTILS = {} +--- Function to infer instance of an object +-- +-- ### Examples: +-- +-- * UTILS.IsInstanceOf( 'some text', 'string' ) will return true +-- * UTILS.IsInstanceOf( some_function, 'function' ) will return true +-- * UTILS.IsInstanceOf( 10, 'number' ) will return true +-- * UTILS.IsInstanceOf( false, 'boolean' ) will return true +-- * UTILS.IsInstanceOf( nil, 'nil' ) will return true +-- +-- * UTILS.IsInstanceOf( ZONE:New( 'some zone', ZONE ) will return true +-- * UTILS.IsInstanceOf( ZONE:New( 'some zone', 'ZONE' ) will return true +-- * UTILS.IsInstanceOf( ZONE:New( 'some zone', 'zone' ) will return true +-- * UTILS.IsInstanceOf( ZONE:New( 'some zone', 'BASE' ) will return true +-- +-- * UTILS.IsInstanceOf( ZONE:New( 'some zone', 'GROUP' ) will return false +-- +-- +-- @param object is the object to be evaluated +-- @param className is the name of the class to evaluate (can be either a string or a Moose class) +-- @return #boolean +UTILS.IsInstanceOf = function( object, className ) + -- Is className NOT a string ? + if not type( className ) == 'string' then + + -- Is className a Moose class ? + if type( className ) == 'table' and className.IsInstanceOf ~= 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 ) + return false + -- error( err_str ) + + end + end + + -- Is the object a Moose class instance ? + if type( object ) == 'table' and object.IsInstanceOf ~= nil then + + -- Use the IsInstanceOf method of the BASE class + return object:IsInstanceOf( className ) + else + + -- If the object is not an instance of a Moose class, evaluate against lua basic data types + local basicDataTypes = { 'string', 'number', 'function', 'boolean', 'nil', 'table' } + for _, basicDataType in ipairs( basicDataTypes ) do + if className == basicDataType then + return type( object ) == basicDataType + end + end + end + + -- Check failed + return false +end + --from http://lua-users.org/wiki/CopyTable UTILS.DeepCopy = function(object) diff --git a/docs/Documentation/Utils.html b/docs/Documentation/Utils.html index 9d8cb3142..13adb0b4a 100644 --- a/docs/Documentation/Utils.html +++ b/docs/Documentation/Utils.html @@ -236,12 +236,41 @@ which are excellent tools to be reused in an OO environment!.
Function to infer instance of an object
+ +UTILS.IsInstanceOf( 'some text', 'string' ) will return true
UTILS.IsInstanceOf( some_function, 'function' ) will return true
UTILS.IsInstanceOf( 10, 'number' ) will return true
UTILS.IsInstanceOf( false, 'boolean' ) will return true
UTILS.IsInstanceOf( nil, 'nil' ) will return true
UTILS.IsInstanceOf( ZONE:New( 'some zone', ZONE ) will return true
UTILS.IsInstanceOf( ZONE:New( 'some zone', 'ZONE' ) will return true
UTILS.IsInstanceOf( ZONE:New( 'some zone', 'zone' ) will return true
UTILS.IsInstanceOf( ZONE:New( 'some zone', 'BASE' ) will return true
UTILS.IsInstanceOf( ZONE:New( 'some zone', 'GROUP' ) will return false
Function to infer instance of an object
+ +UTILS.IsInstanceOf( 'some text', 'string' ) will return true
UTILS.IsInstanceOf( some_function, 'function' ) will return true
UTILS.IsInstanceOf( 10, 'number' ) will return true
UTILS.IsInstanceOf( false, 'boolean' ) will return true
UTILS.IsInstanceOf( nil, 'nil' ) will return true
UTILS.IsInstanceOf( ZONE:New( 'some zone', ZONE ) will return true
UTILS.IsInstanceOf( ZONE:New( 'some zone', 'ZONE' ) will return true
UTILS.IsInstanceOf( ZONE:New( 'some zone', 'zone' ) will return true
UTILS.IsInstanceOf( ZONE:New( 'some zone', 'BASE' ) will return true
UTILS.IsInstanceOf( ZONE:New( 'some zone', 'GROUP' ) will return false
object :
+is the object to be evaluated
className :
+is the name of the class to evaluate (can be either a string or a Moose class)
#boolean:
+ + + knots :
This is the worker method to inherit from a parent class.
+This is the worker method to check if an object is an (sub)instance of a class.
#BASE: Child
+ + +This is the worker method to check if an object is an (sub)instance of a class.
+ + + +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
ClassName :
+is the name of the class or the class itself to run the check against
self :
className :
#boolean:
+ +