-
Notifications
You must be signed in to change notification settings - Fork 280
/
NSTimer.m
452 lines (417 loc) · 12.1 KB
/
NSTimer.m
1
2
3
4
5
6
7
8
9
10
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
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
259
260
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
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
318
319
320
321
322
323
324
325
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/** Implementation of NSTimer for GNUstep
Copyright (C) 1995, 1996, 1999 Free Software Foundation, Inc.
Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
Created: March 1996
Rewrite by: Richard Frith-Macdonald <rfm@gnu.org>
This file is part of the GNUstep Base Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110 USA.
<title>NSTimer class reference</title>
$Date$ $Revision$
*/
#import "common.h"
#define EXPOSE_NSTimer_IVARS 1
#import "Foundation/NSTimer.h"
#import "Foundation/NSDate.h"
#import "Foundation/NSException.h"
#import "Foundation/NSRunLoop.h"
#import "Foundation/NSInvocation.h"
@class NSGDate;
@interface NSGDate : NSObject // Help the compiler
@end
static Class NSDate_class;
/**
* <p>An <code>NSTimer</code> provides a way to send a message at some time in
* the future, possibly repeating every time a fixed interval has passed. To
* use a timer, you can either create one that will automatically be added to
* the run loop in the current thread (using the -addTimer:forMode: method),
* or you can create it without adding it then add it to an [NSRunLoop]
* explicitly later.
* </p>
* <p>NB. You may not use -init or +new to create a timer, as the timer must
* be properly initialised to send an action after some interval.
* </p>
*/
@implementation NSTimer
+ (void) initialize
{
if (self == [NSTimer class])
{
NSDate_class = [NSGDate class];
}
}
- (NSString*) description
{
NSString *s = [super description];
if ([self isValid])
{
if (_selector == 0)
{
return [NSString stringWithFormat: @"%@ at %@ invokes %@",
s, [self fireDate], _target];
}
else
{
return [NSString stringWithFormat: @"%@ at %@ sends %@ to (%@)",
s, [self fireDate], NSStringFromSelector(_selector), _target];
}
}
else
{
return [NSString stringWithFormat: @"%@ (invalidated)", s];
}
}
/* For MacOS-X compatibility, this returns nil.
*/
- (id) init
{
DESTROY(self);
return nil;
}
/** <init />
* Initialise the receive, a newly allocated NSTimer object.<br />
* The ti argument specifies the time (in seconds) between the firing.
* If it is less than or equal to 0.0 then a small interval is chosen
* automatically.<br />
* The fd argument specifies an initial fire date copied by the timer...
* if it is not supplied (a nil object) then the ti argument is used to
* create a start date relative to the current time.<br />
* The f argument specifies whether the timer will fire repeatedly
* or just once.<br />
* If the selector argument is zero, then then object is an invocation
* to be used when the timer fires. otherwise, the object is sent the
* message specified by the selector and with the timer as an argument.<br />
* The object and info arguments will be retained until the timer is
* invalidated.
*/
- (id) initWithFireDate: (NSDate*)fd
interval: (NSTimeInterval)ti
target: (id)object
selector: (SEL)selector
userInfo: (id)info
repeats: (BOOL)f
{
if (ti <= 0.0)
{
ti = 0.0001;
}
if (fd == nil)
{
_date = [[NSDate_class allocWithZone: NSDefaultMallocZone()]
initWithTimeIntervalSinceNow: ti];
}
else
{
_date = [fd copyWithZone: NSDefaultMallocZone()];
}
_target = RETAIN(object);
_selector = selector;
_info = RETAIN(info);
if (f == YES)
{
_repeats = YES;
_interval = ti;
}
else
{
_repeats = NO;
_interval = 0.0;
}
return self;
}
- (instancetype) initWithFireDate: (NSDate *)date
interval: (NSTimeInterval)interval
repeats: (BOOL)repeats
block: (GSTimerBlock)block
{
ASSIGN(_block, (id)block);
return [self initWithFireDate: date
interval: interval
target: nil
selector: NULL
userInfo: nil
repeats: repeats];
}
/**
* Create a timer which will fire after ti seconds and, if f is YES,
* every ti seconds thereafter. On firing, invocation will be performed.<br />
* NB. To make the timer operate, you must add it to a run loop.
*/
+ (NSTimer*) timerWithTimeInterval: (NSTimeInterval)ti
invocation: (NSInvocation*)invocation
repeats: (BOOL)f
{
return AUTORELEASE([[self alloc] initWithFireDate: nil
interval: ti
target: invocation
selector: NULL
userInfo: nil
repeats: f]);
}
/**
* Create a timer which will fire after ti seconds and, if f is YES,
* every ti seconds thereafter. On firing, the target object will be
* sent a message specified by selector and with the timer as its
* argument.<br />
* NB. To make the timer operate, you must add it to a run loop.
*/
+ (NSTimer*) timerWithTimeInterval: (NSTimeInterval)ti
target: (id)object
selector: (SEL)selector
userInfo: (id)info
repeats: (BOOL)f
{
return AUTORELEASE([[self alloc] initWithFireDate: nil
interval: ti
target: object
selector: selector
userInfo: info
repeats: f]);
}
+ (NSTimer*) timerWithTimeInterval: (NSTimeInterval)ti
repeats: (BOOL)f
block: (GSTimerBlock)block
{
return AUTORELEASE([[self alloc] initWithFireDate: nil
interval: ti
repeats: f
block: block]);
}
/**
* Create a timer which will fire after ti seconds and, if f is YES,
* every ti seconds thereafter. On firing, invocation will be performed.<br />
* This timer will automatically be added to the current run loop and
* will fire in the default run loop mode.
*/
+ (NSTimer*) scheduledTimerWithTimeInterval: (NSTimeInterval)ti
invocation: (NSInvocation*)invocation
repeats: (BOOL)f
{
id t = [[self alloc] initWithFireDate: nil
interval: ti
target: invocation
selector: NULL
userInfo: nil
repeats: f];
[[NSRunLoop currentRunLoop] addTimer: t forMode: NSDefaultRunLoopMode];
RELEASE(t);
return t;
}
/**
* Create a timer which will fire after ti seconds and, if f is YES,
* every ti seconds thereafter. On firing, the target object will be
* sent a message specified by selector and with the timer as its
* argument.<br />
* This timer will automatically be added to the current run loop and
* will fire in the default run loop mode.
*/
+ (NSTimer*) scheduledTimerWithTimeInterval: (NSTimeInterval)ti
target: (id)object
selector: (SEL)selector
userInfo: (id)info
repeats: (BOOL)f
{
id t = [[self alloc] initWithFireDate: nil
interval: ti
target: object
selector: selector
userInfo: info
repeats: f];
[[NSRunLoop currentRunLoop] addTimer: t forMode: NSDefaultRunLoopMode];
RELEASE(t);
return t;
}
+ (NSTimer *) scheduledTimerWithTimeInterval: (NSTimeInterval)ti
repeats: (BOOL)f
block: (GSTimerBlock)block
{
id t = [[self alloc] initWithFireDate: nil
interval: ti
repeats: f
block: block];
[[NSRunLoop currentRunLoop] addTimer: t forMode: NSDefaultRunLoopMode];
RELEASE(t);
return t;
}
- (void) dealloc
{
if (_invalidated == NO)
{
[self invalidate];
}
RELEASE(_date);
[super dealloc];
}
/**
* Fires the timer ... either performs an invocation or sends a message
* to a target object, depending on how the timer was set up.<br />
* If the timer is not set to repeat, it is automatically invalidated.<br />
* Exceptions raised during firing of the timer are caught and logged.
*/
- (void) fire
{
/* We check that we have not been invalidated before we fire.
*/
if (NO == _invalidated)
{
if ((id)_block != nil)
{
NS_DURING
{
CALL_NON_NULL_BLOCK(_block, self);
}
NS_HANDLER
{
NSLog(@"*** NSTimer ignoring exception '%@' (reason '%@') "
@"raised during posting of timer with block",
[localException name], [localException reason]);
}
NS_ENDHANDLER
}
else
{
id target;
/* We retain the target so it won't be deallocated while we are using
* it (if this timer gets invalidated while we are firing).
*/
target = RETAIN(_target);
if (_selector == 0)
{
NS_DURING
{
[(NSInvocation*)target invoke];
}
NS_HANDLER
{
NSLog(@"*** NSTimer ignoring exception '%@' (reason '%@') "
@"raised during posting of timer with target %s(%s) "
@"and selector '%@'",
[localException name], [localException reason],
GSClassNameFromObject(target),
GSObjCIsInstance(target) ? "instance" : "class",
NSStringFromSelector([target selector]));
}
NS_ENDHANDLER
}
else
{
NS_DURING
{
[target performSelector: _selector withObject: self];
}
NS_HANDLER
{
NSLog(@"*** NSTimer ignoring exception '%@' (reason '%@') "
@"raised during posting of timer with target %p and "
@"selector '%@'",
[localException name], [localException reason], target,
NSStringFromSelector(_selector));
}
NS_ENDHANDLER
}
RELEASE(target);
}
if (_repeats == NO)
{
[self invalidate];
}
}
}
/**
* Marks the timer as invalid, causing its target/invocation and user info
* objects to be released.<br />
* Invalidated timers are automatically removed from the run loop when it
* detects them.
*/
- (void) invalidate
{
/* OPENSTEP allows this method to be called multiple times. */
_invalidated = YES;
if (_target != nil)
{
DESTROY(_target);
}
if (_info != nil)
{
DESTROY(_info);
}
}
/**
* Checks to see if the timer has been invalidated.
*/
- (BOOL) isValid
{
if (_invalidated == NO)
{
return YES;
}
else
{
return NO;
}
}
/**
* Returns the date/time at which the timer is next due to fire.
*/
- (NSDate*) fireDate
{
return _date;
}
/**
* Change the fire date for the receiver.<br />
* NB. You should <em>NOT</em> use this method for a timer which has
* been added to a run loop. The only time when it is safe to modify
* the fire date of a timer in a run loop is for a repeating timer
* when the timer is actually in the process of firing.
*/
- (void) setFireDate: (NSDate*)fireDate
{
ASSIGN(_date, fireDate);
}
/**
* Returns the interval between firings, or zero if the timer
* does not repeat.
*/
- (NSTimeInterval) timeInterval
{
return _interval;
}
/**
* Returns the user info which was set for the timer when it was created,
* or nil if none was set or the timer is invalid.
*/
- (id) userInfo
{
return _info;
}
/**
* Compares timers based on the date at which they should next fire.
*/
- (NSComparisonResult) compare: (id)anotherTimer
{
if (anotherTimer == self)
{
return NSOrderedSame;
}
else if (anotherTimer == nil)
{
[NSException raise: NSInvalidArgumentException
format: @"nil argument for compare:"];
}
else
{
return [_date compare: ((NSTimer*)anotherTimer)->_date];
}
return 0;
}
@end