summaryrefslogtreecommitdiff
path: root/xmake/core/base/singleton.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2018-12-29 22:45:04 +0800
committerruki <[email protected]>2018-12-29 22:45:04 +0800
commitd563d8d01f7f2aa1daa6e177dfe483e52ac786f8 (patch)
treec4a0bd3112911d98e6c94a30195399c307721198 /xmake/core/base/singleton.lua
parent5a516124fe04f2526cadebf0cc6c96057d9af553 (diff)
uses singleton for platform
Diffstat (limited to 'xmake/core/base/singleton.lua')
-rw-r--r--xmake/core/base/singleton.lua22
1 files changed, 16 insertions, 6 deletions
diff --git a/xmake/core/base/singleton.lua b/xmake/core/base/singleton.lua
index 4579cf6fe..ed90286ab 100644
--- a/xmake/core/base/singleton.lua
+++ b/xmake/core/base/singleton.lua
@@ -29,7 +29,12 @@ local singleton = singleton or {}
--
-- e.g.
--
--- local instance = singleton.get("key")
+-- function get_xxx()
+-- return {xxx = 1}
+-- end
+-- local instance = singleton.get("key", get_xxx)
+--
+-- Or
--
-- function get_xxx()
--
@@ -44,7 +49,7 @@ local singleton = singleton or {}
-- return instance
-- end
--
-function singleton.get(key)
+function singleton.get(key, init)
-- get key
key = tostring(key)
@@ -57,11 +62,16 @@ function singleton.get(key)
local instance = instances[key]
if instance == nil then
- -- mark as not inited
- inited = false
-
-- init instance
- instance = {}
+ if init then
+ instance = init()
+ else
+ -- mark as not inited
+ inited = false
+
+ -- init instance
+ instance = {}
+ end
-- save this instance
instances[key] = instance