annotate libs/LibStub/LibStub.lua @ 0:ec731d2fe6ba

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