2001-12-17 14:31:42 +00:00
|
|
|
/** Implementation of NSProtocolChecker for GNUStep
|
1998-06-16 13:52:57 +00:00
|
|
|
Copyright (C) 1995 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by: Mike Kienenberger
|
|
|
|
Date: Jun 1998
|
|
|
|
|
|
|
|
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 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
|
1999-09-09 02:56:20 +00:00
|
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
|
2001-12-18 16:54:15 +00:00
|
|
|
|
|
|
|
<title>NSProtocolChecker class reference</title>
|
|
|
|
$Date$ $Revision$
|
1998-06-16 13:52:57 +00:00
|
|
|
*/
|
|
|
|
|
2000-03-24 05:40:19 +00:00
|
|
|
#include "config.h"
|
2001-10-13 08:51:54 +00:00
|
|
|
#include <base/preface.h>
|
|
|
|
#include <Foundation/NSProtocolChecker.h>
|
|
|
|
#include <Foundation/NSException.h>
|
|
|
|
#include <Foundation/NSInvocation.h>
|
|
|
|
#include <Foundation/NSMethodSignature.h>
|
1998-06-16 13:52:57 +00:00
|
|
|
|
|
|
|
@implementation NSProtocolChecker
|
|
|
|
|
2001-12-12 14:10:13 +00:00
|
|
|
/*
|
|
|
|
* Allocates and initializes an NSProtocolChecker instance that will
|
|
|
|
* forward any messages in the aProtocol protocol to anObject, its
|
|
|
|
* target. Thus, the checker can be vended in lieu of anObject to
|
|
|
|
* restrict the messages that can be sent to anObject. Returns the
|
|
|
|
* new instance.
|
|
|
|
*/
|
1998-06-16 13:52:57 +00:00
|
|
|
|
2001-12-12 14:10:13 +00:00
|
|
|
+ (id) protocolCheckerWithTarget: (NSObject*)anObject
|
|
|
|
protocol: (Protocol*)aProtocol
|
1998-06-16 13:52:57 +00:00
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
return AUTORELEASE([[NSProtocolChecker alloc] initWithTarget: anObject
|
2001-12-12 14:10:13 +00:00
|
|
|
protocol: aProtocol]);
|
1998-06-16 13:52:57 +00:00
|
|
|
}
|
|
|
|
|
2001-12-12 14:10:13 +00:00
|
|
|
/*
|
|
|
|
* Forwards any message to the delegate if the method is declared in
|
|
|
|
* the checker's protocol; otherwise raises an NSInvalidArgumentException.
|
|
|
|
*/
|
|
|
|
- (void) forwardInvocation: (NSInvocation*)anInvocation
|
1998-06-16 13:52:57 +00:00
|
|
|
{
|
2001-12-12 14:10:13 +00:00
|
|
|
unsigned int length;
|
|
|
|
void *buffer;
|
1998-06-16 13:52:57 +00:00
|
|
|
|
|
|
|
if ((struct objc_method_description *)NULL
|
2001-12-12 14:10:13 +00:00
|
|
|
!= [self methodDescriptionForSelector: [anInvocation selector]])
|
1999-07-03 19:59:44 +00:00
|
|
|
[[NSException exceptionWithName: NSInvalidArgumentException
|
|
|
|
reason: @"Method not declared in current protocol"
|
2001-12-12 14:10:13 +00:00
|
|
|
userInfo: nil] raise];
|
1998-06-16 13:52:57 +00:00
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
[anInvocation invokeWithTarget: _myTarget];
|
1998-06-16 13:52:57 +00:00
|
|
|
|
|
|
|
length = [[anInvocation methodSignature] methodReturnLength];
|
|
|
|
buffer = (void *)malloc(length);
|
1999-07-03 19:59:44 +00:00
|
|
|
[anInvocation getReturnValue: buffer];
|
1998-06-16 13:52:57 +00:00
|
|
|
|
|
|
|
if (0 == strcmp([[anInvocation methodSignature] methodReturnType],
|
|
|
|
[[anInvocation methodSignatureForSelector:
|
1999-07-03 19:59:44 +00:00
|
|
|
@selector(init: )] methodReturnType]) )
|
1998-06-16 13:52:57 +00:00
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
if (((id)buffer) == _myTarget)
|
1998-06-16 13:52:57 +00:00
|
|
|
{
|
|
|
|
((id)buffer) = self;
|
|
|
|
[anInvocation setReturnValue: buffer];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-07-03 19:59:44 +00:00
|
|
|
- (id) init
|
1998-06-16 13:52:57 +00:00
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
_myProtocol = nil;
|
|
|
|
_myTarget = nil;
|
1998-06-16 13:52:57 +00:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2001-12-12 14:10:13 +00:00
|
|
|
/*
|
|
|
|
* Initializes a newly allocated NSProtocolChecker instance that will
|
|
|
|
* forward any messages in the aProtocol protocol to anObject, its
|
|
|
|
* delegate. Thus, the checker can be vended in lieu of anObject to
|
|
|
|
* restrict the messages that can be sent to anObject. If anObject is
|
|
|
|
* allowed to be freed or dereferenced by clients, the free method
|
|
|
|
* should be included in aProtocol. Returns the new instance.
|
|
|
|
*/
|
|
|
|
- (id) initWithTarget: (NSObject*)anObject protocol: (Protocol*)aProtocol
|
1998-06-16 13:52:57 +00:00
|
|
|
{
|
|
|
|
[super init];
|
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
_myProtocol = aProtocol;
|
1998-06-16 13:52:57 +00:00
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
ASSIGN(_myTarget, anObject);
|
1998-06-16 13:52:57 +00:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2001-12-12 14:10:13 +00:00
|
|
|
/*
|
|
|
|
* Returns an Objective C description for a method in the checker's
|
|
|
|
* protocol, or NULL if aSelector isn't declared as an instance method
|
|
|
|
* in the protocol.
|
|
|
|
*/
|
|
|
|
- (struct objc_method_description*) methodDescriptionForSelector: (SEL)aSelector
|
1998-06-16 13:52:57 +00:00
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
return [_myProtocol descriptionForInstanceMethod: aSelector];
|
1998-06-16 13:52:57 +00:00
|
|
|
}
|
|
|
|
|
2001-12-12 14:10:13 +00:00
|
|
|
/*
|
|
|
|
* Returns the protocol object the checker uses to verify whether a
|
|
|
|
* given message should be forwarded to its delegate, or the protocol
|
|
|
|
* checker should raise an NSInvalidArgumentException.
|
|
|
|
*/
|
|
|
|
- (Protocol*) protocol
|
1998-06-16 13:52:57 +00:00
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
if (nil == _myProtocol)
|
1999-07-03 19:59:44 +00:00
|
|
|
[[NSException exceptionWithName: NSInvalidArgumentException
|
|
|
|
reason: @"No protocol specified"
|
2001-12-12 14:10:13 +00:00
|
|
|
userInfo: nil] raise];
|
1998-06-16 13:52:57 +00:00
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
return _myProtocol;
|
1998-06-16 13:52:57 +00:00
|
|
|
}
|
|
|
|
|
2001-12-12 14:10:13 +00:00
|
|
|
/*
|
|
|
|
* Returns the target of the NSProtocolChecker.
|
|
|
|
*/
|
|
|
|
- (NSObject*) target
|
1998-06-16 13:52:57 +00:00
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
return _myTarget;
|
1998-06-16 13:52:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|