summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/support/lua2dox/examples/lua/class.lua
blob: 7e155ceb05044c27082d376a153ce7828a85529b (plain)
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
---- Copyright 2011 Simon Dales
--
-- This work may be distributed and/or modified under the
-- conditions of the LaTeX Project Public License, either version 1.3
-- of this license or (at your option) any later version.
-- The latest version of this license is in
--   http://www.latex-project.org/lppl.txt
--
-- This work has the LPPL maintenance status `maintained'.
-- 
-- The Current Maintainer of this work is Simon Dales.
--

--[[!
	\file	
	\brief enables classes in lua
	]]
	
--[[ class.lua
-- Compatible with Lua 5.1 (not 5.0).

	---------------------
	
	]]--
--! \brief ``declare'' as class
--! 
--! use as:
--!	\code{lua}
--!	TWibble = class()
--!	function TWibble:init(instance)
--!		self.instance = instance
--!		-- more stuff here
--!	end
--! \endcode
--! 	
function class(BaseClass, ClassInitialiser)
	local newClass = {}    -- a new class newClass
	if not ClassInitialiser and type(BaseClass) == 'function' then
		ClassInitialiser = BaseClass
		BaseClass = nil
	elseif type(BaseClass) == 'table' then
		-- our new class is a shallow copy of the base class!
		for i,v in pairs(BaseClass) do
			newClass[i] = v
		end
		newClass._base = BaseClass
	end
	-- the class will be the metatable for all its newInstanceects,
	-- and they will look up their methods in it.
	newClass.__index = newClass

	-- expose a constructor which can be called by <classname>(<args>)
	local classMetatable = {}
	classMetatable.__call = 
	function(class_tbl, ...)
		local newInstance = {}
		setmetatable(newInstance,newClass)
		--if init then
		--	init(newInstance,...)
		if class_tbl.init then
			class_tbl.init(newInstance,...)
		else 
			-- make sure that any stuff from the base class is initialized!
			if BaseClass and BaseClass.init then
				BaseClass.init(newInstance, ...)
			end
		end
		return newInstance
	end
	newClass.init = ClassInitialiser
	newClass.is_a = 
	function(this, klass)
		local thisMetabable = getmetatable(this)
		while thisMetabable do 
			if thisMetabable == klass then
				return true
			end
			thisMetabable = thisMetabable._base
		end
		return false
	end
	setmetatable(newClass, classMetatable)
	return newClass
end
--eof