Oolite
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 38 of file OOScriptTimer.h.

Method Documentation

◆ compareByNextFireTime:

- (NSComparisonResult) compareByNextFireTime: (OOScriptTimer*) other

Definition at line 224 of file OOScriptTimer.m.

224 :(OOScriptTimer*)other
225{
226 OOTimeAbsolute otherTime = -INFINITY;
227
228 @try {
229 if (other != nil)
230 otherTime = [other nextTime];
231 }
232 @catch (NSException* exception) {
233 OOLog(kOOLogException, @"\n\n***** Ignoring Timer Exception: %@ : %@ *****\n\n", [exception name], [exception reason]);
234 }
235
236 if (_nextTime < otherTime)
237 return NSOrderedAscending;
238 else if (_nextTime > otherTime)
239 return NSOrderedDescending;
240 else
241 return NSOrderedSame;
242}
NSString *const kOOLogException
Definition OOLogging.m:600
#define OOLog(class, format,...)
Definition OOLogging.h:84
return nil
double OOTimeAbsolute
Definition OOTypes.h:192
OOTimeAbsolute _nextTime
OOTimeAbsolute nextTime()

References _nextTime, kOOLogException, nextTime, nil, and OOLog.

Here is the call graph for this function:

◆ dealloc

- (void) dealloc
implementation

Reimplemented in OOJSTimer.

Definition at line 71 of file OOScriptTimer.m.

72{
73 if (_isScheduled)
74 [self unscheduleTimer];
75
76 [super dealloc];
77}

References _isScheduled, dealloc, and unscheduleTimer.

Referenced by OOJSTimer::dealloc, and dealloc.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ descriptionComponents

- (NSString *) descriptionComponents
implementation

Reimplemented in OOJSTimer.

Definition at line 79 of file OOScriptTimer.m.

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

References _interval, descriptionComponents, and nil.

Referenced by OOJSTimer::descriptionComponents, and descriptionComponents.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ initOneShotTimerWithDelay:

- (id) initOneShotTimerWithDelay: (OOTimeDelta) delay

Definition at line 66 of file OOScriptTimer.m.

66 :(OOTimeDelta)delay
67{
68 return [self initWithNextTime:[UNIVERSE getTime] + delay interval:-1.0];
69}
double OOTimeDelta
Definition OOTypes.h:193

References initWithNextTime:interval:.

Here is the call graph for this function:

◆ initWithNextTime:interval:

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

Definition at line 39 of file OOScriptTimer.m.

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

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

Referenced by initOneShotTimerWithDelay:, OOJSTimer::initWithDelay:interval:context:function:this:, and OOJSTimer(Private)::initWithDelay:interval:context:function:this:.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ interval

- (OOTimeDelta) interval

Definition at line 105 of file OOScriptTimer.m.

106{
107 return _interval;
108}

References _interval, and interval.

Referenced by initWithNextTime:interval:, interval, setInterval:, and OOJSTimer(Private)::willNotFireAgain.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isScheduled

- (BOOL) isScheduled

Definition at line 150 of file OOScriptTimer.m.

151{
152 return _isScheduled;
153}

References _isScheduled, and isScheduled.

Referenced by isScheduled.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isValidForScheduling

- (BOOL) isValidForScheduling

Definition at line 201 of file OOScriptTimer.m.

202{
203 OOTimeAbsolute now;
204 double scaled;
205
206 now = [UNIVERSE getTime];
207 if (_nextTime <= now) {
208 if (_interval <= 0.0 && _hasBeenRun)
209 return NO; // One-shot timer which has expired
210
211 // Move _nextTime to the closest future time that's a multiple of _interval
212 scaled = (now - _nextTime) / _interval;
213 scaled = ceil(scaled);
214 _nextTime += scaled * _interval;
215 if (_nextTime <= now && _hasBeenRun) {
216 // Should only happen if _nextTime is exactly equal to now after previous stuff
218 }
219 }
220
221 return YES;
222}

References _hasBeenRun, _interval, _nextTime, and isValidForScheduling.

Referenced by isValidForScheduling, and scheduleTimer.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ nextTime

- (OOTimeAbsolute) nextTime

Definition at line 91 of file OOScriptTimer.m.

92{
93 return _nextTime;
94}

References _nextTime, and nextTime.

Referenced by compareByNextFireTime:, initWithNextTime:interval:, nextTime, setNextTime:, and updateTimers.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ noteGameReset

+ (void) noteGameReset

Definition at line 189 of file OOScriptTimer.m.

190{
191 NSArray* timers = nil;
192 OOScriptTimer* timer = nil;
193
194 // Intermediate array is required so we don't get stuck in an endless loop over reinserted timers. Note that -sortedObjects also clears the queue!
195 timers = [sTimers sortedObjects];
196 foreach (timer, timers) {
197 timer->_isScheduled = NO;
198 }
199}
NSArray * sortedObjects()

