OLD | NEW |
1 //------------------------------------------------------------------------------
--------- | 1 /* |
2 // $Id$ | 2 * Copyright (c) 2009-2015 Erik Doernenburg and contributors |
3 // Copyright (c) 2009 by Mulle Kybernetik. See License file for details. | 3 * |
4 //------------------------------------------------------------------------------
--------- | 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 * not use these files except in compliance with the License. You may obtain |
| 6 * a copy of the License at |
| 7 * |
| 8 * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 * |
| 10 * Unless required by applicable law or agreed to in writing, software |
| 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 * License for the specific language governing permissions and limitations |
| 14 * under the License. |
| 15 */ |
5 | 16 |
6 #import "OCMBoxedReturnValueProvider.h" | 17 #import "OCMBoxedReturnValueProvider.h" |
7 | 18 #import "OCMFunctionsPrivate.h" |
8 static const char* OCMTypeWithoutQualifiers(const char* objCType) | 19 #import "NSValue+OCMAdditions.h" |
9 { | |
10 while(strchr("rnNoORV", objCType[0]) != NULL) | |
11 objCType += 1; | |
12 return objCType; | |
13 } | |
14 | |
15 /* | |
16 * Sometimes an external type is an opaque struct (which will have an @encode of
"{structName}" | |
17 * or "{structName=}") but the actual method return type, or property type, will
know the contents | |
18 * of the struct (so will have an objcType of say "{structName=iiSS}". This fun
ction will determine | |
19 * those are equal provided they have the same structure name, otherwise everyth
ing else will be | |
20 * compared textually. This can happen particularly for pointers to such struct
ures, which still | |
21 * encode what is being pointed to. | |
22 */ | |
23 static BOOL OCMTypesEqualAllowOpaqueStructs(const char *type1, const char *type2
) | |
24 { | |
25 type1 = OCMTypeWithoutQualifiers(type1); | |
26 type2 = OCMTypeWithoutQualifiers(type2); | |
27 | |
28 switch (type1[0]) | |
29 { | |
30 case '{': | |
31 case '(': | |
32 { | |
33 if (type2[0] != type1[0]) | |
34 return NO; | |
35 char endChar = type1[0] == '{'? '}' : ')'; | |
36 | |
37 const char *type1End = strchr(type1, endChar); | |
38 const char *type2End = strchr(type2, endChar); | |
39 const char *type1Equals = strchr(type1, '='); | |
40 const char *type2Equals = strchr(type2, '='); | |
41 | |
42 /* Opaque types either don't have an equals sign (just the name and
the end brace), or | |
43 * empty content after the equals sign. | |
44 * We want that to compare the same as a type of the same name but w
ith the content. | |
45 */ | |
46 BOOL type1Opaque = (type1Equals == NULL || (type1End < type1Equals)
|| type1Equals[1] == endChar); | |
47 BOOL type2Opaque = (type2Equals == NULL || (type2End < type2Equals)
|| type2Equals[1] == endChar); | |
48 const char *type1NameEnd = (type1Equals == NULL || (type1End < type1
Equals)) ? type1End : type1Equals; | |
49 const char *type2NameEnd = (type2Equals == NULL || (type2End < type2
Equals)) ? type2End : type2Equals; | |
50 intptr_t type1NameLen = type1NameEnd - type1; | |
51 intptr_t type2NameLen = type2NameEnd - type2; | |
52 | |
53 /* If the names are not equal, return NO */ | |
54 if (type1NameLen != type2NameLen || strncmp(type1, type2, type1NameL
en)) | |
55 return NO; | |
56 | |
57 /* If the same name, and at least one is opaque, that is close enoug
h. */ | |
58 if (type1Opaque || type2Opaque) | |
59 return YES; | |
60 | |
61 /* Otherwise, compare all the elements. Use NSGetSizeAndAlignment t
o walk through the struct elements. */ | |
62 type1 = type1Equals + 1; | |
63 type2 = type2Equals + 1; | |
64 while (type1[0] != endChar && type1[0] != '\0') | |
65 { | |
66 if (!OCMTypesEqualAllowOpaqueStructs(type1, type2)) | |
67 return NO; | |
68 type1 = NSGetSizeAndAlignment(type1, NULL, NULL); | |
69 type2 = NSGetSizeAndAlignment(type2, NULL, NULL); | |
70 } | |
71 return YES; | |
72 } | |
73 case '^': | |
74 /* for a pointer, make sure the other is a pointer, then recursively
compare the rest */ | |
75 if (type2[0] != type1[0]) | |
76 return NO; | |
77 return OCMTypesEqualAllowOpaqueStructs(type1+1, type2+1); | |
78 | |
79 case '?': | |
80 return type2[0] == '?'; | |
81 | |
82 case '\0': | |
83 return type2[0] == '\0'; | |
84 | |
85 default: | |
86 { | |
87 // Move the type pointers past the current types, then compare that
region | |
88 const char *afterType1 = NSGetSizeAndAlignment(type1, NULL, NULL); | |
89 const char *afterType2 = NSGetSizeAndAlignment(type2, NULL, NULL); | |
90 intptr_t type1Len = afterType1 - type1; | |
91 intptr_t type2Len = afterType2 - type2; | |
92 | |
93 return (type1Len == type2Len && (strncmp(type1, type2, type1Len) ==
0)); | |
94 } | |
95 } | |
96 } | |
97 | |
98 static CFNumberType OCMNumberTypeForObjCType(const char *objcType) | |
99 { | |
100 switch (objcType[0]) | |
101 { | |
102 case 'c': return kCFNumberCharType; | |
103 case 'C': return kCFNumberCharType; | |
104 case 'B': return kCFNumberCharType; | |
105 case 's': return kCFNumberShortType; | |
106 case 'S': return kCFNumberShortType; | |
107 case 'i': return kCFNumberIntType; | |
108 case 'I': return kCFNumberIntType; | |
109 case 'l': return kCFNumberLongType; | |
110 case 'L': return kCFNumberLongType; | |
111 case 'q': return kCFNumberLongLongType; | |
112 case 'Q': return kCFNumberLongLongType; | |
113 case 'f': return kCFNumberFloatType; | |
114 case 'd': return kCFNumberDoubleType; | |
115 } | |
116 | |
117 return 0; | |
118 } | |
119 | |
120 static NSNumber *OCMNumberForValue(NSValue *value) | |
121 { | |
122 #define CREATE_NUM(_type, _meth) ({ _type _v; [value getValue:&_v]; [NSNumber _m
eth _v]; }) | |
123 switch ([value objCType][0]) | |
124 { | |
125 case 'c': return CREATE_NUM(char, numberWithChar:); | |
126 case 'C': return CREATE_NUM(unsigned char, numberWithUnsignedChar:)
; | |
127 case 'B': return CREATE_NUM(bool, numberWithBool:); | |
128 case 's': return CREATE_NUM(short, numberWithShort:); | |
129 case 'S': return CREATE_NUM(unsigned short, numberWithUnsignedShort:
); | |
130 case 'i': return CREATE_NUM(int, numberWithInt:); | |
131 case 'I': return CREATE_NUM(unsigned int, numberWithUnsignedInt:); | |
132 case 'l': return CREATE_NUM(long, numberWithLong:); | |
133 case 'L': return CREATE_NUM(unsigned long, numberWithUnsignedLong:)
; | |
134 case 'q': return CREATE_NUM(long long, numberWithLongLong:); | |
135 case 'Q': return CREATE_NUM(unsigned long long, numberWithUnsignedLongLo
ng:); | |
136 case 'f': return CREATE_NUM(float, numberWithFloat:); | |
137 case 'd': return CREATE_NUM(double, numberWithDouble:); | |
138 } | |
139 | |
140 return nil; | |
141 } | |
142 | 20 |
143 @implementation OCMBoxedReturnValueProvider | 21 @implementation OCMBoxedReturnValueProvider |
144 | 22 |
145 - (void)handleInvocation:(NSInvocation *)anInvocation | 23 - (void)handleInvocation:(NSInvocation *)anInvocation |
146 { | 24 { |
147 const char *returnType = [[anInvocation methodSignature] methodReturnType]; | 25 » const char *returnType = [[anInvocation methodSignature] methodReturnTyp
e]; |
148 NSUInteger returnTypeSize = [[anInvocation methodSignature] methodReturnLeng
th]; | 26 NSUInteger returnTypeSize = [[anInvocation methodSignature] methodReturnLeng
th]; |
149 char valueBuffer[returnTypeSize]; | 27 char valueBuffer[returnTypeSize]; |
150 NSValue *value = (NSValue *)returnValue; | 28 NSValue *returnValueAsNSValue = (NSValue *)returnValue; |
151 | 29 |
152 if ([self getBytes:valueBuffer forValue:value compatibleWithType:returnType]
) | 30 if([self isMethodReturnType:returnType compatibleWithValueType:[returnValueA
sNSValue objCType]]) |
| 31 { |
| 32 [returnValueAsNSValue getValue:valueBuffer]; |
| 33 [anInvocation setReturnValue:valueBuffer]; |
| 34 } |
| 35 else if([returnValueAsNSValue getBytes:valueBuffer objCType:returnType]) |
153 { | 36 { |
154 [anInvocation setReturnValue:valueBuffer]; | 37 [anInvocation setReturnValue:valueBuffer]; |
155 } | 38 } |
156 else | 39 else |
157 { | 40 { |
158 [NSException raise:NSInvalidArgumentException | 41 [NSException raise:NSInvalidArgumentException |
159 format:@"Return value does not match method signature; signa
ture declares '%s' but value is '%s'.", returnType, [value objCType]]; | 42 format:@"Return value cannot be used for method; method sign
ature declares '%s' but value is '%s'.", returnType, [returnValueAsNSValue objCT
ype]]; |
160 } | 43 } |
161 } | 44 } |
162 | 45 |
| 46 |
163 - (BOOL)isMethodReturnType:(const char *)returnType compatibleWithValueType:(con
st char *)valueType | 47 - (BOOL)isMethodReturnType:(const char *)returnType compatibleWithValueType:(con
st char *)valueType |
164 { | 48 { |
| 49 /* Same types are obviously compatible */ |
| 50 if(strcmp(returnType, valueType) == 0) |
| 51 return YES; |
| 52 |
165 /* Allow void* for methods that return id, mainly to be able to handle nil *
/ | 53 /* Allow void* for methods that return id, mainly to be able to handle nil *
/ |
166 if(strcmp(returnType, @encode(id)) == 0 && strcmp(valueType, @encode(void *)
) == 0) | 54 if(strcmp(returnType, @encode(id)) == 0 && strcmp(valueType, @encode(void *)
) == 0) |
167 return YES; | 55 return YES; |
168 | |
169 /* Same types are obviously compatible */ | |
170 if(strcmp(returnType, valueType) == 0) | |
171 return YES; | |
172 | |
173 @try { | |
174 if(OCMTypesEqualAllowOpaqueStructs(returnType, valueType)) | |
175 return YES; | |
176 } | |
177 @catch (NSException *e) { | |
178 /* Probably a bitfield or something that NSGetSizeAndAlignment chokes on
, oh well */ | |
179 return NO; | |
180 } | |
181 | 56 |
182 return NO; | 57 return OCMEqualTypesAllowingOpaqueStructs(returnType, valueType); |
183 } | 58 } |
184 | 59 |
185 - (BOOL)getBytes:(void *)outputBuf forValue:(NSValue *)inputValue compatibleWith
Type:(const char *)targetType | |
186 { | |
187 /* If the types are directly compatible, use it */ | |
188 if ([self isMethodReturnType:targetType compatibleWithValueType:[inputValue
objCType]]) | |
189 { | |
190 [inputValue getValue:outputBuf]; | |
191 return YES; | |
192 } | |
193 | |
194 /* | |
195 * See if they are similar number types, and if we can convert losslessly be
tween them. | |
196 * For the most part, we set things up to use CFNumberGetValue, which return
s false if | |
197 * conversion will be lossy. | |
198 */ | |
199 CFNumberType inputType = OCMNumberTypeForObjCType([inputValue objCType]); | |
200 CFNumberType outputType = OCMNumberTypeForObjCType(targetType); | |
201 | |
202 if (inputType == 0 || outputType == 0) // one or both are non-number types | |
203 return NO; | |
204 | |
205 | |
206 NSNumber *inputNumber = [inputValue isKindOfClass:[NSNumber class]]? (id)inp
utValue : OCMNumberForValue(inputValue); | |
207 | |
208 /* | |
209 * Due to some legacy, back-compatible requirements in CFNumber.c, CFNumberG
etValue can return true for | |
210 * some conversions which should not be allowed (by reading source, conversi
ons from integer types to | |
211 * 8-bit or 16-bit integer types). So, check ourselves. | |
212 */ | |
213 long long min = LLONG_MIN; | |
214 long long max = LLONG_MAX; | |
215 long long val = [inputNumber longLongValue]; | |
216 switch (targetType[0]) | |
217 { | |
218 case 'B': | |
219 case 'c': min = CHAR_MIN; max = CHAR_MAX; break; | |
220 case 'C': min = 0; max = UCHAR_MAX; break; | |
221 case 's': min = SHRT_MIN; max = SHRT_MAX; break; | |
222 case 'S': min = 0; max = USHRT_MAX; break; | |
223 } | |
224 if (val < min || val > max) | |
225 return NO; | |
226 | |
227 /* Get the number, and return NO if the value was out of range or conversion
was lossy */ | |
228 return CFNumberGetValue((CFNumberRef)inputNumber, outputType, outputBuf); | |
229 } | |
230 | 60 |
231 @end | 61 @end |
OLD | NEW |