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
|
2004-04-25 07:06:41 +00:00
|
|
|
Rewrite: Richard Frith-Macdonald
|
|
|
|
Date: April 2004
|
|
|
|
|
1998-06-16 13:52:57 +00:00
|
|
|
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.
|
2004-04-25 07:06:41 +00:00
|
|
|
|
1998-06-16 13:52:57 +00:00
|
|
|
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.
|
2004-04-25 07:06:41 +00:00
|
|
|
|
1998-06-16 13:52:57 +00:00
|
|
|
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$
|
2004-04-25 07:06:41 +00:00
|
|
|
*/
|
1998-06-16 13:52:57 +00:00
|
|
|
|
2000-03-24 05:40:19 +00:00
|
|
|
#include "config.h"
|
2003-07-31 23:49:32 +00:00
|
|
|
#include "GNUstepBase/preface.h"
|
2003-06-07 01:24:41 +00:00
|
|
|
#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
|
|
|
|
|
2004-04-25 07:06:41 +00:00
|
|
|
/**
|
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.
|
|
|
|
*/
|
|
|
|
+ (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
|
|
|
}
|
|
|
|
|
2004-04-25 07:06:41 +00:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
DESTROY(_myTarget);
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2004-04-25 07:40:19 +00:00
|
|
|
if (GSObjCIsInstance(_myTarget))
|
1998-06-16 13:52:57 +00:00
|
|
|
{
|
2004-04-25 07:40:19 +00:00
|
|
|
if (![_myProtocol descriptionForInstanceMethod: [anInvocation selector]])
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"<%s -%@> not declared",
|
|
|
|
[_myProtocol name], NSStringFromSelector([anInvocation selector])];
|
|
|
|
}
|
2004-04-25 07:06:41 +00:00
|
|
|
}
|
2004-04-25 07:40:19 +00:00
|
|
|
else
|
2004-04-25 07:06:41 +00:00
|
|
|
{
|
2004-04-25 07:40:19 +00:00
|
|
|
if (![_myProtocol descriptionForClassMethod: [anInvocation selector]])
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"<%s +%@> not declared",
|
|
|
|
[_myProtocol name], NSStringFromSelector([anInvocation selector])];
|
|
|
|
}
|
1998-06-16 13:52:57 +00:00
|
|
|
}
|
2004-04-25 07:06:41 +00:00
|
|
|
[anInvocation invokeWithTarget: _myTarget];
|
|
|
|
}
|
1998-06-16 13:52:57 +00:00
|
|
|
|
1999-07-03 19:59:44 +00:00
|
|
|
- (id) init
|
1998-06-16 13:52:57 +00:00
|
|
|
{
|
2004-04-25 07:06:41 +00:00
|
|
|
self = [self initWithTarget: nil protocol: nil];
|
1998-06-16 13:52:57 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2004-04-25 07:06:41 +00:00
|
|
|
/**
|
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
|
|
|
{
|
2004-04-25 07:40:19 +00:00
|
|
|
_myProtocol = aProtocol;
|
|
|
|
ASSIGN(_myTarget, anObject);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IMP) methodForSelector: (SEL)aSelector
|
|
|
|
{
|
|
|
|
return get_imp(GSObjCClass((id)self), aSelector);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSMethodSignature*) methodSignatureForSelector: (SEL)aSelector
|
|
|
|
{
|
|
|
|
if (aSelector == _cmd || [self respondsToSelector: aSelector] == YES)
|
2004-04-25 07:06:41 +00:00
|
|
|
{
|
2004-04-25 07:40:19 +00:00
|
|
|
return [_myTarget methodSignatureForSelector: aSelector];
|
2004-04-25 07:06:41 +00:00
|
|
|
}
|
2004-04-25 07:40:19 +00:00
|
|
|
return nil;
|
1998-06-16 13:52:57 +00:00
|
|
|
}
|
|
|
|
|
2004-04-25 07:06:41 +00:00
|
|
|
/**
|
2001-12-12 14:10:13 +00:00
|
|
|
* Returns the protocol object the checker uses to verify whether a
|
2004-04-25 07:06:41 +00:00
|
|
|
* given message should be forwarded to its delegate.
|
2001-12-12 14:10:13 +00:00
|
|
|
*/
|
|
|
|
- (Protocol*) protocol
|
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
|
|
|
}
|
|
|
|
|
2004-04-25 07:40:19 +00:00
|
|
|
- (BOOL) respondsToSelector: (SEL)aSelector
|
|
|
|
{
|
|
|
|
if (GSObjCIsInstance(_myTarget))
|
|
|
|
{
|
|
|
|
if ([_myProtocol descriptionForInstanceMethod: aSelector])
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ([_myProtocol descriptionForClassMethod: aSelector])
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2004-04-25 07:06:41 +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
|