lotrointerface.com
Search Downloads

LoTROInterface SVN KragenBars

[/] [branches/] [3.10/] [Utils/] [Class.lua] - Blame information for rev 15

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 6 Kragenwar-3044
-- Class.lua
2 Kragenwar-3044
-- Compatible with Lua 5.1 (not 5.0).
3 Kragenwar-3044
--
4 Kragenwar-3044
-- Modified to use a metatable to reference base objects instead
5 Kragenwar-3044
-- or performing shallow copies. This means smaller classes that
6 Kragenwar-3044
-- are a bit more dynamic at the expence of some performance.
7 Kragenwar-3044
_G.class = function( base )
8 Kragenwar-3044
        if ( base ~= nil ) then
9 Kragenwar-3044
                local baseType = type( base );
10 Kragenwar-3044
 
11 Kragenwar-3044
                if ( baseType ~= 'table' ) then
12 Kragenwar-3044
                        error( "Base class is not a table. Base class was a " .. baseType );
13 Kragenwar-3044
                end
14 Kragenwar-3044
        end
15 Kragenwar-3044
 
16 Kragenwar-3044
        -- The class definition table. Contains all of the actual class
17 Kragenwar-3044
        -- methods, constructor, and an IsA function automatically
18 Kragenwar-3044
        -- provided.
19 Kragenwar-3044
        local c  = { };
20 Kragenwar-3044
 
21 Kragenwar-3044
        c.base = base;
22 Kragenwar-3044
        c.IsA = function( self, klass )
23 Kragenwar-3044
                local m = getmetatable( self );
24 Kragenwar-3044
 
25 Kragenwar-3044
                while m do
26 Kragenwar-3044
                        if m == klass then
27 Kragenwar-3044
                                return true;
28 Kragenwar-3044
                        end
29 Kragenwar-3044
 
30 Kragenwar-3044
                        m = m.base;
31 Kragenwar-3044
                end
32 Kragenwar-3044
 
33 Kragenwar-3044
                return false;
34 Kragenwar-3044
        end
35 Kragenwar-3044
 
36 Kragenwar-3044
        -- The class definition metatable. This is used to hook up
37 Kragenwar-3044
        -- base classes and then to register a call handler that is
38 Kragenwar-3044
        -- used to construct new instances of the class.
39 Kragenwar-3044
        local mt = { };
40 Kragenwar-3044
        mt.__index = base;
41 Kragenwar-3044
 
42 Kragenwar-3044
        mt.__call = function( class_tbl, ... )
43 Kragenwar-3044
                if ( class_tbl.Abstract ) then
44 Kragenwar-3044
                        error "Abstract class cannot be constructed.";
45 Kragenwar-3044
                end
46 Kragenwar-3044
 
47 Kragenwar-3044
                -- Create the new class instance.
48 Kragenwar-3044
                local instance = { };
49 Kragenwar-3044
                setmetatable( instance, { __index = c } );
50 Kragenwar-3044
 
51 Kragenwar-3044
                -- Invoke the constructor if it exists.
52 Kragenwar-3044
                if ( ( instance.Constructor ~= nil ) and ( type( instance.Constructor ) == 'function' ) ) then
53 Kragenwar-3044
                        instance:Constructor( ... );
54 Kragenwar-3044
                end
55 Kragenwar-3044
 
56 Kragenwar-3044
                return instance;
57 Kragenwar-3044
        end
58 Kragenwar-3044
 
59 Kragenwar-3044
        setmetatable( c, mt );
60 Kragenwar-3044
        return c;
61 Kragenwar-3044
end

All times are GMT -5. The time now is 01:38 AM.


Our Network
EQInterface | EQ2Interface | Minion | WoWInterface | ESOUI | LoTROInterface | MMOUI | Swtorui