Skip to content

TeddyEngel/Luoop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Luoop

A lightweight and flexible object-oriented library for Lua.

License Lua

Features

  • Simple class definition — Single class() function for all OOP needs
  • Constructors & destructorsinit and destroy lifecycle methods
  • Single inheritance — Extend classes with automatic method inheritance
  • Multiple inheritance — Inherit from multiple parent classes
  • Method overloading — Override parent methods in child classes
  • Parent method access — Call parent methods via _parentMethod()
  • Ad-hoc parent calls — Pass custom parameters to parent constructors/destructors
  • Singleton support — Built-in singleton pattern with newSingleton()
  • Instance cloning — Create new instances from existing objects with newInstance()

Installation

Copy luoop.lua to your project and require it:

require("luoop")

Quick Start

Basic Class

require("luoop")

-- Define constructor
local function new(self, name)
    self.name = name
end

-- Create class
Person = class(new)

-- Add methods
function Person:greet()
    return "Hello, I'm " .. self.name
end

function Person:destroy()
    self.name = nil
end

-- Usage
local john = Person("John")
print(john:greet())  -- "Hello, I'm John"
john:destroy()

Inheritance

require("luoop")

-- Parent class
local function newAnimal(self, species)
    self.species = species
end

Animal = class(newAnimal)

function Animal:speak()
    return "..."
end

function Animal:destroy() end

-- Child class
local function newDog(self, name)
    self:_parentConstructor(Animal, "Canine")  -- Call parent constructor
    self.name = name
end

Dog = class(newDog, Animal)  -- Inherit from Animal

function Dog:speak()  -- Override parent method
    return "Woof!"
end

function Dog:introduce()
    return self.name .. " the " .. self.species
end

function Dog:destroy()
    self:_parentDestructor(Animal)
end

-- Usage
local rex = Dog("Rex")
print(rex:speak())      -- "Woof!"
print(rex:introduce())  -- "Rex the Canine"
rex:destroy()

Multiple Inheritance

require("luoop")

-- Parent classes
Swimmer = class(function(self) self.canSwim = true end)
function Swimmer:swim() return "Swimming!" end
function Swimmer:destroy() end

Flyer = class(function(self) self.canFly = true end)
function Flyer:fly() return "Flying!" end
function Flyer:destroy() end

-- Child inherits from both
local function newDuck(self)
    self:_parentConstructor(Swimmer)
    self:_parentConstructor(Flyer)
end

Duck = class(newDuck, Swimmer, Flyer)

function Duck:destroy()
    self:_parentDestructor(Swimmer)
    self:_parentDestructor(Flyer)
end

-- Usage
local donald = Duck()
print(donald:swim())    -- "Swimming!"
print(donald:fly())     -- "Flying!"
print(donald.canSwim)   -- true
print(donald.canFly)    -- true

Singleton

require("luoop")

local function newConfig(self)
    self.debug = false
end

Config = class(newConfig)
Config:enableSingleton()

function Config:destroy() end

-- Usage
local config1 = Config.newSingleton()
local config2 = Config.newSingleton()  -- Same instance as config1

config1.debug = true
print(config2.debug)  -- true (same object)

Config.destroySingleton()  -- Clean up

API Reference

Class Creation

Function Description
class(init, ...) Create a new class with constructor init and optional parent classes

Instance Methods

Method Description
ClassName(...) Create new instance (calls init)
obj:destroy(...) Destructor (implement in your class)
obj:newInstance(...) Create new instance from existing object

Parent Class Methods

Method Description
obj:_parentConstructor(Parent, ...) Call parent's constructor with arguments
obj:_parentDestructor(Parent, ...) Call parent's destructor with arguments
obj:_parentMethod(Parent, "methodName", ...) Call parent's method
obj:_hasParentClass(Parent) Check if class inherits from Parent

Singleton Methods

Method Description
ClassName:enableSingleton() Enable singleton mode for class
ClassName:disableSingleton() Disable singleton mode (before instantiation)
ClassName:isSingleton() Check if class is singleton
ClassName.newSingleton(...) Get or create singleton instance
ClassName.destroySingleton(...) Destroy singleton instance

Examples

See the example/ folder for complete working examples:

Related Projects

  • Loogine — A game engine built on Luoop

License

MIT License — see LICENSE for details.

About

Easy and flexible object orientated library for Lua

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages