Fix look-ups of plug-in instances.
authorPeter Hosey <hg@boredzo.org>
Wed Oct 07 03:20:21 2009 -0700 (2009-10-07)
changeset 44904e571142d4ce
parent 4489 9c0b9f927d0e
child 4491 5e2b01d86037
Fix look-ups of plug-in instances.

Since we're putting mutable objects (mutable dictionaries) into this collection, we need to make the collection an NSHashTable using NSHashTableObjectPointerPersonality, which uses the objects' pointers (duly shifted) as their hashes, instead of an NSMutableSet, which would ask the mutable objects for their hashes (which tend to change when something mutates the object).
Core/Source/GrowlPluginController.m
     1.1 --- a/Core/Source/GrowlPluginController.m	Sat Oct 03 23:26:34 2009 -0700
     1.2 +++ b/Core/Source/GrowlPluginController.m	Wed Oct 07 03:20:21 2009 -0700
     1.3 @@ -326,6 +326,14 @@
     1.4  
     1.5  #pragma mark -
     1.6  
     1.7 +//Private method.
     1.8 +//This creates and returns a hash table that uses the object's pointer for the hash, instead of sending it a -hash message. This enables us to mutate the object without disturbing its hash.
     1.9 +- (NSHashTable *) makeHashTableWithObject:(NSObject *)obj {
    1.10 +	NSHashTable *hashTable = [NSHashTable hashTableWithOptions:NSHashTableStrongMemory | NSHashTableObjectPointerPersonality];
    1.11 +	[hashTable addObject:obj];
    1.12 +	return hashTable;
    1.13 +}
    1.14 +
    1.15  //private method.
    1.16  - (NSDictionary *) addPluginInstance:(GrowlPlugin *)plugin fromPath:(NSString *)path bundle:(NSBundle *)bundle {
    1.17  	//If we're passed a bundle, refuse to load it if we've already loaded a different bundle with the same identifier, instead returning whatever dictionary we already have.
    1.18 @@ -465,11 +473,11 @@
    1.19  
    1.20  	#define ADD_TO_DICT(dictName, key, value)                                          \
    1.21  			do {                                                                        \
    1.22 -				NSMutableSet *plugins = [dictName objectForKey:key];                     \
    1.23 +				NSHashTable *plugins = [dictName objectForKey:key];                     \
    1.24  				if (plugins)                                                              \
    1.25  					[plugins addObject:value];                                             \
    1.26  				else                                                                        \
    1.27 -					[dictName setObject:[NSMutableSet setWithObject:value] forKey:key];      \
    1.28 +					[dictName setObject:[self makeHashTableWithObject:value] forKey:key];    \
    1.29  			} while(0)
    1.30  		ADD_TO_DICT(pluginsByName,     name,                     pluginDict);
    1.31  		ADD_TO_DICT(pluginsByAuthor,   author,                   pluginDict);
    1.32 @@ -588,13 +596,13 @@
    1.33  
    1.34  	if ([matches count]) {
    1.35  		if (name)
    1.36 -			[matches intersectSet:[pluginsByName objectForKey:name]];
    1.37 +			[matches intersectSet:[[pluginsByName objectForKey:name] setRepresentation]];
    1.38  		if (author)
    1.39 -			[matches intersectSet:[pluginsByAuthor objectForKey:author]];
    1.40 +			[matches intersectSet:[[pluginsByAuthor objectForKey:author] setRepresentation]];
    1.41  		if (version)
    1.42 -			[matches intersectSet:[pluginsByVersion objectForKey:version]];
    1.43 +			[matches intersectSet:[[pluginsByVersion objectForKey:version] setRepresentation]];
    1.44  		if (type)
    1.45 -			[matches intersectSet:[pluginsByType objectForKey:type]];
    1.46 +			[matches intersectSet:[[pluginsByType objectForKey:type] setRepresentation]];
    1.47  	}
    1.48  
    1.49  	return matches;