Index: src/OFTimer.h ================================================================== --- src/OFTimer.h +++ src/OFTimer.h @@ -13,10 +13,14 @@ * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #import "OFObject.h" + +#ifdef OF_HAVE_BLOCKS +typedef void (^of_timer_block_t)(void); +#endif @class OFDate; /** * \brief A class for creating and firing timers. @@ -27,10 +31,13 @@ double interval; id target, object1, object2; SEL selector; uint8_t arguments; BOOL repeats; +#ifdef OF_HAVE_BLOCKS + of_timer_block_t block; +#endif } /** * \brief Creates and schedules a new timer with the specified time interval. * @@ -82,10 +89,25 @@ selector: (SEL)selector object: (id)object1 object: (id)object2 repeats: (BOOL)repeats; +#ifdef OF_HAVE_BLOCKS +/** + * \brief Creates and schedules a new timer with the specified time interval. + * + * \param interval The time interval after which the timer should be executed + * when fired + * \param repeats Whether the timer repeats after it has been executed + * \param block The block to invoke when the timer fires + * \return A new, autoreleased timer + */ ++ scheduledTimerWithTimeInterval: (double)interval + repeats: (BOOL)repeats + block: (of_timer_block_t)block; +#endif + /** * \brief Creates a new timer with the specified time interval. * * \param interval The time interval after which the timer should be executed * when fired @@ -134,10 +156,25 @@ target: (id)target selector: (SEL)selector object: (id)object1 object: (id)object2 repeats: (BOOL)repeats; + +#ifdef OF_HAVE_BLOCKS +/** + * \brief Creates a new timer with the specified time interval. + * + * \param interval The time interval after which the timer should be executed + * when fired + * \param repeats Whether the timer repeats after it has been executed + * \param block The block to invoke when the timer fires + * \return A new, autoreleased timer + */ ++ timerWithTimeInterval: (double)interval + repeats: (BOOL)repeats + block: (of_timer_block_t)block; +#endif /** * \brief Initializes an already allocated timer with the specified time * interval. * @@ -192,10 +229,28 @@ target: (id)target selector: (SEL)selector object: (id)object1 object: (id)object2 repeats: (BOOL)repeats; + +#ifdef OF_HAVE_BLOCKS +/** + * \brief Initializes an already allocated timer with the specified time + * interval. + * + * \param fireDate The date at which the timer should fire + * \param interval The time interval after which to repeat the timer, if it is + * a repeating timer + * \param repeats Whether the timer repeats after it has been executed + * \param block The block to invoke when the timer fires + * \return An initialized timer + */ +- initWithFireDate: (OFDate*)fireDate + interval: (double)interval + repeats: (BOOL)repeats + block: (of_timer_block_t)block; +#endif /** * \brief Fires the timer, meaning it will execute the specified selector on the * target. */ Index: src/OFTimer.m ================================================================== --- src/OFTimer.m +++ src/OFTimer.m @@ -92,10 +92,31 @@ [timer retain]; objc_autoreleasePoolPop(pool); return [timer autorelease]; } + +#ifdef OF_HAVE_BLOCKS ++ scheduledTimerWithTimeInterval: (double)interval + repeats: (BOOL)repeats + block: (of_timer_block_t)block +{ + void *pool = objc_autoreleasePoolPush(); + OFDate *fireDate = [OFDate dateWithTimeIntervalSinceNow: interval]; + id timer = [[[self alloc] initWithFireDate: fireDate + interval: interval + repeats: repeats + block: block] autorelease]; + + [[OFRunLoop currentRunLoop] addTimer: timer]; + + [timer retain]; + objc_autoreleasePoolPop(pool); + + return [timer autorelease]; +} +#endif + timerWithTimeInterval: (double)interval target: (id)target selector: (SEL)selector repeats: (BOOL)repeats @@ -155,10 +176,29 @@ [timer retain]; objc_autoreleasePoolPop(pool); return [timer autorelease]; } + +#ifdef OF_HAVE_BLOCKS ++ timerWithTimeInterval: (double)interval + repeats: (BOOL)repeats + block: (of_timer_block_t)block +{ + void *pool = objc_autoreleasePoolPush(); + OFDate *fireDate = [OFDate dateWithTimeIntervalSinceNow: interval]; + id timer = [[[self alloc] initWithFireDate: fireDate + interval: interval + repeats: repeats + block: block] autorelease]; + + [timer retain]; + objc_autoreleasePoolPop(pool); + + return [timer autorelease]; +} +#endif - _initWithFireDate: (OFDate*)fireDate_ interval: (double)interval_ target: (id)target_ selector: (SEL)selector_ @@ -234,17 +274,42 @@ object: object1_ object: object2_ arguments: 2 repeats: repeats_]; } + +#ifdef OF_HAVE_BLOCKS +- initWithFireDate: (OFDate*)fireDate_ + interval: (double)interval_ + repeats: (BOOL)repeats_ + block: (of_timer_block_t)block_ +{ + self = [super init]; + + @try { + fireDate = [fireDate_ retain]; + interval = interval_; + repeats = repeats_; + block = [block_ copy]; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} +#endif - (void)dealloc { [fireDate release]; [target release]; [object1 release]; [object2 release]; +#ifdef OF_HAVE_BLOCKS + [block release]; +#endif [super dealloc]; } - (of_comparison_result_t)compare: (id )object_ @@ -263,24 +328,32 @@ - (void)fire { OF_ENSURE(arguments >= 0 && arguments <= 2); - switch (arguments) { - case 0: - [target performSelector: selector]; - break; - case 1: - [target performSelector: selector - withObject: object1]; - break; - case 2: - [target performSelector: selector - withObject: object1 - withObject: object2]; - break; - } +#ifdef OF_HAVE_BLOCKS + if (block != NULL) + block(); + else { +#endif + switch (arguments) { + case 0: + [target performSelector: selector]; + break; + case 1: + [target performSelector: selector + withObject: object1]; + break; + case 2: + [target performSelector: selector + withObject: object1 + withObject: object2]; + break; + } +#ifdef OF_HAVE_BLOCKS + } +#endif if (repeats) { OFDate *old = fireDate; fireDate = [[OFDate alloc] initWithTimeIntervalSinceNow: interval];