File: classes.lua

package info (click to toggle)
ntopng 5.2.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 121,832 kB
  • sloc: javascript: 143,431; cpp: 71,175; ansic: 11,108; sh: 4,687; makefile: 911; python: 587; sql: 512; pascal: 234; perl: 118; ruby: 52; exp: 4
file content (96 lines) | stat: -rw-r--r-- 3,997 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
--- classes.lua
--
-- The classes library enables simple OOP constructs using prototypes and meta-tables.
--
-- @author Paul Moore
--
-- Copyright (C) 2011 by Strange Ideas Software
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- THE SOFTWARE.

local classes = {}

-- Baseclass of all objects.
classes.Object = {}
classes.Object.class = classes.Object
--- Nullary constructor.
function classes.Object:init (...)
end
--- Base alloc method.
function classes.Object.alloc (mastertable)
   return setmetatable({}, {__index = classes.Object, __newindex = mastertable})
end
--- Base new method.
function classes.Object.new (...)
   return classes.Object.alloc({}):init(...)
end
--- Checks if this object is an instance of class.
-- @param class The class object to check.
-- @return Returns true if this object is an instance of class, false otherwise.
function classes.Object:instanceOf (class)
   -- Recurse up the supertypes until class is found, or until the supertype is not part of the inheritance tree.
   if self.class == class then
      return true
   end
   if self.super then
      return self.super:instanceOf(class)
   end
   return false
end

--- Creates a new class.
-- @param baseclass The Baseclass of this class, or nil.
-- @return A new class reference.
function classes.class (baseclass)
   -- Create the class definition and metatable.
   local classdef = {}
   -- Find the super class, either Object or user-defined.
   baseclass = baseclass or classes.Object
   -- If this class definition does not know of a function, it will 'look up' to the Baseclass via the __index of the metatable.
   setmetatable(classdef, {__index = baseclass})
   -- All class instances have a reference to the class object.
   classdef.class = classdef
   --- Recursivly allocates the inheritance tree of the instance.
   -- @param mastertable The 'root' of the inheritance tree.
   -- @return Returns the instance with the allocated inheritance tree.
   function classdef.alloc (mastertable)
      -- All class instances have a reference to a superclass object.
      local instance = {super = baseclass.alloc(mastertable)}
      -- Any functions this instance does not know of will 'look up' to the superclass definition.
      setmetatable(instance, {__index = classdef, __newindex = mastertable})
      return instance
   end
   --- Constructs a new instance from this class definition.
   -- @param arg Arguments to this class' constructor
   -- @return Returns a new instance of this class.
   function classdef.new (...)
      -- Create the empty object.
      local instance = {}
      -- Start the process of creating the inheritance tree.
      instance.super = baseclass.alloc(instance)
      setmetatable(instance, {__index = classdef})
      -- Finally, init the object, it is up to the programmer to choose to call the super init method.
      instance:init(...)
      return instance
   end
   -- Finally, return the class we created.
   return classdef
end

return classes