Merge pull request #9 from gunyarakun/fix-typo
[cocotron.git] / Foundation / NSMethodSignature.m
blobec0bf7dd8a9604923e1fc9ca6f6676300605c7fc
1 /* Copyright (c) 2006-2007 Christopher J. W. Lloyd
3 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
9 #import <Foundation/NSMethodSignature.h>
10 #import <Foundation/NSArray.h>
11 #import <Foundation/NSMapTable.h>
12 #import <Foundation/NSRaise.h>
13 #import <Foundation/NSStringHashing.h>
14 #import <Foundation/NSCoder.h>
15 #include <string.h>
17 @implementation NSMethodSignature
19 -initWithTypes:(const char *)typesCString {
20    const char *next,*last;
21    NSUInteger  size,align;
22    BOOL        first=YES;
23    size_t      typesCStringLength=strlen(typesCString);
24    char       *types[typesCStringLength]; // at most strlen arguments
25    
26     // not guaranteed that typesCString is static
27    _typesCString=NSZoneMalloc(NULL,typesCStringLength+1);
28    strcpy(_typesCString,typesCString);
29    next=last=_typesCString;
30    _returnType=NULL;
31    _numberOfArguments=0;
33    while((next=NSGetSizeAndAlignment(next,&size,&align))!=last){
34     NSUInteger length=next-last;
35     char      *nextCString=NSZoneMalloc(NULL,length+1);
36     
37     strncpy(nextCString,last,length);
38     nextCString[length]='\0';
40     if(first)
41      _returnType=nextCString;
42     else {
43      types[_numberOfArguments]=nextCString;
44      _numberOfArguments++;
45     }
46     
47     first=NO;
49     while((*next>='0' && *next<='9') || *next=='+' || *next=='-' || *next=='?')
50      next++; 
52     if(*next=='\0')
53       break;
55     last=next;
56    }
57    
58    if(_numberOfArguments){  
59     _types=NSZoneMalloc(NULL,_numberOfArguments*sizeof(char *));
60     
61     NSInteger i;
62     
63     for(i=0;i<_numberOfArguments;i++)
64      _types[i]=types[i];
65    }
66    
67    return self;
70 -(void)dealloc {
71    NSZoneFree(NULL,_typesCString);
72    
73    if(_returnType!=NULL)
74     NSZoneFree(NULL,_returnType);
75    
76    NSInteger i;
77    if(_types!=NULL){
78     for(i=0;i<_numberOfArguments;i++)
79      NSZoneFree(NULL,_types[i]);
80     NSZoneFree(NULL,_types);
81    }
82    
83    NSDeallocateObject(self);
84    return;
85    [super dealloc];
88 +(NSMethodSignature *)signatureWithObjCTypes:(const char *)typesCString {
89    return [[[NSMethodSignature allocWithZone:NULL] initWithTypes:typesCString] autorelease];
92 -(NSString *)description {
93    return [NSString stringWithFormat:@"<NSMethodSignature: -(%s)%s>",_returnType,_typesCString];
96 -(NSUInteger)hash {
97    return NSStringHashZeroTerminatedASCII(_typesCString);
100 -(BOOL)isEqual:otherObject {
101    if(self==otherObject)
102     return YES;
104    if([otherObject isKindOfClass:[NSMethodSignature class]]){
105     NSMethodSignature *other=otherObject;
107     return (strcmp(_typesCString,other->_typesCString)==0)?YES:NO;
108    }
110    return NO;
113 -(BOOL)isOneway {
114    return (_returnType!=NULL && _returnType[0]=='V');
117 -(NSUInteger)frameLength {
118    NSUInteger result=0;
119    NSInteger  i;
121    for(i=0;i<_numberOfArguments;i++){
122     NSUInteger align;
123     NSUInteger naturalSize;
124     NSUInteger promotedSize;
126     NSGetSizeAndAlignment(_types[i],&naturalSize,&align);
127     promotedSize=((naturalSize+sizeof(long)-1)/sizeof(long))*sizeof(long);
129     result+=promotedSize;
130    }
131    return result;
134 -(NSUInteger)methodReturnLength {
135    NSUInteger size,align;
137    NSGetSizeAndAlignment(_returnType,&size,&align);
139    return size;
142 -(const char *)methodReturnType {
143    return _returnType;
146 -(NSUInteger)numberOfArguments {
147    return _numberOfArguments;
150 -(const char *)getArgumentTypeAtIndex:(NSUInteger)index {
151    if(index>=_numberOfArguments){
152     [NSException raise:NSInvalidArgumentException format:@"index (%d) is beyond number of arguments (%d)",index,_numberOfArguments];
153     return NULL;
154    }
155    
156    return _types[index];
159 @end