gabriel / gh-kit
- Source
- Commits
- Network (1)
- Issues (0)
- Downloads (0)
- Wiki (1)
- Graphs
-
Branch:
master
Alex Pretzlav (author)
Tue Dec 15 13:54:50 -0800 2009
| 4b932dfe » | gabriel | 2009-05-11 | 1 | // | |
| 2 | // GHNSInvocationProxy.h | ||||
| 3 | // GHKit | ||||
| 4 | // | ||||
| 5 | // Modified by Gabriel Handford on 5/9/09. | ||||
| 6 | // This class is based on DDInvocationGrabber. | ||||
| 7 | // | ||||
| 8 | |||||
| 9 | /* | ||||
| 10 | * Copyright (c) 2007-2009 Dave Dribin | ||||
| 11 | * | ||||
| 12 | * Permission is hereby granted, free of charge, to any person | ||||
| 13 | * obtaining a copy of this software and associated documentation | ||||
| 14 | * files (the "Software"), to deal in the Software without | ||||
| 15 | * restriction, including without limitation the rights to use, copy, | ||||
| 16 | * modify, merge, publish, distribute, sublicense, and/or sell copies | ||||
| 17 | * of the Software, and to permit persons to whom the Software is | ||||
| 18 | * furnished to do so, subject to the following conditions: | ||||
| 19 | * | ||||
| 20 | * The above copyright notice and this permission notice shall be | ||||
| 21 | * included in all copies or substantial portions of the Software. | ||||
| 22 | * | ||||
| 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||||
| 24 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||||
| 25 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||||
| 26 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | ||||
| 27 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | ||||
| 28 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||||
| 29 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||||
| 30 | * SOFTWARE. | ||||
| 31 | */ | ||||
| 32 | |||||
| 33 | |||||
| 34 | /* | ||||
| 35 | * This class is based on CInvocationGrabber: | ||||
| 36 | * | ||||
| 37 | * Copyright (c) 2007, Toxic Software | ||||
| 38 | * All rights reserved. | ||||
| 39 | * Redistribution and use in source and binary forms, with or without | ||||
| 40 | * modification, are permitted provided that the following conditions are | ||||
| 41 | * met: | ||||
| 42 | * | ||||
| 43 | * * Redistributions of source code must retain the above copyright notice, | ||||
| 44 | * this list of conditions and the following disclaimer. | ||||
| 45 | * | ||||
| 46 | * * Redistributions in binary form must reproduce the above copyright | ||||
| 47 | * notice, this list of conditions and the following disclaimer in the | ||||
| 48 | * documentation and/or other materials provided with the distribution. | ||||
| 49 | * | ||||
| 50 | * * Neither the name of the Toxic Software nor the names of its | ||||
| 51 | * contributors may be used to endorse or promote products derived from | ||||
| 52 | * this software without specific prior written permission. | ||||
| 53 | * | ||||
| 54 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||||
| 55 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||||
| 56 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||||
| 57 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE | ||||
| 58 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||||
| 59 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||||
| 60 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||||
| 61 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||||
| 62 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||||
| 63 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | ||||
| 64 | * THE POSSIBILITY OF SUCH DAMAGE. | ||||
| 65 | * | ||||
| 66 | */ | ||||
| 67 | |||||
| 72b4efc4 » | gabriel | 2009-06-29 | 68 | @class GHNSInvocationProxy; | |
| bc6d2526 » | gabriel | 2009-07-08 | 69 | @class GHNSInvocationProxyCallback; | |
| 72b4efc4 » | gabriel | 2009-06-29 | 70 | ||
| 71 | @protocol GHNSInvocationTracer <NSObject> | ||||
| 72 | - (void)proxy:(GHNSInvocationProxy *)proxy willInvoke:(NSInvocation *)invocation; | ||||
| 73 | - (void)proxy:(GHNSInvocationProxy *)proxy didInvoke:(NSInvocation *)invocation; | ||||
| 74 | @end | ||||
| 75 | |||||
| bc6d2526 » | gabriel | 2009-07-08 | 76 | @interface GHNSLogInvocationTracer : NSObject <GHNSInvocationTracer> { | |
| 77 | NSTimeInterval _interval; | ||||
| 78 | } | ||||
| 72b4efc4 » | gabriel | 2009-06-29 | 79 | + (GHNSLogInvocationTracer *)shared; | |
| 80 | @end | ||||
| 81 | |||||
| 4b932dfe » | gabriel | 2009-05-11 | 82 | /*! | |
| a2a0700a » | gabriel | 2009-05-14 | 83 | Proxy that allows invocation on a separate thread, or with a delay. | |
| 4b932dfe » | gabriel | 2009-05-11 | 84 | ||
| a2a0700a » | gabriel | 2009-05-14 | 85 | Use with the GHNSObject+Invocation category: | |
| 4b932dfe » | gabriel | 2009-05-11 | 86 | ||
| 87 | @code | ||||
| a2a0700a » | gabriel | 2009-05-14 | 88 | NSMutableArray *array = ...; | |
| 89 | // Adds object to array after 5 seconds | ||||
| 90 | [[array gh_proxyAfterDelay:5.0] addObject:@"test"]; | ||||
| 91 | |||||
| 92 | |||||
| 93 | NSThread *thread = ... | ||||
| 94 | // Remove all objects from another thread | ||||
| 95 | [[array gh_proxyOnThread:thread waitUntilDone:NO] removeAllObjects]; | ||||
| 4b932dfe » | gabriel | 2009-05-11 | 96 | @endcode | |
| 97 | |||||
| a2a0700a » | gabriel | 2009-05-14 | 98 | Create invocation proxy for a NSMutableArray. | |
| 99 | |||||
| 100 | @code | ||||
| 101 | NSMutableArray *array = ...; | ||||
| 102 | NSThread *thread = ... | ||||
| 103 | |||||
| 104 | GHNSInvocationProxy *arrayProxy = [GHNSInvocationProxy invocation]; | ||||
| 105 | arrayProxy.target = array; | ||||
| 106 | arrayProxy.thread = thread; | ||||
| 107 | arrayProxy.waitUntilDone = NO; | ||||
| 108 | |||||
| 109 | // Performs method on thread and doesn't wait for return | ||||
| 110 | [arrayProxy addObject:@"test"]; | ||||
| 4b932dfe » | gabriel | 2009-05-11 | 111 | @endcode | |
| 112 | */ | ||||
| 113 | @interface GHNSInvocationProxy : NSProxy { | ||||
| 114 | |||||
| 115 | id target_; | ||||
| d2794906 » | gabriel | 2009-05-26 | 116 | ||
| 117 | // If not forwarding to target selector, we can use this to override the | ||||
| 118 | // selector called | ||||
| 119 | SEL selector_; | ||||
| ecd21d08 » | gabriel | 2009-05-19 | 120 | ||
| 4b932dfe » | gabriel | 2009-05-11 | 121 | NSThread *thread_; | |
| 122 | BOOL waitUntilDone_; | ||||
| bc6d2526 » | gabriel | 2009-07-08 | 123 | NSTimeInterval delay_; // Defaults to -1 (no delay) | |
| 124 | |||||
| 72b4efc4 » | gabriel | 2009-06-29 | 125 | id<GHNSInvocationTracer> tracer_; // weak | |
| 4b932dfe » | gabriel | 2009-05-11 | 126 | ||
| bc6d2526 » | gabriel | 2009-07-08 | 127 | // If detaching on new thread | |
| 128 | GHNSInvocationProxyCallback *detachCallback_; | ||||
| 129 | |||||
| 4b932dfe » | gabriel | 2009-05-11 | 130 | NSInvocation *invocation_; | |
| 131 | } | ||||
| 132 | |||||
| 133 | @property (retain, nonatomic) id target; | ||||
| d2794906 » | gabriel | 2009-05-26 | 134 | @property (assign, nonatomic) SEL selector; | |
| 4b932dfe » | gabriel | 2009-05-11 | 135 | @property (retain, nonatomic) NSInvocation *invocation; | |
| 136 | @property (retain, nonatomic) NSThread *thread; | ||||
| 137 | @property (assign, nonatomic) BOOL waitUntilDone; | ||||
| 138 | @property (assign, nonatomic) NSTimeInterval delay; | ||||
| 72b4efc4 » | gabriel | 2009-06-29 | 139 | @property (assign, nonatomic) id<GHNSInvocationTracer> tracer; | |
| bc6d2526 » | gabriel | 2009-07-08 | 140 | @property (retain, nonatomic) GHNSInvocationProxyCallback *detachCallback; | |
| 4b932dfe » | gabriel | 2009-05-11 | 141 | ||
| a2a0700a » | gabriel | 2009-05-14 | 142 | /*! | |
| 143 | Create autoreleased empty invocation proxy. | ||||
| 144 | @result Invocation proxy | ||||
| 145 | */ | ||||
| 4b932dfe » | gabriel | 2009-05-11 | 146 | + (id)invocation; | |
| 147 | |||||
| a2a0700a » | gabriel | 2009-05-14 | 148 | /*! | |
| 149 | Create invocation proxy with target. | ||||
| 150 | @param target | ||||
| 151 | @result Invocation proxy | ||||
| 152 | */ | ||||
| 4b932dfe » | gabriel | 2009-05-11 | 153 | - (id)prepareWithInvocationTarget:(id)target; | |
| 154 | |||||
| d2794906 » | gabriel | 2009-05-26 | 155 | /*! | |
| 156 | Create invocation proxy with target and overriding selector. | ||||
| 157 | |||||
| 158 | Overriding the selector only make sense when using the "argument proxy". | ||||
| 159 | For example, | ||||
| 160 | |||||
| 161 | id target = ...; | ||||
| 162 | SEL selector = @selector(bar:baz:); | ||||
| 163 | [[[GHNSInvocationProxy invocation] prepareWithInvocationTarget:target selector:selector] arg:10 arg:20]; | ||||
| 164 | |||||
| 165 | Will call [target bar:10 baz:20]; (and not arg:arg: selector which doesn't exist). | ||||
| 166 | |||||
| 167 | This allows you to call a selector variable with primitive and multi arguments, | ||||
| 168 | whereas before you would have to use a manually constructed NSInvocation. | ||||
| 169 | |||||
| 170 | See GHNSObject+Invocation#gh_argumentProxy for the shorthand. | ||||
| 171 | |||||
| 172 | @param target | ||||
| 173 | @param selector | ||||
| 174 | @result Invocation proxy | ||||
| 175 | */ | ||||
| 176 | - (id)prepareWithInvocationTarget:(id)target selector:(SEL)selector; | ||||
| 177 | |||||
| 4b932dfe » | gabriel | 2009-05-11 | 178 | @end | |
| bc6d2526 » | gabriel | 2009-07-08 | 179 | ||
| 180 | |||||
| 181 | @interface GHNSInvocationProxyCallback : NSObject { | ||||
| 182 | id target_; // Retained until after callback | ||||
| 183 | SEL action_; | ||||
| 184 | id context_; // Retained until after callback | ||||
| 185 | NSThread *thread_; // Retained until after callback | ||||
| 186 | } | ||||
| 187 | |||||
| 188 | - (id)initWithTarget:(id)target action:(SEL)action context:(id)context; | ||||
| 189 | |||||
| 190 | @end | ||||
| 191 | |||||
