Last modified 6 August 2003 by jdavis@math.wisc.edu
Inherits from NSObject
SMKResourceManager is a simple database for shared resources. The resources must be of type SMKResource or one of its subclasses. In particular, it is assumed that the resources are identified by unique numeric keys; the user obtains them through the resource manager by these keys. Only one copy of each resource is maintained by the manager at any given time.
For example, let's say you have a complicated 3D model, to which several parts of your program need access. Each part could load its own copy of the model, but then you'd have redundant copies sitting around. Instead, have each part obtain the model through a common resource manager.
SMKResourceManager has two distinct implementations - one based on SMKSplayTree, and one based on NSMutableDictionary. The splay tree is faster in theory (that is, asymptotically), but you might find the mutable dictionary faster in practice. You pick your implementation at initialization.
- (id)initWithImplementation:(int)implementation;Initializes the receiver with implementation type SMKSplayTreeResourceManager or SMKMutableDictionaryResourceManager.
SMKResourceManager's user interface has only one method, the one that requests a resource by key.
- (id)resourceWithKey:(int)key class:(Class)class;Returns the resource indicated by key. If the resource is not in the resource manager, then this method first creates it by passing class the message resourceWithKey:manager:. class must be a subclass of SMKResource. If creation of the resource fails (for example, because class is nil), then the key is not added to the resource manager, and nil is returned.
Notice that the resources in a resource manager need not be instances of a single class, but there can be only one resource for each key, regardless of class. For example, suppose the first three calls to our manager are as follows:
firstResource = [[myManager resourceWithKey:37 class:[MyOtterResource class]] retain];
secondResource = [[myManager resourceWithKey:100 class:[MyMarmosetResource class]] retain];
thirdResource = [[myManager resourceWithKey:37 class:[MyToadResource class]] retain];
After these invocations, there is one otter in the resource manager (resource 37, with a retain count of 2) and one marmoset (resource 100, with a count of 1). The pointers firstResource and thirdResource are equal. In the third line, resource number 37 was retained; since it was already resident, there was no need to allocate it, and the class MyToadResource was ignored.
To test whether a given key is in the resource manager or not, simply pass a nil class to resourceWithKey:class:. The key is in the manager if and only if a non-nil value is returned.