Wired Foundation  2.0
A foundation framework for the Wired implementation on Mac OS X
NSObject-WIFoundation.m
00001 /* $Id$ */
00002 
00003 /*
00004  *  Copyright (c) 2003-2009 Axel Andersson
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *  1. Redistributions of source code must retain the above copyright
00011  *     notice, this list of conditions and the following disclaimer.
00012  *  2. Redistributions in binary form must reproduce the above copyright
00013  *     notice, this list of conditions and the following disclaimer in the
00014  *     documentation and/or other materials provided with the distribution.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00017  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
00020  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00021  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00023  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
00024  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00025  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00026  * POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00029 #import <WiredFoundation/NSObject-WIFoundation.h>
00030 #import <WiredFoundation/NSInvocation-WIFoundation.h>
00031 #import <WiredFoundation/NSThread-WIFoundation.h>
00032 
00033 @implementation NSObject(WIFoundation)
00034 
00035 + (NSBundle *)bundle {
00036         return [NSBundle bundleForClass:self];
00037 }
00038 
00039 
00040 
00041 - (NSBundle *)bundle {
00042         return [[self class] bundle];
00043 }
00044 
00045 
00046 
00047 #pragma mark -
00048 
00049 + (void)cancelPreviousPerformRequestsWithTarget:(id)target selector:(SEL)selector {
00050         [NSObject cancelPreviousPerformRequestsWithTarget:target selector:selector object:NULL];
00051 }
00052 
00053 
00054 #pragma mark -
00055 
00056 - (void)performSelector:(SEL)selector afterDelay:(NSTimeInterval)delay {
00057         [self performSelector:selector withObject:NULL afterDelay:delay];
00058 }
00059 
00060 
00061 
00062 - (void)performSelectorOnce:(SEL)selector afterDelay:(NSTimeInterval)delay {
00063         [NSObject cancelPreviousPerformRequestsWithTarget:self selector:selector object:NULL];
00064         [self performSelector:selector withObject:NULL afterDelay:delay];
00065 }
00066 
00067 
00068 
00069 - (void)performSelectorOnce:(SEL)selector withObject:(id)object afterDelay:(NSTimeInterval)delay {
00070         [NSObject cancelPreviousPerformRequestsWithTarget:self selector:selector object:object];
00071         [self performSelector:selector withObject:object afterDelay:delay];
00072 }
00073 
00074 @end
00075 
00076 
00077 
00078 @implementation NSObject(WIObjectPropertylistSerialization)
00079 
00080 - (BOOL)isKindOfPropertyListSerializableClass {
00081         return ([self isKindOfClass:[NSData class]] ||
00082                         [self isKindOfClass:[NSDate class]] ||
00083                         [self isKindOfClass:[NSNumber class]] ||
00084                         [self isKindOfClass:[NSString class]] ||
00085                         [self isKindOfClass:[NSArray class]] ||
00086                         [self isKindOfClass:[NSDictionary class]]);
00087 }
00088 
00089 @end
00090 
00091 
00092 
00093 @implementation NSObject(WIDeepMutableCopying)
00094 
00095 - (id)deepMutableCopy {
00096         return [self deepMutableCopyWithZone:NULL];
00097 }
00098 
00099 
00100 
00101 - (id)deepMutableCopyWithZone:(NSZone *)zone {
00102         if([self respondsToSelector:@selector(mutableCopyWithZone:)])
00103                 return [(id) self mutableCopyWithZone:zone];
00104         else if([self respondsToSelector:@selector(copyWithZone:)])
00105                 return [(id) self copyWithZone:zone];
00106         
00107         return NULL;
00108 }
00109 
00110 @end
00111 
00112 
00113 
00114 
00115 @implementation NSObject(WIThreadScheduling)
00116 
00117 - (void)performSelectorOnMainThread:(SEL)action {
00118         [self performSelectorOnMainThread:action withObject:NULL waitUntilDone:NO];
00119 }
00120 
00121 
00122 
00123 - (void)performSelectorOnMainThread:(SEL)action waitUntilDone:(BOOL)waitUntilDone {
00124         [self performSelectorOnMainThread:action withObject:NULL waitUntilDone:waitUntilDone];
00125 }
00126 
00127 
00128 
00129 - (void)performSelectorOnMainThread:(SEL)action withObject:(id)object1 {
00130         [self performSelectorOnMainThread:action withObject:object1 waitUntilDone:NO];
00131 }
00132 
00133 
00134 
00135 - (void)performSelectorOnMainThread:(SEL)action withObject:(id)object1 withObject:(id)object2 {
00136         [self performSelectorOnMainThread:action withObject:object1 withObject:object2 waitUntilDone:NO];
00137 }
00138 
00139 
00140 
00141 - (void)performSelectorOnMainThread:(SEL)action withObject:(id)object1 withObject:(id)object2 waitUntilDone:(BOOL)waitUntilDone {
00142         NSInvocation    *invocation;
00143         
00144         if([NSThread isMainThread]) {
00145                 [self performSelector:action withObject:object1 withObject:object2];
00146         } else {
00147                 invocation = [NSInvocation invocationWithTarget:self action:action];
00148                 [invocation setArgument:&object1 atIndex:2];
00149                 [invocation setArgument:&object2 atIndex:3];
00150                 [self performInvocationOnMainThread:invocation waitUntilDone:waitUntilDone];
00151         }
00152 }
00153 
00154 
00155 
00156 - (void)performSelectorOnMainThread:(SEL)action withObject:(id)object1 withObject:(id)object2 withObject:(id)object3 {
00157         [self performSelectorOnMainThread:action withObject:object1 withObject:object2 withObject:object3 waitUntilDone:NO];
00158 }
00159 
00160 
00161 
00162 - (void)performSelectorOnMainThread:(SEL)action withObject:(id)object1 withObject:(id)object2 withObject:(id)object3 waitUntilDone:(BOOL)waitUntilDone {
00163         NSInvocation    *invocation;
00164         
00165         invocation = [NSInvocation invocationWithTarget:self action:action];
00166         [invocation setArgument:&object1 atIndex:2];
00167         [invocation setArgument:&object2 atIndex:3];
00168         [invocation setArgument:&object3 atIndex:4];
00169         
00170         if([NSThread isMainThread])
00171                 [invocation invoke];
00172         else
00173                 [self performInvocationOnMainThread:invocation waitUntilDone:waitUntilDone];
00174 }
00175 
00176 
00177 
00178 - (void)performInvocationOnMainThread:(NSInvocation *)invocation {
00179         [self performInvocationOnMainThread:invocation waitUntilDone:NO];
00180 }
00181 
00182 
00183 
00184 - (void)performInvocationOnMainThread:(NSInvocation *)invocation waitUntilDone:(BOOL)waitUntilDone {
00185         [invocation retainArguments];
00186         [invocation performSelectorOnMainThread:@selector(invoke) withObject:NULL waitUntilDone:waitUntilDone];
00187 }
00188 
00189 @end
 All Classes