Oolite 1.91.0.7645-241119-222d325
Loading...
Searching...
No Matches
OOScriptTimer Class Reference

#include <OOScriptTimer.h>

+ Inheritance diagram for OOScriptTimer:
+ Collaboration diagram for OOScriptTimer:

Instance Methods

(id) - initWithNextTime:interval:
 
(id) - initOneShotTimerWithDelay:
 
(OOTimeAbsolute- nextTime
 
(BOOL) - setNextTime:
 
(OOTimeDelta- interval
 
(void) - setInterval:
 
(void) - timerFired
 
(BOOL) - scheduleTimer
 
(void) - unscheduleTimer
 
(BOOL) - isScheduled
 
(BOOL) - isValidForScheduling
 
(NSComparisonResult) - compareByNextFireTime:
 
(void) - dealloc [implementation]
 
(NSString *) - descriptionComponents [implementation]
 

Class Methods

(void) + updateTimers
 
(void) + noteGameReset
 

Private Attributes

OOTimeAbsolute _nextTime
 
OOTimeDelta _interval
 
BOOL _isScheduled
 
BOOL _hasBeenRun
 

Detailed Description

Definition at line 39 of file OOScriptTimer.h.

Method Documentation

◆ compareByNextFireTime:

- (NSComparisonResult) compareByNextFireTime: (OOScriptTimer *) other

Definition at line 36 of file OOScriptTimer.m.

240 :(OOScriptTimer *)other
241{
242 OOTimeAbsolute otherTime = -INFINITY;
243
244 @try
245 {
246 if (other != nil) otherTime = [other nextTime];
247 }
248 @catch (NSException *exception)
249 {
250 OOLog(kOOLogException, @"\n\n***** Ignoring Timer Exception: %@ : %@ *****\n\n",[exception name], [exception reason]);
251 }
252
253 if (_nextTime < otherTime) return NSOrderedAscending;
254 else if (_nextTime > otherTime) return NSOrderedDescending;
255 else return NSOrderedSame;
256}
NSString *const kOOLogException
Definition OOLogging.m:651
#define OOLog(class, format,...)
Definition OOLogging.h:88
return nil
double OOTimeAbsolute
Definition OOTypes.h:223
OOTimeAbsolute _nextTime
OOTimeAbsolute nextTime()

References _hasBeenRun, _interval, _nextTime, interval, nextTime, and nil.

+ Here is the call graph for this function:

◆ dealloc

- (void) dealloc
implementation

Reimplemented in OOJSTimer.

Definition at line 36 of file OOScriptTimer.m.

77{
78 if (_isScheduled) [self unscheduleTimer];
79
80 [super dealloc];
81}

◆ descriptionComponents

- (NSString *) descriptionComponents
implementation

Reimplemented in OOJSTimer.

Definition at line 36 of file OOScriptTimer.m.

85{
86 NSString *intervalDesc = nil;
87
88 if (_interval <= 0.0) intervalDesc = @"one-shot";
89 else intervalDesc = [NSString stringWithFormat:@"interval: %g", _interval];
90
91 return [NSString stringWithFormat:@"nextTime: %g, %@, %srunning", _nextTime, intervalDesc, _isScheduled ? "" : "not "];
92}
OOTimeDelta _interval

◆ initOneShotTimerWithDelay:

- (id) initOneShotTimerWithDelay: (OOTimeDelta) delay

Definition at line 36 of file OOScriptTimer.m.

70 :(OOTimeDelta)delay
71{
72 return [self initWithNextTime:[UNIVERSE getTime] + delay interval:-1.0];
73}
double OOTimeDelta
Definition OOTypes.h:224

◆ initWithNextTime:interval:

- (id) initWithNextTime: (OOTimeAbsolute) nextTime
interval: (OOTimeDelta) interval 

Definition at line 36 of file OOScriptTimer.m.

43{
45
46 if ((self = [super init]))
47 {
48 if (interval <= 0.0) interval = -1.0;
49
50 now = [UNIVERSE getTime];
51 if (nextTime < 0.0) nextTime = now + interval;
52 if (nextTime < now && interval < 0)
53 {
54 // Negative or old nextTime and negative interval = meaningless.
55 [self release];
56 self = nil;
57 }
58 else
59 {
62 _hasBeenRun = NO;
63 }
64 }
65
66 return self;
67}
OOTimeDelta interval()

◆ interval

- (OOTimeDelta) interval

Definition at line 36 of file OOScriptTimer.m.

111{
112 return _interval;
113}

Referenced by compareByNextFireTime:.

+ Here is the caller graph for this function:

◆ isScheduled

- (BOOL) isScheduled

Definition at line 36 of file OOScriptTimer.m.

159{
160 return _isScheduled;
161}

◆ isValidForScheduling

- (BOOL) isValidForScheduling

Definition at line 36 of file OOScriptTimer.m.

217{
218 OOTimeAbsolute now;
219 double scaled;
220
221 now = [UNIVERSE getTime];
222 if (_nextTime <= now)
223 {
224 if (_interval <= 0.0 && _hasBeenRun) return NO; // One-shot timer which has expired
225
226 // Move _nextTime to the closest future time that's a multiple of _interval
227 scaled = (now - _nextTime) / _interval;
228 scaled = ceil(scaled);
229 _nextTime += scaled * _interval;
230 if (_nextTime <= now && _hasBeenRun)
231 {
232 // Should only happen if _nextTime is exactly equal to now after previous stuff
234 }
235 }
236
237 return YES;
238}

◆ nextTime

- (OOTimeAbsolute) nextTime

Definition at line 36 of file OOScriptTimer.m.

96{
97 return _nextTime;
98}

Referenced by compareByNextFireTime:.

+ Here is the caller graph for this function:

◆ noteGameReset

+ (void) noteGameReset

Definition at line 36 of file OOScriptTimer.m.

202{
203 NSArray *timers = nil;
204 NSEnumerator *timerEnum = nil;
205 OOScriptTimer *timer = nil;
206
207 // Intermediate array is required so we don't get stuck in an endless loop over reinserted timers. Note that -sortedObjects also clears the queue!
208 timers = [sTimers sortedObjects];
209 for (timerEnum = [timers objectEnumerator]; (timer = [timerEnum nextObject]); )
210 {
211 timer->_isScheduled = NO;
212 }
213}

◆ scheduleTimer

- (BOOL) scheduleTimer

Definition at line 36 of file OOScriptTimer.m.

130{
131 if (_isScheduled) return YES;
132 if (![self isValidForScheduling]) return NO;
133
134 if (EXPECT(!sUpdating))
135 {
136 if (EXPECT_NOT(sTimers == nil)) sTimers = [[OOPriorityQueue alloc] initWithComparator:@selector(compareByNextFireTime:)];
137 [sTimers addObject:self];
138 }
139 else
140 {
141 if (sDeferredTimers == nil) sDeferredTimers = [[NSMutableArray alloc] init];
142 [sDeferredTimers addObject:self];
143 }
144
145 _isScheduled = YES;
146 return YES;
147}
#define EXPECT_NOT(x)
#define EXPECT(x)
static NSMutableArray * sDeferredTimers
static BOOL sUpdating
static OOPriorityQueue * sTimers
BOOL isValidForScheduling()

Referenced by TimerConstruct().

+ Here is the caller graph for this function:

◆ setInterval:

- (void) setInterval: (OOTimeDelta) interval

Definition at line 36 of file OOScriptTimer.m.

117{
118 if (interval <= 0.0) interval = -1.0;
120}

Referenced by TimerSetProperty().

+ Here is the caller graph for this function:

◆ setNextTime:

- (BOOL) setNextTime: (OOTimeAbsolute) nextTime

Definition at line 36 of file OOScriptTimer.m.

102{
103 if (_isScheduled) return NO;
104
106 return YES;
107}

◆ timerFired

- (void) timerFired

Reimplemented in OOJSTimer.

Definition at line 36 of file OOScriptTimer.m.

124{
126}
#define OOLogGenericSubclassResponsibility()
Definition OOLogging.h:129

◆ unscheduleTimer

- (void) unscheduleTimer

Definition at line 36 of file OOScriptTimer.m.

151{
152 [sTimers removeExactObject:self];
153 _isScheduled = NO;
154 _hasBeenRun = NO;
155}

Referenced by OOJSTimer::dealloc.

+ Here is the caller graph for this function:

◆ updateTimers

+ (void) updateTimers

Definition at line 36 of file OOScriptTimer.m.

165{
166 OOScriptTimer *timer = nil;
167 OOTimeAbsolute now;
168
169 sUpdating = YES;
170
171 now = [UNIVERSE getTime];
172 for (;;)
173 {
174 timer = [sTimers peekAtNextObject];
175 if (timer == nil || now < [timer nextTime]) break;
176
177 [sTimers removeNextObject];
178
179 // Must fire before rescheduling so that the timer callback can stop itself. -- Ahruman 2011-01-01
180 [timer timerFired];
181
182 timer->_hasBeenRun = YES;
183
184 if (timer->_isScheduled)
185 {
186 timer->_isScheduled = NO;
187 [timer scheduleTimer];
188 }
189 }
190
191 if (sDeferredTimers != nil)
192 {
193 [sTimers addObjects:sDeferredTimers];
195 }
196
197 sUpdating = NO;
198}
#define DESTROY(x)
Definition OOCocoa.h:77

Member Data Documentation

◆ _hasBeenRun

- (BOOL) _hasBeenRun
private

Definition at line 45 of file OOScriptTimer.h.

Referenced by compareByNextFireTime:.

◆ _interval

- (OOTimeDelta) _interval
private

Definition at line 43 of file OOScriptTimer.h.

Referenced by compareByNextFireTime:.

◆ _isScheduled

- (BOOL) _isScheduled
private

Definition at line 44 of file OOScriptTimer.h.

◆ _nextTime

- (OOTimeAbsolute) _nextTime
private

Definition at line 42 of file OOScriptTimer.h.

Referenced by compareByNextFireTime:.


The documentation for this class was generated from the following files: