Luaoop
Luaoop is a library to do OOP (Object Oriented Programming) which aims to be simple, powerful and optimized (especially for LuaJIT).
Look at the examples for more understanding of the library.
Install
API
-- create a new class
-- name: identifier for debugging purpose
-- ...: base classes (single/multiple inheritance)
-- return created class
class.new(name, ...)
-- SHORTCUT class(name, ...)
-- t: class or instance
-- return class name or nil
class.name(t)
-- t: class or instance
-- return the type (class) or nil
class.type(t)
-- check if an instance/class is/inherits from a specific class
-- t: class or instance
-- classdef: class
-- return true or nil
class.is(t, classdef)
-- return instance/class types map (type => true)
-- t: class or instance
-- return types map (type => true) or nil
class.types(t)
-- get the class metatable applied to the instances
-- useful to apply class behaviour to a custom table
-- will build the class if not already built
-- classdef: class
-- return meta or nil
class.meta(classdef)
-- create instance
-- classdef: class
-- ...: constructor arguments
class.instantiate(classdef, ...)
-- SHORTCUT Class(...)
-- build class
-- will build/re-build the class
-- (if a class is not already built, when used for inheritance or instantiation this function is called)
-- classdef: class
class.build(classdef)
-- get operator
-- lhs: instance
-- name: full name of the operator (starting with "__")
-- rhs: any value, can be nil for unary operators
-- no_error: if passed/true, will not trigger an error if no operator was found
class.getop(lhs, name, rhs, no_error)
Inheritance
-
single and multiple inheritance
-
all properties are inherited for classes
-
all properties except specials (starting with
__
) are inherited for instances
|
Special tables inherit from bases' special tables. |
|
In case of multiple inheritance with functions with the same name, one will be taken arbitrarily. This issue can be solved by accessing directly a specific parent method/function using the class definition. |
Inheritance is not dynamic (cached) and is built by class.build
. class.build
must be called for any later changes to the class definition or base classes (in order, build the dependencies, then build the class, then build the derived classes).
|
This function is already called when using a class for inheritance or instantiation for the first time; in most cases calling this function is not needed because the class is completely defined when used. |
A = class("A")
function A:test()
print("a")
end
B = class("B")
function B:test()
print("b")
end
C = class("C", A, B) -- inheritance from A and B
function C:test() -- force the usage of B:test()
B.test(self)
end
A = class("A")
function A:__construct()
print("A")
end
B = class("B", A)
function B:__construct()
A.__construct(self) -- call parent (A) constructor
print("B")
end
Special methods
Special methods for a class can be defined; they will be inherited (for a class) the same way other properties are.
Each special method is prefixed by __
.
|
They are not metamethods, they are named like this to keep consistency with the Lua notation. |
Misc
construct |
called at initialization |
destruct |
called at garbage collection |
Operators
Operators can be defined like this:
function Object:__op() end -- unary
Object.__op[rhs] = function(self, rhs) end -- binary
|
rhs can be a class or a Lua type (as string).
|
call |
like the metamethod |
tostring |
like the metamethod |
unm |
like the metamethod |
concat |
like the metamethod (no order, but has a second parameter "inverse" when the concat is not forward) |
add |
like the metamethod (no order) |
sub |
like the metamethod (can be omitted if |
mul |
like the metamethod (no order) |
div |
like the metamethod |
mod |
like the metamethod |
pow |
like the metamethod |
eq |
like the metamethod (doesn’t throw an error if the operator is missing, will be false by default) |
le |
like the metamethod |
lt |
like the metamethod |
|
Comparison of different instances with different types is possible, but this may change in the future. |
Private / Protected
There are no private/protected mechanisms in Luaoop.
local function pmethod(self)
end
local privates = setmetatable({}, {__mode = "k"})
function Object:__construct()
privates[self] = { a = 1, b = 2 }
end
function Object:method()
local p = privates[self]
p.a = p.a*p.b
end
Behavior transfer
It’s possible to give Luaoop class and instance behavior to any object by adding the luaoop
property (a table) to its metatable (and set some metamethods).
Class behavior
|
class.new will check (and build if not built) base classes and initialize class special tables. It is easier to use this function and copy/modify the metatable afterwards.
|
name |
class name |
bases |
list of base classes |
Optional build hooks can be added to customize some parts of the build process, they are functions starting with __
.
postbuild(class, build) |
used to add more properties to the build, called after the base classes inheritance process |
||
postmeta(class, meta) |
used to modify the built instance metatable, called at the end of the build process |
||
instantiate(class, …) |
used to replace the default instantiate behavior, should return a valid new Luaoop instance (
|
build |
table containing inherited properties and special tables for the class (not self) |
||||||||||||||||||||||||||||||||||
instance_build |
table containing inherited class properties without specials |
||||||||||||||||||||||||||||||||||
types |
map of type (class) ⇒ true |
||||||||||||||||||||||||||||||||||
meta |
metatable built used for the instances Base properties
Metamethods
|
|
It’s easier to let these properties being created by class.build and just implement the build hooks.
|
call |
shortcut for |
tostring |
for regular classes, will print |
index |
inherits from the |
Instance behavior
The instance behavior is set using the meta
built metatable. In some cases if the class has a destructor, this metatable could be replaced by a copy (not deep) to have custom instance fields.
|
The new metatable would be marked as custom with a luaoop.custom boolean property set to true.
|