From b798ee0b76d1b35ee39e5e9727815f4c9b1b15ac Mon Sep 17 00:00:00 2001 From: Vanic Date: Wed, 3 Sep 2025 19:07:08 +0000 Subject: [PATCH] Enum: create Enum class --- mods/CORE/Enum/init.lua | 5 +++++ mods/CORE/Enum/mod.conf | 2 ++ mods/CORE/Enum/src/Enum.lua | 30 ++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 mods/CORE/Enum/init.lua create mode 100644 mods/CORE/Enum/mod.conf create mode 100644 mods/CORE/Enum/src/Enum.lua diff --git a/mods/CORE/Enum/init.lua b/mods/CORE/Enum/init.lua new file mode 100644 index 0000000..52fd2af --- /dev/null +++ b/mods/CORE/Enum/init.lua @@ -0,0 +1,5 @@ +local Enum = Mod:new() + +Enum.Class = Enum.require('Enum') + +Api.addModToGlobalSpace(Enum, 'Core.Enum') diff --git a/mods/CORE/Enum/mod.conf b/mods/CORE/Enum/mod.conf new file mode 100644 index 0000000..1d7d764 --- /dev/null +++ b/mods/CORE/Enum/mod.conf @@ -0,0 +1,2 @@ +name = smc__core__enum +description = smc__api__system diff --git a/mods/CORE/Enum/src/Enum.lua b/mods/CORE/Enum/src/Enum.lua new file mode 100644 index 0000000..fc6503c --- /dev/null +++ b/mods/CORE/Enum/src/Enum.lua @@ -0,0 +1,30 @@ +---Abstract class implementing enumeration. It is a table. Read-only table! +---@abstract +---@class Enum +local Enum = {} + +---@param enum table[] +---@return Enum +function Enum:new(enum) + local metaEnum = {} + + -- Create table + for _, v in ipairs(enum) do + metaEnum[v] = v + end + + ---@type Enum + instance = setmetatable({}, { + __index = metaEnum, + __newindex = function() + error('The Enum table is read only!') + end, + __metatable = false + }) + + return instance +end + +---@alias Enum.EnumKey string Key from enumeration + +return Enum \ No newline at end of file