mirror of
https://github.com/FlightControl-Master/MOOSE.git
synced 2025-08-15 10:47:21 +00:00
Merge pull request #622 from FlightControl-Master/620-IsInstance
620: IsInstance Closes #620
This commit is contained in:
commit
60681d7e23
@ -276,7 +276,10 @@ end
|
|||||||
-- @return #BASE
|
-- @return #BASE
|
||||||
function BASE:GetParent( Child )
|
function BASE:GetParent( Child )
|
||||||
local Parent
|
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
|
Parent = getmetatable( Child.__ ).__index
|
||||||
else
|
else
|
||||||
Parent = getmetatable( Child ).__index
|
Parent = getmetatable( Child ).__index
|
||||||
@ -284,6 +287,63 @@ function BASE:GetParent( Child )
|
|||||||
return Parent
|
return Parent
|
||||||
end
|
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.
|
--- Get the ClassName + ClassID of the class instance.
|
||||||
-- The ClassName + ClassID is formatted as '%s#%09d'.
|
-- The ClassName + ClassID is formatted as '%s#%09d'.
|
||||||
-- @param #BASE self
|
-- @param #BASE self
|
||||||
|
|||||||
@ -33,6 +33,69 @@ FLARECOLOR = trigger.flareColor -- #FLARECOLOR
|
|||||||
-- @type UTILS
|
-- @type UTILS
|
||||||
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
|
--from http://lua-users.org/wiki/CopyTable
|
||||||
UTILS.DeepCopy = function(object)
|
UTILS.DeepCopy = function(object)
|
||||||
|
|||||||
@ -257,6 +257,12 @@
|
|||||||
<td class="name" nowrap="nowrap"><a href="##(BASE).Inherit">BASE:Inherit(Child, Parent)</a></td>
|
<td class="name" nowrap="nowrap"><a href="##(BASE).Inherit">BASE:Inherit(Child, Parent)</a></td>
|
||||||
<td class="summary">
|
<td class="summary">
|
||||||
<p>This is the worker method to inherit from a parent class.</p>
|
<p>This is the worker method to inherit from a parent class.</p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td class="name" nowrap="nowrap"><a href="##(BASE).IsInstanceOf">BASE.IsInstanceOf(ClassName, self, className)</a></td>
|
||||||
|
<td class="summary">
|
||||||
|
<p>This is the worker method to check if an object is an (sub)instance of a class.</p>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
@ -1220,6 +1226,56 @@ is the Parent class that the Child inherits from.</p>
|
|||||||
<p><em><a href="##(BASE)">#BASE</a>:</em>
|
<p><em><a href="##(BASE)">#BASE</a>:</em>
|
||||||
Child</p>
|
Child</p>
|
||||||
|
|
||||||
|
</dd>
|
||||||
|
</dl>
|
||||||
|
<dl class="function">
|
||||||
|
<dt>
|
||||||
|
|
||||||
|
<a id="#(BASE).IsInstanceOf" >
|
||||||
|
<strong>BASE.IsInstanceOf(ClassName, self, className)</strong>
|
||||||
|
</a>
|
||||||
|
</dt>
|
||||||
|
<dd>
|
||||||
|
|
||||||
|
<p>This is the worker method to check if an object is an (sub)instance of a class.</p>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<h3>Examples:</h3>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><p>ZONE:New( 'some zone' ):IsInstanceOf( ZONE ) will return true</p></li>
|
||||||
|
<li><p>ZONE:New( 'some zone' ):IsInstanceOf( 'ZONE' ) will return true</p></li>
|
||||||
|
<li><p>ZONE:New( 'some zone' ):IsInstanceOf( 'zone' ) will return true</p></li>
|
||||||
|
<li><p>ZONE:New( 'some zone' ):IsInstanceOf( 'BASE' ) will return true</p></li>
|
||||||
|
<li><p>ZONE:New( 'some zone' ):IsInstanceOf( 'GROUP' ) will return false</p></li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
|
||||||
|
<h3>Parameters</h3>
|
||||||
|
<ul>
|
||||||
|
<li>
|
||||||
|
|
||||||
|
<p><code><em> ClassName </em></code>:
|
||||||
|
is the name of the class or the class itself to run the check against</p>
|
||||||
|
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
|
||||||
|
<p><code><em> self </em></code>: </p>
|
||||||
|
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
|
||||||
|
<p><code><em> className </em></code>: </p>
|
||||||
|
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
<h3>Return value</h3>
|
||||||
|
|
||||||
|
<p><em>#boolean:</em></p>
|
||||||
|
|
||||||
|
|
||||||
</dd>
|
</dd>
|
||||||
</dl>
|
</dl>
|
||||||
<dl class="function">
|
<dl class="function">
|
||||||
|
|||||||
@ -236,12 +236,41 @@ which are excellent tools to be reused in an OO environment!.</p>
|
|||||||
<td class="name" nowrap="nowrap"><a href="##(UTILS).FeetToMeters">UTILS.FeetToMeters(feet)</a></td>
|
<td class="name" nowrap="nowrap"><a href="##(UTILS).FeetToMeters">UTILS.FeetToMeters(feet)</a></td>
|
||||||
<td class="summary">
|
<td class="summary">
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td class="name" nowrap="nowrap"><a href="##(UTILS).IsInstanceOf">UTILS.IsInstanceOf(object, className)</a></td>
|
||||||
|
<td class="summary">
|
||||||
|
<p>Function to infer instance of an object</p>
|
||||||
|
|
||||||
|
<h3>Examples:</h3>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><p>UTILS.IsInstanceOf( 'some text', 'string' ) will return true</p></li>
|
||||||
|
<li><p>UTILS.IsInstanceOf( some_function, 'function' ) will return true</p></li>
|
||||||
|
<li><p>UTILS.IsInstanceOf( 10, 'number' ) will return true</p></li>
|
||||||
|
<li><p>UTILS.IsInstanceOf( false, 'boolean' ) will return true</p></li>
|
||||||
|
<li><p>UTILS.IsInstanceOf( nil, 'nil' ) will return true</p></li>
|
||||||
|
<li><p>UTILS.IsInstanceOf( ZONE:New( 'some zone', ZONE ) will return true</p></li>
|
||||||
|
<li><p>UTILS.IsInstanceOf( ZONE:New( 'some zone', 'ZONE' ) will return true</p></li>
|
||||||
|
<li><p>UTILS.IsInstanceOf( ZONE:New( 'some zone', 'zone' ) will return true</p></li>
|
||||||
|
<li><p>UTILS.IsInstanceOf( ZONE:New( 'some zone', 'BASE' ) will return true</p></li>
|
||||||
|
<li><p>UTILS.IsInstanceOf( ZONE:New( 'some zone', 'GROUP' ) will return false</p></li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td class="name" nowrap="nowrap"><a href="##(UTILS).KmphToMps">UTILS.KmphToMps(kmph)</a></td>
|
<td class="name" nowrap="nowrap"><a href="##(UTILS).KmphToMps">UTILS.KmphToMps(kmph)</a></td>
|
||||||
<td class="summary">
|
<td class="summary">
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td class="name" nowrap="nowrap"><a href="##(UTILS).KnotsToKmph">UTILS.KnotsToKmph(knots)</a></td>
|
||||||
|
<td class="summary">
|
||||||
|
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
@ -608,6 +637,56 @@ use negative idp for rounding ahead of decimal place, positive for rounding afte
|
|||||||
<dl class="function">
|
<dl class="function">
|
||||||
<dt>
|
<dt>
|
||||||
|
|
||||||
|
<a id="#(UTILS).IsInstanceOf" >
|
||||||
|
<strong>UTILS.IsInstanceOf(object, className)</strong>
|
||||||
|
</a>
|
||||||
|
</dt>
|
||||||
|
<dd>
|
||||||
|
|
||||||
|
<p>Function to infer instance of an object</p>
|
||||||
|
|
||||||
|
<h3>Examples:</h3>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><p>UTILS.IsInstanceOf( 'some text', 'string' ) will return true</p></li>
|
||||||
|
<li><p>UTILS.IsInstanceOf( some_function, 'function' ) will return true</p></li>
|
||||||
|
<li><p>UTILS.IsInstanceOf( 10, 'number' ) will return true</p></li>
|
||||||
|
<li><p>UTILS.IsInstanceOf( false, 'boolean' ) will return true</p></li>
|
||||||
|
<li><p>UTILS.IsInstanceOf( nil, 'nil' ) will return true</p></li>
|
||||||
|
<li><p>UTILS.IsInstanceOf( ZONE:New( 'some zone', ZONE ) will return true</p></li>
|
||||||
|
<li><p>UTILS.IsInstanceOf( ZONE:New( 'some zone', 'ZONE' ) will return true</p></li>
|
||||||
|
<li><p>UTILS.IsInstanceOf( ZONE:New( 'some zone', 'zone' ) will return true</p></li>
|
||||||
|
<li><p>UTILS.IsInstanceOf( ZONE:New( 'some zone', 'BASE' ) will return true</p></li>
|
||||||
|
<li><p>UTILS.IsInstanceOf( ZONE:New( 'some zone', 'GROUP' ) will return false</p></li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<h3>Parameters</h3>
|
||||||
|
<ul>
|
||||||
|
<li>
|
||||||
|
|
||||||
|
<p><code><em> object </em></code>:
|
||||||
|
is the object to be evaluated</p>
|
||||||
|
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
|
||||||
|
<p><code><em> className </em></code>:
|
||||||
|
is the name of the class to evaluate (can be either a string or a Moose class)</p>
|
||||||
|
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
<h3>Return value</h3>
|
||||||
|
|
||||||
|
<p><em>#boolean:</em></p>
|
||||||
|
|
||||||
|
|
||||||
|
</dd>
|
||||||
|
</dl>
|
||||||
|
<dl class="function">
|
||||||
|
<dt>
|
||||||
|
|
||||||
<a id="#(UTILS).KmphToMps" >
|
<a id="#(UTILS).KmphToMps" >
|
||||||
<strong>UTILS.KmphToMps(kmph)</strong>
|
<strong>UTILS.KmphToMps(kmph)</strong>
|
||||||
</a>
|
</a>
|
||||||
@ -629,6 +708,27 @@ use negative idp for rounding ahead of decimal place, positive for rounding afte
|
|||||||
<dl class="function">
|
<dl class="function">
|
||||||
<dt>
|
<dt>
|
||||||
|
|
||||||
|
<a id="#(UTILS).KnotsToKmph" >
|
||||||
|
<strong>UTILS.KnotsToKmph(knots)</strong>
|
||||||
|
</a>
|
||||||
|
</dt>
|
||||||
|
<dd>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<h3>Parameter</h3>
|
||||||
|
<ul>
|
||||||
|
<li>
|
||||||
|
|
||||||
|
<p><code><em> knots </em></code>: </p>
|
||||||
|
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</dd>
|
||||||
|
</dl>
|
||||||
|
<dl class="function">
|
||||||
|
<dt>
|
||||||
|
|
||||||
<a id="#(UTILS).KnotsToMps" >
|
<a id="#(UTILS).KnotsToMps" >
|
||||||
<strong>UTILS.KnotsToMps(knots)</strong>
|
<strong>UTILS.KnotsToMps(knots)</strong>
|
||||||
</a>
|
</a>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user