The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.

Sample Code  0.1 20120518
class.lua
Go to the documentation of this file.
1 ---- Copyright 2011 Simon Dales
2 --
3 -- This work may be distributed and/or modified under the
4 -- conditions of the LaTeX Project Public License, either version 1.3
5 -- of this license or (at your option) any later version.
6 -- The latest version of this license is in
7 -- http://www.latex-project.org/lppl.txt
8 --
9 -- This work has the LPPL maintenance status `maintained'.
10 --
11 -- The Current Maintainer of this work is Simon Dales.
12 --
13 
14 --[[!
15  \file
16  \brief enables classes in lua
17  ]]
18 
19 --[[ class.lua
20 -- Compatible with Lua 5.1 (not 5.0).
21 
22  ---------------------
23 
24  ]]--
25 --! \brief ``declare'' as class
26 --!
27 --! use as:
28 --! \code{lua}
29 --! TWibble = class()
30 --! function TWibble.init(instance)
31 --! self.instance = instance
32 --! -- more stuff here
33 --! end
34 --! \endcode
35 --!
36 function class(BaseClass, ClassInitialiser)
37  local newClass = {} -- a new class newClass
38  if not ClassInitialiser and type(BaseClass) == 'function' then
39  ClassInitialiser = BaseClass
40  BaseClass = nil
41  elseif type(BaseClass) == 'table' then
42  -- our new class is a shallow copy of the base class!
43  for i,v in pairs(BaseClass) do
44  newClass[i] = v
45  end
46  newClass._base = BaseClass
47  end
48  -- the class will be the metatable for all its newInstanceects,
49  -- and they will look up their methods in it.
50  newClass.__index = newClass
51 
52  -- expose a constructor which can be called by <classname>(<args>)
53  local classMetatable = {}
54  classMetatable.__call =
55  function(class_tbl, ...)
56  local newInstance = {}
57  setmetatable(newInstance,newClass)
58  --if init then
59  -- init(newInstance,...)
60  if class_tbl.init then
61  class_tbl.init(newInstance,...)
62  else
63  -- make sure that any stuff from the base class is initialized!
64  if BaseClass and BaseClass.init then
65  BaseClass.init(newInstance, ...)
66  end
67  end
68  return newInstance
69  end
70  newClass.init = ClassInitialiser
71  newClass.is_a =
72  function(this, klass)
73  local thisMetabable = getmetatable(this)
74  while thisMetabable do
75  if thisMetabable == klass then
76  return true
77  end
78  thisMetabable = thisMetabable._base
79  end
80  return false
81  end
82  setmetatable(newClass, classMetatable)
83  return newClass
84 end
85 --eof

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.