annotate Details_RaidInfo-SiegeOfOrgrimmar/Libs/LibStub/LibStub.lua @ 0:0de01324b4f2

first commit
author Tercio
date Tue, 23 Jun 2015 15:26:28 -0300
parents
children
rev   line source
Tercio@0 1 -- $Id: LibStub.lua 76 2007-09-03 01:50:17Z mikk $
Tercio@0 2 -- LibStub is a simple versioning stub meant for use in Libraries. http://www.wowace.com/wiki/LibStub for more info
Tercio@0 3 -- LibStub is hereby placed in the Public Domain
Tercio@0 4 -- Credits: Kaelten, Cladhaire, ckknight, Mikk, Ammo, Nevcairiel, joshborke
Tercio@0 5 local LIBSTUB_MAJOR, LIBSTUB_MINOR = "LibStub", 2 -- NEVER MAKE THIS AN SVN REVISION! IT NEEDS TO BE USABLE IN ALL REPOS!
Tercio@0 6 local LibStub = _G[LIBSTUB_MAJOR]
Tercio@0 7
Tercio@0 8 -- Check to see is this version of the stub is obsolete
Tercio@0 9 if not LibStub or LibStub.minor < LIBSTUB_MINOR then
Tercio@0 10 LibStub = LibStub or {libs = {}, minors = {} }
Tercio@0 11 _G[LIBSTUB_MAJOR] = LibStub
Tercio@0 12 LibStub.minor = LIBSTUB_MINOR
Tercio@0 13
Tercio@0 14 -- LibStub:NewLibrary(major, minor)
Tercio@0 15 -- major (string) - the major version of the library
Tercio@0 16 -- minor (string or number ) - the minor version of the library
Tercio@0 17 --
Tercio@0 18 -- returns nil if a newer or same version of the lib is already present
Tercio@0 19 -- returns empty library object or old library object if upgrade is needed
Tercio@0 20 function LibStub:NewLibrary(major, minor)
Tercio@0 21 assert(type(major) == "string", "Bad argument #2 to `NewLibrary' (string expected)")
Tercio@0 22 minor = assert(tonumber(strmatch(minor, "%d+")), "Minor version must either be a number or contain a number.")
Tercio@0 23
Tercio@0 24 local oldminor = self.minors[major]
Tercio@0 25 if oldminor and oldminor >= minor then return nil end
Tercio@0 26 self.minors[major], self.libs[major] = minor, self.libs[major] or {}
Tercio@0 27 return self.libs[major], oldminor
Tercio@0 28 end
Tercio@0 29
Tercio@0 30 -- LibStub:GetLibrary(major, [silent])
Tercio@0 31 -- major (string) - the major version of the library
Tercio@0 32 -- silent (boolean) - if true, library is optional, silently return nil if its not found
Tercio@0 33 --
Tercio@0 34 -- throws an error if the library can not be found (except silent is set)
Tercio@0 35 -- returns the library object if found
Tercio@0 36 function LibStub:GetLibrary(major, silent)
Tercio@0 37 if not self.libs[major] and not silent then
Tercio@0 38 error(("Cannot find a library instance of %q."):format(tostring(major)), 2)
Tercio@0 39 end
Tercio@0 40 return self.libs[major], self.minors[major]
Tercio@0 41 end
Tercio@0 42
Tercio@0 43 -- LibStub:IterateLibraries()
Tercio@0 44 --
Tercio@0 45 -- Returns an iterator for the currently registered libraries
Tercio@0 46 function LibStub:IterateLibraries()
Tercio@0 47 return pairs(self.libs)
Tercio@0 48 end
Tercio@0 49
Tercio@0 50 setmetatable(LibStub, { __call = LibStub.GetLibrary })
Tercio@0 51 end