annotate Libs/LibStub/LibStub.lua @ 0:fc346da3afd9

First commit Hot Corners standalone.
author tercio
date Fri, 08 Aug 2014 12:35:17 -0300
parents
children c31ee4251181
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