Libpurple.framework [484]: libpurple 2.2.0
[adiumx.git] / Source / AIDictionaryDebug.m
blobd262c6518142fd3d4be8bb69355d36cc4f80b60e
1 /*
2  * AIDictionaryDebug.m
3  *
4  * Created by Evan Schoenberg on 7/26/05.
5  *
6  * This class is explicitly released under the BSD license with the following modification:
7  * It may be used without reproduction of its copyright notice within The Adium Project.
8  *
9  * This class was created for use in the Adium project, which is released under the GPL.
10  * The release of this specific class (AIDictionaryDebug) under BSD in no way changes the licensing of any other portion
11  * of the Adium project.
12  *
13  ****
14  Copyright (c) 2005, Evan Schoenberg
15  All rights reserved.
17  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
19  Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
20  Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer 
21  in the documentation and/or other materials provided with the distribution.
22  Neither the name of Adium nor the names of its contributors may be used to endorse or promote products
23  derived from this software without specific prior written permission.
25  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
26  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
33 #import "AIDictionaryDebug.h"
35 #ifdef DEBUG_BUILD
36 #import <objc/objc-class.h>
38 @interface AIDictionaryDebug (PRIVATE)
39 + (IMP)replaceSelector:(SEL)sel ofClass:(Class)oldClass withClass:(Class)newClass;
40 @end
42 #endif
44 @implementation AIDictionaryDebug
46 #ifdef DEBUG_BUILD
48 typedef void (*SetObjectForKeyIMP)(id, SEL, id, id);
49 SetObjectForKeyIMP      originalSetObjectForKey = nil;
51 typedef void (*RemoveObjectForKeyIMP)(id, SEL, id);
52 RemoveObjectForKeyIMP   originalRemoveObjectForKey = nil;
54 extern void _objc_flush_caches(Class);
56 + (void)load
58         originalSetObjectForKey = (SetObjectForKeyIMP)[AIDictionaryDebug replaceSelector:@selector(setObject:forKey:)
59                                                                                                                                                          ofClass:NSClassFromString(@"NSCFDictionary")
60                                                                                                                                                    withClass:[AIDictionaryDebug class]];
61         originalRemoveObjectForKey = (RemoveObjectForKeyIMP)[AIDictionaryDebug replaceSelector:@selector(removeObjectForKey:)
62                                                                                                                                                                 ofClass:NSClassFromString(@"NSCFDictionary")
63                                                                                                                                                           withClass:[AIDictionaryDebug class]];
66 - (void)setObject:(id)object forKey:(id)key
68         NSAssert3(object != nil, @"%@: Attempted to set %@ for %@",self,object,key);
69         NSAssert3(key != nil, @"%@: Attempted to set %@ for %@",self,object,key);
71         originalSetObjectForKey(self, @selector(setObject:forKey:),object,key);
74 - (void)removeObjectForKey:(id)key
76         NSAssert1(key != nil, @"%@: Attempted to remove a nil key",self);
78         originalRemoveObjectForKey(self, @selector(removeObjectForKey:),key);
81 + (IMP)replaceSelector:(SEL)sel ofClass:(Class)oldClass withClass:(Class)newClass
83     IMP original = [oldClass instanceMethodForSelector:sel];
85         if (!original) {
86                 NSLog(@"Cannot find implementation for '%@' in %@",
87                           NSStringFromSelector(sel),
88                           NSStringFromClass(oldClass));
89                 return NULL;
90         }
92     struct objc_method *method = class_getInstanceMethod(oldClass, sel); 
94         // original to change
95     if (!method) {
96         NSLog(@"Cannot find method for '%@' in %@",
97                           NSStringFromSelector(sel),
98                           NSStringFromClass(oldClass));
99         return NULL;
100     }
102     IMP new = [newClass instanceMethodForSelector:sel]; // new method to use
103         if (!new) {
104                 NSLog(@"Cannot find implementation for '%@' in %@",
105                           NSStringFromSelector(sel),
106                           NSStringFromClass(newClass));
107                 return NULL;
108         }
110     method->method_imp = new;
112     _objc_flush_caches(oldClass);
114     return original;
117 #endif
118 @end