v2.0.2
Instance Methods | Class Methods | Properties | List of all members
CC3Cache Class Reference

#import <CC3Cache.h>

Inheritance diagram for CC3Cache:
Inheritance graph
[legend]

Instance Methods

(void) - addObject:
 
(void) - enumerateObjectsUsingBlock:
 
(id< CC3Cacheable >) - getObjectNamed:
 
(id) - initAsWeakCache:forType:
 
(NSArray *) - objectsSortedByName
 
(void) - removeAllObjects
 
(void) - removeAllObjectsOfType:
 
(void) - removeObject:
 
(void) - removeObjectNamed:
 

Class Methods

(id) + strongCacheForType:
 
(id) + weakCacheForType:
 

Properties

BOOL isWeak
 
NSString * typeName
 

Detailed Description

Instances of CC3Cache hold cachable objects, which are stored and retrieved by name.

Objects must implement the CC3Cacheable protocol to be stored in the cache. Objects in the cache must have a unique name. An assertion error will be raised if an object with the same name exists in the cache already. To replace an object in the cache, you must first remove the existing object from the cache.

CC3Cache implements the NSLocking protocol, and all access to the cache contents is thread-safe.

Each object may be held either strongly or weakly by this cache, depending on the value of the isWeak property at the time the object was added to the cache.

Method Documentation

- (void) addObject: (id< CC3Cacheable >)  obj

Adds the specified object to the cache.

Objects in the cache must have a unique name. An assertion error will be raised if an object with the same name exists in the cache already. To replace an object in the cache, you must first remove the existing object from the cache.

If the value of the isWeak property is YES at the time this method is invoked, this cache will hold a weak reference to the specified object. In this case, the specified object should automatically remove itself from this cache during deallocation, once all external strong references to it have been released, to avoid bad accesses to the cached object after it has been deallocated.

If the value of the isWeak property is NO at the time this method is invoked, this cache will hold a strong reference to the specified object, and it cannot be deallocated until it is specifically removed from this cache.

- (void) enumerateObjectsUsingBlock: (id< CC3Cacheable >)  obj
(BOOL *stop)  block 

Enumerates all of the objects in the cache with the specified block.

The block takes as arguments an object from the cache, and a pointer to a boolean that can be set to YES to stop enumeration.

- (id<CC3Cacheable>) getObjectNamed: (NSString *)  name

Returns the cached object with the specified name, or nil if an object with that name has not been cached.

- (id) initAsWeakCache: (BOOL)  isWeak
forType: (NSString *)  typeName 

Initializes this instance as either a weak or strong cache, for holding objects of the specified content type.

The specified type name is used for logging and reporting informational purposes only. It should be a lower-case descriptive name, such as "resource", "texture", "shader", etc.

If the value of the specified flag is YES, this cache will hold weak references to the objects within the cache, and the presence of the object in the cache will not stop the object from being deallocated. If the value of the specified flag is NO, this cache will hold strong references to the objects within the cache, and the presence of the object in the cache will stop the object from being deallocated.

- (NSArray*) objectsSortedByName

Returns an array of all objects in this cache, sorted by name.

This is a convenience method for use when listing items in the array during logging and development.

Since the objects are retained within the returned array, be careful not to hold on to the array, if you want weakly cached objects to be automatically removed from this cache.

- (void) removeAllObjects

Removes all objects from the cache.

This is typically invoked when 3D capabilities are no longer required, or will not be needed for a significant time, and the app wishes to release 3D resources. This method checks each cache entry, and logs an info message for any object that is weakly cached, as the existence of a weakly-cached entry is an indication that the cached object is being retained somewhere else within the app, and is therefore a potential source of a memory leak.

- (void) removeAllObjectsOfType: (Class)  type

Removes all objects that are instances of the specified class, or a subclass.

This is typically invoked when strongly cached objects are no longer required, or will not be needed for a significant time, and the app wishes to release 3D resources. This method checks each cache entry, and logs an info message for any object that is weakly cached, as the existence of a weakly-cached entry is an indication that the cached object is being retained somewhere else within the app, and is therefore a potential source of a memory leak.

- (void) removeObject: (id< CC3Cacheable >)  obj

Removes the specified object from the cache.

- (void) removeObjectNamed: (NSString *)  name

Removes the object with the specified name from the cache.

+ (id) strongCacheForType: (NSString *)  typeName

Allocates and initializes an empty autoreleased instance holding strong references to the objects within the cache.

The presence of an object in this cache will stop the object from being deallocated.

+ (id) weakCacheForType: (NSString *)  typeName

Allocates and initializes an empty autoreleased instance holding weak references to the objects within the cache.

The presence of an object in this cache will not stop the object from being deallocated.

Property Documentation

- (BOOL) isWeak
readwritenonatomicassign

Indicates whether this cache holds weak references to the objects within.

If the value of this property is YES, this cache will hold a weak reference to each object within the cache, and the presence of the object in the cache will not stop the object from being deallocated. In this case, each object should automatically remove itself from this cache during deallocation, once all external strong references to the object have been released.

This property is set during cache instantiation and initialization, and can be changed at any time, allowing the cache to hold a mix of weakly and strongly cached objects. Whether an object is weakly or strongly cached is determined at the time the object is added to the cached.

- (NSString*) typeName
readnonatomicretain

A descriptive name of the type of object being cached.


The documentation for this class was generated from the following file: