mirror of
https://github.com/gnustep/libs-base.git
synced 2025-05-30 00:11:26 +00:00
New file.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@1217 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
b69f76e3d0
commit
1b2de183e8
3 changed files with 246 additions and 0 deletions
56
Headers/gnustep/base/NSRunLoop.h
Normal file
56
Headers/gnustep/base/NSRunLoop.h
Normal file
|
@ -0,0 +1,56 @@
|
|||
/* Interface for NSRunLoop for GNUStep
|
||||
Copyright (C) 1996 Free Software Foundation, Inc.
|
||||
|
||||
Written by: R. Andrew McCallum <mccallum@gnu.ai.mit.edu>
|
||||
Created: March 1996
|
||||
|
||||
This file is part of the GNU Objective C Class Library.
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
#ifndef __NSRunLoop_h_OBJECTS_INCLUDE
|
||||
#define __NSRunLoop_h_OBJECTS_INCLUDE
|
||||
|
||||
#include <objects/stdobjects.h>
|
||||
|
||||
@class NSString;
|
||||
|
||||
@interface NSRunLoop : NSObject
|
||||
@end
|
||||
|
||||
/* Put this in a category to avoid unimportant errors due to behaviors. */
|
||||
@interface NSRunLoop (GNUstep)
|
||||
|
||||
/* Getting this thread's current run loop */
|
||||
+ (NSRunLoop*) currentRunLoop;
|
||||
- (NSString*) currentMode;
|
||||
- (NSDate*) limitDateForMode: (NSString*)mode
|
||||
|
||||
/* Adding timers. */
|
||||
- (void) addTimer: (NSTimer*)timer forMode: (NSString*)mode;
|
||||
|
||||
/* Running a run loop. */
|
||||
- (void) acceptInputForMode: (NSString*)mode beforeDate: (NSDate*)limit_date;
|
||||
- (void) run;
|
||||
- (BOOL) runMode: (NSString*)mode beforeDate: (NSDate*)limit_date;
|
||||
- (void) runUntilDate: (NSDate*)limit_date;
|
||||
|
||||
@end
|
||||
|
||||
extern NSString *NSDefaultRunLoopMode;
|
||||
extern NSString *NSConnectionReplyMode;
|
||||
|
||||
#endif /*__NSRunLoop_h_OBJECTS_INCLUDE */
|
133
Source/NotificationQueue.m
Normal file
133
Source/NotificationQueue.m
Normal file
|
@ -0,0 +1,133 @@
|
|||
/* Implementation of object for queuing Notification objects
|
||||
Copyright (C) 1996 Free Software Foundation, Inc.
|
||||
|
||||
Written by: R. Andrew McCallum <mccallum@gnu.ai.mit.edu>
|
||||
Created: March 1996
|
||||
|
||||
This file is part of the GNU Objective C Class Library.
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
/* The implementation for NotificationDispatcher. */
|
||||
|
||||
#include <objects/stdobjects.h>
|
||||
#include <objects/NotificationQueue.h>
|
||||
#include <objects/Notification.h>
|
||||
#include <objects/LinkedListNode.h>
|
||||
#include <objects/Array.h>
|
||||
#include <objects/Invocation.h>
|
||||
#include <Foundation/NSException.h>
|
||||
|
||||
@implementation NotificationQueue
|
||||
|
||||
/* This is the default initialzer. */
|
||||
- initWithNotificationDistributor: nd
|
||||
{
|
||||
[super init];
|
||||
_notification_distributor = nd;
|
||||
_asap_queue = [Queue new];
|
||||
_idle_queue = [Queue new];
|
||||
return self;
|
||||
}
|
||||
|
||||
- init
|
||||
{
|
||||
return [self initWithNotificationDistributor:
|
||||
[NotificationDistributor defaultInstance]];
|
||||
}
|
||||
|
||||
- (void) dequeueNotificationsMatching: notification
|
||||
coalesceMask: (unsigned)mask
|
||||
{
|
||||
/* Remove from the queues notifications that match NOTIFICATION, according
|
||||
the the MASK. */
|
||||
void remove_matching_from_queue (id q)
|
||||
{
|
||||
int i;
|
||||
for (i = [q count] - 1; i >= 0; i--)
|
||||
{
|
||||
id n = [q objectAtIndex: i];
|
||||
/* If the aspects that need to match do match... */
|
||||
if (( !(mask & NSNotificationCoalescingOnName)
|
||||
|| [[notification name] isEqual: [n name]])
|
||||
&&
|
||||
( !(mask & NSNotificationCoalescingOnSender)
|
||||
|| [[notification object] isEqual: [n object]]))
|
||||
/* ...then remove it. */
|
||||
[q removeObjectAtIndex: i];
|
||||
}
|
||||
}
|
||||
remove_matching_from_queue (_asap_queue);
|
||||
remove_matching_from_queue (_idle_queue);
|
||||
}
|
||||
|
||||
- (void) enqueueNotification: notification
|
||||
postingStyle: (NSPostingStyle)style
|
||||
coalesceMask: (unsigned)mask
|
||||
forModes: (id <ConstantCollecting>)modes
|
||||
{
|
||||
[self dequeueNotificationsMatching: notification
|
||||
coalesceMask: mask];
|
||||
|
||||
switch (style)
|
||||
{
|
||||
case NSPostIdle:
|
||||
[_idle_queue enqueueObject: notification];
|
||||
break;
|
||||
case NSPostASAP:
|
||||
[_asap_queue enqueueObject: notification];
|
||||
break;
|
||||
case NSPostNow:
|
||||
if ([modes containsObject: [RunLoop mode]])
|
||||
[_notification_distributor postNotification: notification];
|
||||
else
|
||||
/* xxx This is the correct behavior? */
|
||||
[_asap_queue enqueueObject: notification];
|
||||
break;
|
||||
default:
|
||||
[self error: "Bad posting style."];
|
||||
}
|
||||
}
|
||||
|
||||
- (void) enqueueNotification: notification
|
||||
postingStyle: (NSPostingStyle)style
|
||||
{
|
||||
[self enqueueNotification: notification
|
||||
postingStyle: style
|
||||
coalesceMask: (NSNotificationCoalescingOnName
|
||||
| NSNotificationCoalescingOnSender)
|
||||
modes: nil];
|
||||
}
|
||||
|
||||
|
||||
- (void) postNotificationsWithASAPStyle
|
||||
{
|
||||
id n;
|
||||
int i, c = [_asap_queue count];
|
||||
|
||||
for (i = 0; i < c; i++)
|
||||
[_notification_dispatcher postNotification:
|
||||
[_idle_queue objectAtIndex: i]];
|
||||
[_asap_queue empty];
|
||||
}
|
||||
|
||||
- (void) postNotificationWithIdleStyle
|
||||
{
|
||||
id n = [_idle_queue dequeueObject];
|
||||
[_notification_dispatcher postNotification: n];
|
||||
}
|
||||
|
||||
@end
|
57
Testing/nstimer.m
Normal file
57
Testing/nstimer.m
Normal file
|
@ -0,0 +1,57 @@
|
|||
#include <objects/RunLoop.h>
|
||||
#include <objects/Invocation.h>
|
||||
#include <Foundation/NSTimer.h>
|
||||
|
||||
@interface TestDouble : NSObject
|
||||
+ (double) testDouble;
|
||||
- (double) testDoubleInstance;
|
||||
@end
|
||||
@implementation TestDouble
|
||||
+ (double) testDouble
|
||||
{
|
||||
return 12345678912345.0;
|
||||
}
|
||||
- (double) testDoubleInstance
|
||||
{
|
||||
return 92345678912345.0;
|
||||
}
|
||||
@end
|
||||
|
||||
double test_double ()
|
||||
{
|
||||
return 92345678912345.0;
|
||||
}
|
||||
|
||||
void say_count ()
|
||||
{
|
||||
static int count = 0;
|
||||
printf ("Timer fired %d times\n", ++count);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
volatile double foo, bar;
|
||||
id inv = [[VoidFunctionInvocation alloc] initWithVoidFunction: say_count];
|
||||
id o;
|
||||
id d;
|
||||
|
||||
foo = [TestDouble testDouble];
|
||||
printf ("TestDouble is %f\n", foo);
|
||||
foo = [TestDouble testDouble];
|
||||
printf ("TestDouble 2 is %f\n", foo);
|
||||
o = [[TestDouble alloc] init];
|
||||
bar = [o testDoubleInstance];
|
||||
printf ("testDouble is %f\n", bar);
|
||||
|
||||
foo = test_double ();
|
||||
printf ("test_double is %f\n", foo);
|
||||
|
||||
d = [NSDate date];
|
||||
printf ("time interval since now %f\n", [d timeIntervalSinceNow]);
|
||||
|
||||
[NSTimer scheduledTimerWithTimeInterval: 3.0
|
||||
invocation: inv
|
||||
repeats: YES];
|
||||
[RunLoop run];
|
||||
exit (0);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue