1 @interface
NSObject (UICaboodle
)
2 - (id
) yieldToSelector
:(SEL
)selector withObject
:(id
)object
;
3 - (id
) yieldToSelector
:(SEL
)selector
;
6 @implementation
NSObject (UICaboodle
)
11 - (void) _yieldToContext
:(NSMutableArray
*)context
{ _pooled
12 SEL
selector(reinterpret_cast<SEL
>([[context objectAtIndex
:0] pointerValue
]));
13 id
object([[context objectAtIndex
:1] nonretainedObjectValue
]);
14 volatile bool &stopped(*reinterpret_cast<bool *>([[context objectAtIndex
:2] pointerValue
]));
16 /* XXX: deal with exceptions */
17 id
value([self performSelector
:selector withObject
:object
]);
19 NSMethodSignature
*signature([self methodSignatureForSelector
:selector
]);
20 [context removeAllObjects
];
21 if ([signature methodReturnLength
] != 0 && value
!= nil
)
22 [context addObject
:value
];
27 performSelectorOnMainThread
:@
selector(doNothing
)
33 - (id
) yieldToSelector
:(SEL
)selector withObject
:(id
)object
{
34 /*return [self performSelector:selector withObject:object];*/
36 volatile bool stopped(false);
38 NSMutableArray
*context([NSMutableArray arrayWithObjects
:
39 [NSValue valueWithPointer
:selector
],
40 [NSValue valueWithNonretainedObject
:object
],
41 [NSValue valueWithPointer
:const_cast<bool *>(&stopped
)],
44 NSThread
*thread([[[NSThread alloc
]
46 selector
:@
selector(_yieldToContext
:)
52 NSRunLoop
*loop([NSRunLoop currentRunLoop
]);
53 NSDate
*future([NSDate distantFuture
]);
55 while (!stopped
&& [loop runMode
:NSDefaultRunLoopMode beforeDate
:future
]);
57 return [context count
] == 0 ? nil
: [context objectAtIndex
:0];
60 - (id
) yieldToSelector
:(SEL
)selector
{
61 return [self yieldToSelector
:selector withObject
:nil
];