0a41ac04d56ccc96491989dc71d9875cd804fc6b
[wowui.git] / libs / AceGUI-3.0-SharedMediaWidgets / Libs / LibStub / LibStub.lua
1 -- LibStub is a simple versioning stub meant for use in Libraries.  http://www.wowace.com/wiki/LibStub for more info
2 -- LibStub is hereby placed in the Public Domain Credits: Kaelten, Cladhaire, ckknight, Mikk, Ammo, Nevcairiel, joshborke
3 local LIBSTUB_MAJOR, LIBSTUB_MINOR = "LibStub", 2  -- NEVER MAKE THIS AN SVN REVISION! IT NEEDS TO BE USABLE IN ALL REPOS!
4 local LibStub = _G[LIBSTUB_MAJOR]
5
6 if not LibStub or LibStub.minor < LIBSTUB_MINOR then
7         LibStub = LibStub or {libs = {}, minors = {} }
8         _G[LIBSTUB_MAJOR] = LibStub
9         LibStub.minor = LIBSTUB_MINOR
10         
11         function LibStub:NewLibrary(major, minor)
12                 assert(type(major) == "string", "Bad argument #2 to `NewLibrary' (string expected)")
13                 minor = assert(tonumber(strmatch(minor, "%d+")), "Minor version must either be a number or contain a number.")
14                 
15                 local oldminor = self.minors[major]
16                 if oldminor and oldminor >= minor then return nil end
17                 self.minors[major], self.libs[major] = minor, self.libs[major] or {}
18                 return self.libs[major], oldminor
19         end
20         
21         function LibStub:GetLibrary(major, silent)
22                 if not self.libs[major] and not silent then
23                         error(("Cannot find a library instance of %q."):format(tostring(major)), 2)
24                 end
25                 return self.libs[major], self.minors[major]
26         end
27         
28         function LibStub:IterateLibraries() return pairs(self.libs) end
29         setmetatable(LibStub, { __call = LibStub.GetLibrary })
30 end