References _isScheduled, nil, noteGameReset, and OOPriorityQueue::sortedObjects.

Referenced by noteGameReset, and PlayerEntity::setUpAndConfirmOK:saveGame:.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ scheduleTimer

- (BOOL) scheduleTimer

Definition at line 122 of file OOScriptTimer.m.

123{
124 if (_isScheduled)
125 return YES;
126 if (![self isValidForScheduling])
127 return NO;
128
129 if (EXPECT(!sUpdating)) {
130 if (EXPECT_NOT(sTimers == nil))
131 sTimers = [[OOPriorityQueue alloc] initWithComparator:@selector(compareByNextFireTime:)];
132 [sTimers addObject:self];
133 } else {
134 if (sDeferredTimers == nil)
135 sDeferredTimers = [[NSMutableArray alloc] init];
136 [sDeferredTimers addObject:self];
137 }
138
139 _isScheduled = YES;
140 return YES;
141}
#define EXPECT_NOT(x)
#define EXPECT(x)
static NSMutableArray * sDeferredTimers
static BOOL sUpdating
static OOPriorityQueue * sTimers
void addObject:(id object)
BOOL isValidForScheduling()

References _isScheduled, OOPriorityQueue::addObject:, EXPECT, EXPECT_NOT, isValidForScheduling, nil, scheduleTimer, sDeferredTimers, sTimers, and sUpdating.

Referenced by scheduleTimer, TimerConstruct(), TimerConstruct(), and updateTimers.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setInterval:

- (void) setInterval: (OOTimeDelta) interval

Definition at line 110 of file OOScriptTimer.m.

111{
112 if (interval <= 0.0)
113 interval = -1.0;
115}

References _interval, and interval.

Referenced by TimerSetProperty(), and TimerSetProperty().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setNextTime:

- (BOOL) setNextTime: (OOTimeAbsolute) nextTime

Definition at line 96 of file OOScriptTimer.m.

97{
98 if (_isScheduled)
99 return NO;
100
102 return YES;
103}

References _isScheduled, _nextTime, and nextTime.

Here is the call graph for this function:

◆ timerFired

- (void) timerFired

Reimplemented in OOJSTimer.

Definition at line 117 of file OOScriptTimer.m.

118{
120}
#define OOLogGenericSubclassResponsibility()
Definition OOLogging.h:141

References OOLogGenericSubclassResponsibility, and timerFired.

Referenced by timerFired, and updateTimers.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ unscheduleTimer

- (void) unscheduleTimer

Definition at line 143 of file OOScriptTimer.m.

144{
146 _isScheduled = NO;
147 _hasBeenRun = NO;
148}
void removeExactObject:(id object)

References _hasBeenRun, _isScheduled, OOPriorityQueue::removeExactObject:, and unscheduleTimer.

Referenced by dealloc, OOJSTimer::deleteJSPointers, OOJSTimer(Private)::releaseJSValues, OOJSTimer::timerFired, TimerStop(), TimerStop(), and unscheduleTimer.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ updateTimers

+ (void) updateTimers

Definition at line 155 of file OOScriptTimer.m.

156{
157 OOScriptTimer* timer = nil;
158 OOTimeAbsolute now;
159
160 sUpdating = YES;
161
162 now = [UNIVERSE getTime];
163 for (;;) {
164 timer = [sTimers peekAtNextObject];
165 if (timer == nil || now < [timer nextTime])
166 break;
167
169
170 // Must fire before rescheduling so that the timer callback can stop itself. -- Ahruman 2011-01-01
171 [timer timerFired];
172
173 timer->_hasBeenRun = YES;
174
175 if (timer->_isScheduled) {
176 timer->_isScheduled = NO;
177 [timer scheduleTimer];
178 }
179 }
180
181 if (sDeferredTimers != nil) {
182 [sTimers addObjects:sDeferredTimers];
184 }
185
186 sUpdating = NO;
187}
#define DESTROY(x)
void addObjects:(id collection)

References _hasBeenRun, _isScheduled, OOPriorityQueue::addObjects:, DESTROY, nextTime, nil, OOPriorityQueue::peekAtNextObject, OOPriorityQueue::removeNextObject, scheduleTimer, sDeferredTimers, sUpdating, timerFired, and updateTimers.

Referenced by PlayerEntity::setUpAndConfirmOK:saveGame:, PlayerEntity::update:, and updateTimers.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ _hasBeenRun

- (BOOL) _hasBeenRun
private

◆ _interval

- (OOTimeDelta) _interval
private

◆ _isScheduled

- (BOOL) _isScheduled
private

◆ _nextTime

- (OOTimeAbsolute) _nextTime
private

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