ObjFW  Check-in [dde45db1ef]

Overview
Comment:OFTimer: Add support for invoking blocks.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: dde45db1efdfcd4f13cee8b0f6e3084370627dad8271994ba98a2f73aaf6a03c
User & Date: js on 2012-09-12 08:03:01
Other Links: manifest | tags
Context
2012-09-12
17:27
Split -[OFStream fileDescriptor]. check-in: 440e95fd4a user: js tags: trunk
08:03
OFTimer: Add support for invoking blocks. check-in: dde45db1ef user: js tags: trunk
06:41
OFRunLoop: Reduce the time the lock is held. check-in: 34e548c51d user: js tags: trunk
Changes

Modified src/OFTimer.h from [1d5be3d2a6] to [e58cca5bff].

11
12
13
14
15
16
17




18
19
20
21
22
23
24
25
26
27
28
29
30
31



32
33
34
35
36
37
38
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#import "OFObject.h"





@class OFDate;

/**
 * \brief A class for creating and firing timers.
 */
@interface OFTimer: OFObject <OFComparing>
{
	OFDate *fireDate;
	double interval;
	id target, object1, object2;
	SEL selector;
	uint8_t arguments;
	BOOL repeats;



}

/**
 * \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







>
>
>
>














>
>
>







11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * 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.
 */
@interface OFTimer: OFObject <OFComparing>
{
	OFDate *fireDate;
	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.
 *
 * \param interval The time interval after which the timer should be executed
 *		   when fired
80
81
82
83
84
85
86















87
88
89
90
91
92
93
+ scheduledTimerWithTimeInterval: (double)interval
			  target: (id)target
			selector: (SEL)selector
			  object: (id)object1
			  object: (id)object2
			 repeats: (BOOL)repeats;
















/**
 * \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 target The target on which to call the selector
 * \param selector The selector to call on the target







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
+ scheduledTimerWithTimeInterval: (double)interval
			  target: (id)target
			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
 * \param target The target on which to call the selector
 * \param selector The selector to call on the target
132
133
134
135
136
137
138















139
140
141
142
143
144
145
 */
+ timerWithTimeInterval: (double)interval
		 target: (id)target
	       selector: (SEL)selector
		 object: (id)object1
		 object: (id)object2
		repeats: (BOOL)repeats;
















/**
 * \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







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
 */
+ timerWithTimeInterval: (double)interval
		 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.
 *
 * \param fireDate The date at which the timer should fire
 * \param interval The time interval after which to repeat the timer, if it is
190
191
192
193
194
195
196


















197
198
199
200
201
202
203
- initWithFireDate: (OFDate*)fireDate
	  interval: (double)interval
	    target: (id)target
	  selector: (SEL)selector
	    object: (id)object1
	    object: (id)object2
	   repeats: (BOOL)repeats;



















/**
 * \brief Fires the timer, meaning it will execute the specified selector on the
 *	  target.
 */
- (void)fire;








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
- initWithFireDate: (OFDate*)fireDate
	  interval: (double)interval
	    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.
 */
- (void)fire;

Modified src/OFTimer.m from [70767b5be1] to [abdbbbded3].

90
91
92
93
94
95
96





















97
98
99
100
101
102
103
	[[OFRunLoop currentRunLoop] addTimer: timer];

	[timer retain];
	objc_autoreleasePoolPop(pool);

	return [timer autorelease];
}






















+ timerWithTimeInterval: (double)interval
		 target: (id)target
	       selector: (SEL)selector
		repeats: (BOOL)repeats
{
	void *pool = objc_autoreleasePoolPush();







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
	[[OFRunLoop currentRunLoop] addTimer: timer];

	[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
{
	void *pool = objc_autoreleasePoolPush();
153
154
155
156
157
158
159



















160
161
162
163
164
165
166
					   repeats: repeats] autorelease];

	[timer retain];
	objc_autoreleasePoolPop(pool);

	return [timer autorelease];
}




















- _initWithFireDate: (OFDate*)fireDate_
	   interval: (double)interval_
	     target: (id)target_
	   selector: (SEL)selector_
	     object: (id)object1_
	     object: (id)object2_







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
					   repeats: repeats] autorelease];

	[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_
	     object: (id)object1_
	     object: (id)object2_
232
233
234
235
236
237
238






















239
240
241
242
243
244
245



246
247
248
249
250
251
252
				target: target_
			      selector: selector_
				object: object1_
				object: object2_
			     arguments: 2
			       repeats: repeats_];
}























- (void)dealloc
{
	[fireDate release];
	[target release];
	[object1 release];
	[object2 release];




	[super dealloc];
}

- (of_comparison_result_t)compare: (id <OFComparing>)object_
{
	OFTimer *otherTimer;







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







>
>
>







272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
				target: target_
			      selector: selector_
				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 <OFComparing>)object_
{
	OFTimer *otherTimer;
261
262
263
264
265
266
267





268
269
270
271
272
273
274
275
276
277
278
279
280
281



282
283
284
285
286
287
288
	return [fireDate compare: otherTimer->fireDate];
}

- (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;
	}




	if (repeats) {
		OFDate *old = fireDate;
		fireDate = [[OFDate alloc]
		    initWithTimeIntervalSinceNow: interval];
		[old release];








>
>
>
>
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
>
>







326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
	return [fireDate compare: otherTimer->fireDate];
}

- (void)fire
{
	OF_ENSURE(arguments >= 0 && arguments <= 2);

#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];
		[old release];