1996-03-12 15:39:13 +00:00
|
|
|
/* Implementation of NSTimer for GNUstep
|
|
|
|
Copyright (C) 1995, 1996 Free Software Foundation, Inc.
|
|
|
|
|
1996-04-17 20:17:45 +00:00
|
|
|
Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
|
1996-03-12 15:39:13 +00:00
|
|
|
Created: March 1996
|
|
|
|
|
1996-05-12 00:56:10 +00:00
|
|
|
This file is part of the GNUstep Base Library.
|
1996-03-12 15:39:13 +00:00
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library 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
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with this library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
1997-11-06 00:51:23 +00:00
|
|
|
#include <config.h>
|
1996-03-12 15:39:13 +00:00
|
|
|
#include <Foundation/NSTimer.h>
|
|
|
|
#include <Foundation/NSDate.h>
|
|
|
|
#include <Foundation/NSException.h>
|
1997-09-01 21:59:51 +00:00
|
|
|
#include <Foundation/NSRunLoop.h>
|
1996-04-17 15:23:00 +00:00
|
|
|
#include <gnustep/base/Invocation.h>
|
1996-03-12 15:39:13 +00:00
|
|
|
|
|
|
|
@implementation NSTimer
|
|
|
|
|
|
|
|
/* This is the designated initializer. */
|
|
|
|
- initWithTimeInterval: (NSTimeInterval)seconds
|
|
|
|
targetOrInvocation: t
|
|
|
|
selector: (SEL)sel
|
|
|
|
userInfo: info
|
|
|
|
repeats: (BOOL)f
|
|
|
|
{
|
|
|
|
[super init];
|
|
|
|
_interval = seconds;
|
|
|
|
_fire_date = [[NSDate alloc] initWithTimeIntervalSinceNow: seconds];
|
|
|
|
_retain_count = 0;
|
|
|
|
_is_valid = YES;
|
|
|
|
_target = t;
|
|
|
|
_selector = sel;
|
|
|
|
_info = info;
|
|
|
|
_repeats = f;
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[_fire_date release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ timerWithTimeInterval: (NSTimeInterval)ti
|
|
|
|
invocation: invocation
|
|
|
|
repeats: (BOOL)f
|
|
|
|
{
|
|
|
|
return [[[self alloc] initWithTimeInterval: ti
|
|
|
|
targetOrInvocation: invocation
|
|
|
|
selector: NULL
|
|
|
|
userInfo: nil
|
|
|
|
repeats: f]
|
|
|
|
autorelease];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ timerWithTimeInterval: (NSTimeInterval)ti
|
|
|
|
target: object
|
|
|
|
selector: (SEL)selector
|
|
|
|
userInfo: info
|
|
|
|
repeats: (BOOL)f
|
|
|
|
{
|
|
|
|
return [[[self alloc] initWithTimeInterval: ti
|
|
|
|
targetOrInvocation: object
|
|
|
|
selector: selector
|
|
|
|
userInfo: info
|
|
|
|
repeats: f]
|
|
|
|
autorelease];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ scheduledTimerWithTimeInterval: (NSTimeInterval)ti
|
|
|
|
invocation: invocation
|
|
|
|
repeats: (BOOL)f
|
|
|
|
{
|
|
|
|
id t = [self timerWithTimeInterval: ti
|
|
|
|
invocation: invocation
|
|
|
|
repeats: f];
|
1997-09-01 21:59:51 +00:00
|
|
|
[[NSRunLoop currentRunLoop] addTimer: t forMode: NSDefaultRunLoopMode];
|
1996-03-12 15:39:13 +00:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ scheduledTimerWithTimeInterval: (NSTimeInterval)ti
|
|
|
|
target: object
|
|
|
|
selector: (SEL)selector
|
|
|
|
userInfo: info
|
|
|
|
repeats: (BOOL)f
|
|
|
|
{
|
|
|
|
id t = [self timerWithTimeInterval: ti
|
|
|
|
target: object
|
|
|
|
selector: selector
|
|
|
|
userInfo: info
|
|
|
|
repeats: f];
|
1997-09-01 21:59:51 +00:00
|
|
|
[[NSRunLoop currentRunLoop] addTimer: t forMode: NSDefaultRunLoopMode];
|
1996-03-12 15:39:13 +00:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void) fire
|
|
|
|
{
|
|
|
|
if (_selector)
|
1998-09-28 20:38:02 +00:00
|
|
|
[_target performSelector: _selector withObject: self];
|
1996-03-12 15:39:13 +00:00
|
|
|
else
|
|
|
|
[_target invoke];
|
|
|
|
|
|
|
|
if (!_repeats)
|
|
|
|
[self invalidate];
|
|
|
|
else if (_is_valid)
|
|
|
|
{
|
|
|
|
NSTimeInterval ti = [_fire_date timeIntervalSinceReferenceDate];
|
|
|
|
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
|
|
|
|
assert (now < 0.0);
|
|
|
|
while (ti < now) // xxx remove this
|
|
|
|
ti += _interval;
|
|
|
|
[_fire_date release];
|
|
|
|
_fire_date = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate: ti];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) invalidate
|
|
|
|
{
|
|
|
|
assert (_is_valid);
|
|
|
|
_is_valid = NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isValid
|
|
|
|
{
|
|
|
|
return _is_valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
- fireDate
|
|
|
|
{
|
|
|
|
return _fire_date;
|
|
|
|
}
|
|
|
|
|
|
|
|
- userInfo
|
|
|
|
{
|
|
|
|
return _info;
|
|
|
|
}
|
|
|
|
|
1997-01-11 21:35:35 +00:00
|
|
|
- (int)compare:(NSTimer*)anotherTimer
|
|
|
|
{
|
|
|
|
return [_fire_date compare: anotherTimer->_fire_date];
|
|
|
|
}
|
1996-03-12 15:39:13 +00:00
|
|
|
@end
|