From 89b94d6a9671afa6bf42d23329730ff654d1a609 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 27 Jan 2019 20:32:02 +0100 Subject: [PATCH] src: add classic as a library --- sonic-radiance.love/libs/classic.lua | 68 ++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 sonic-radiance.love/libs/classic.lua diff --git a/sonic-radiance.love/libs/classic.lua b/sonic-radiance.love/libs/classic.lua new file mode 100644 index 0000000..cbd6f81 --- /dev/null +++ b/sonic-radiance.love/libs/classic.lua @@ -0,0 +1,68 @@ +-- +-- classic +-- +-- Copyright (c) 2014, rxi +-- +-- This module is free software; you can redistribute it and/or modify it under +-- the terms of the MIT license. See LICENSE for details. +-- + + +local Object = {} +Object.__index = Object + + +function Object:new() +end + + +function Object:extend() + local cls = {} + for k, v in pairs(self) do + if k:find("__") == 1 then + cls[k] = v + end + end + cls.__index = cls + cls.super = self + setmetatable(cls, self) + return cls +end + + +function Object:implement(...) + for _, cls in pairs({...}) do + for k, v in pairs(cls) do + if self[k] == nil and type(v) == "function" then + self[k] = v + end + end + end +end + + +function Object:is(T) + local mt = getmetatable(self) + while mt do + if mt == T then + return true + end + mt = getmetatable(mt) + end + return false +end + + +function Object:__tostring() + return "Object" +end + + +function Object:__call(...) + local obj = setmetatable({}, self) + obj:new(...) + return obj +end + + +return Object