Line data Source code
1 0 : /*
2 :
3 : OOCollectionExtractors.h
4 :
5 : Convenience extensions to Foundation collections to extract optional values.
6 : In addition to being convenient, these perform type checking. Which is,
7 : y'know, good to have.
8 :
9 : Note on types: ideally, stdint.h types would be used for integers. However,
10 : NSNumber doesn't do this, so doing so portably would add new complications.
11 :
12 : Starting with Oolite 1.69.1, the various integer methods will always clamp
13 : values to the range of the return type, rather than truncating like NSNumber.
14 : Before that, they weren't entirely inconsistent.
15 :
16 : The "non-negative float"/"non-negative double" will clamp read values to zero
17 : if negative, but will return a negative defaultValue unchanged.
18 :
19 :
20 : Copyright (C) 2007-2013 Jens Ayton and contributors
21 :
22 : Permission is hereby granted, free of charge, to any person obtaining a copy
23 : of this software and associated documentation files (the "Software"), to deal
24 : in the Software without restriction, including without limitation the rights
25 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
26 : copies of the Software, and to permit persons to whom the Software is
27 : furnished to do so, subject to the following conditions:
28 :
29 : The above copyright notice and this permission notice shall be included in all
30 : copies or substantial portions of the Software.
31 :
32 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38 : SOFTWARE.
39 :
40 : */
41 :
42 :
43 : #import <Foundation/Foundation.h>
44 : #import "OOFunctionAttributes.h"
45 : #include <limits.h>
46 :
47 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
48 : #import "OOMaths.h"
49 : #endif
50 :
51 :
52 :
53 : @interface NSArray (OOExtractor)
54 :
55 0 : - (char) oo_charAtIndex:(NSUInteger)index defaultValue:(char)value;
56 0 : - (short) oo_shortAtIndex:(NSUInteger)index defaultValue:(short)value;
57 0 : - (int) oo_intAtIndex:(NSUInteger)index defaultValue:(int)value;
58 0 : - (long) oo_longAtIndex:(NSUInteger)index defaultValue:(long)value;
59 0 : - (long long) oo_longLongAtIndex:(NSUInteger)index defaultValue:(long long)value;
60 0 : - (NSInteger) oo_integerAtIndex:(NSUInteger)index defaultValue:(NSInteger)value;
61 :
62 0 : - (unsigned char) oo_unsignedCharAtIndex:(NSUInteger)index defaultValue:(unsigned char)value;
63 0 : - (unsigned short) oo_unsignedShortAtIndex:(NSUInteger)index defaultValue:(unsigned short)value;
64 0 : - (unsigned int) oo_unsignedIntAtIndex:(NSUInteger)index defaultValue:(unsigned int)value;
65 0 : - (unsigned long) oo_unsignedLongAtIndex:(NSUInteger)index defaultValue:(unsigned long)value;
66 0 : - (unsigned long long) oo_unsignedLongLongAtIndex:(NSUInteger)index defaultValue:(unsigned long long)value;
67 0 : - (NSUInteger) oo_unsignedIntegerAtIndex:(NSUInteger)index defaultValue:(NSUInteger)value;
68 :
69 0 : - (BOOL) oo_boolAtIndex:(NSUInteger)index defaultValue:(BOOL)value;
70 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
71 0 : - (BOOL) oo_fuzzyBooleanAtIndex:(NSUInteger)index defaultValue:(float)value; // Reads a float in the range [0, 1], and returns YES with that probability.
72 : #endif
73 :
74 0 : - (float) oo_floatAtIndex:(NSUInteger)index defaultValue:(float)value;
75 0 : - (double) oo_doubleAtIndex:(NSUInteger)index defaultValue:(double)value;
76 0 : - (float) oo_nonNegativeFloatAtIndex:(NSUInteger)index defaultValue:(float)value;
77 0 : - (double) oo_nonNegativeDoubleAtIndex:(NSUInteger)index defaultValue:(double)value;
78 :
79 0 : - (id) oo_objectAtIndex:(NSUInteger)index defaultValue:(id)value;
80 0 : - (id) oo_objectOfClass:(Class)class atIndex:(NSUInteger)index defaultValue:(id)value;
81 0 : - (NSString *) oo_stringAtIndex:(NSUInteger)index defaultValue:(NSString *)value;
82 0 : - (NSArray *) oo_arrayAtIndex:(NSUInteger)index defaultValue:(NSArray *)value;
83 0 : - (NSSet *) oo_setAtIndex:(NSUInteger)index defaultValue:(NSSet *)value;
84 0 : - (NSDictionary *) oo_dictionaryAtIndex:(NSUInteger)index defaultValue:(NSDictionary *)value;
85 0 : - (NSData *) oo_dataAtIndex:(NSUInteger)index defaultValue:(NSData *)value;
86 :
87 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
88 0 : - (Vector) oo_vectorAtIndex:(NSUInteger)index defaultValue:(Vector)value;
89 0 : - (Quaternion) oo_quaternionAtIndex:(NSUInteger)index defaultValue:(Quaternion)value;
90 : #endif
91 :
92 :
93 : // Default: 0
94 0 : - (char) oo_charAtIndex:(NSUInteger)index;
95 0 : - (short) oo_shortAtIndex:(NSUInteger)index;
96 0 : - (int) oo_intAtIndex:(NSUInteger)index;
97 0 : - (long) oo_longAtIndex:(NSUInteger)index;
98 0 : - (long long) oo_longLongAtIndex:(NSUInteger)index;
99 0 : - (NSInteger) oo_integerAtIndex:(NSUInteger)index;
100 :
101 0 : - (unsigned char) oo_unsignedCharAtIndex:(NSUInteger)index;
102 0 : - (unsigned short) oo_unsignedShortAtIndex:(NSUInteger)index;
103 0 : - (unsigned int) oo_unsignedIntAtIndex:(NSUInteger)index;
104 0 : - (unsigned long) oo_unsignedLongAtIndex:(NSUInteger)index;
105 0 : - (unsigned long long) oo_unsignedLongLongAtIndex:(NSUInteger)index;
106 0 : - (NSUInteger) oo_unsignedIntegerAtIndex:(NSUInteger)index;
107 :
108 : // Default: NO
109 0 : - (BOOL) oo_boolAtIndex:(NSUInteger)index;
110 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
111 0 : - (BOOL) oo_fuzzyBooleanAtIndex:(NSUInteger)index; // Reads a float in the range [0, 1], and returns YES with that probability.
112 : #endif
113 :
114 : // Default: 0.0
115 0 : - (float) oo_floatAtIndex:(NSUInteger)index;
116 0 : - (double) oo_doubleAtIndex:(NSUInteger)index;
117 0 : - (float) oo_nonNegativeFloatAtIndex:(NSUInteger)index;
118 0 : - (double) oo_nonNegativeDoubleAtIndex:(NSUInteger)index;
119 :
120 : // Default: nil
121 0 : - (id) oo_objectAtIndex:(NSUInteger)index; // Differs from objectAtIndex: in that it returns nil rather than throwing NSRangeException.
122 0 : - (id) oo_objectOfClass:(Class)class atIndex:(NSUInteger)index;
123 0 : - (NSString *) oo_stringAtIndex:(NSUInteger)index;
124 0 : - (NSArray *) oo_arrayAtIndex:(NSUInteger)index;
125 0 : - (NSSet *) oo_setAtIndex:(NSUInteger)index;
126 0 : - (NSDictionary *) oo_dictionaryAtIndex:(NSUInteger)index;
127 0 : - (NSData *) oo_dataAtIndex:(NSUInteger)index;
128 :
129 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
130 : // Default: kZeroVector
131 0 : - (Vector) oo_vectorAtIndex:(NSUInteger)index;
132 : // Default: kIdentityQuaternion
133 0 : - (Quaternion) oo_quaternionAtIndex:(NSUInteger)index;
134 : #endif
135 :
136 : @end
137 :
138 :
139 : @interface NSDictionary (OOExtractor)
140 :
141 0 : - (char) oo_charForKey:(id)key defaultValue:(char)value;
142 0 : - (short) oo_shortForKey:(id)key defaultValue:(short)value;
143 0 : - (int) oo_intForKey:(id)key defaultValue:(int)value;
144 0 : - (long) oo_longForKey:(id)key defaultValue:(long)value;
145 0 : - (long long) oo_longLongForKey:(id)key defaultValue:(long long)value;
146 0 : - (NSInteger) oo_integerForKey:(id)key defaultValue:(NSInteger)value;
147 :
148 0 : - (unsigned char) oo_unsignedCharForKey:(id)key defaultValue:(unsigned char)value;
149 0 : - (unsigned short) oo_unsignedShortForKey:(id)key defaultValue:(unsigned short)value;
150 0 : - (unsigned int) oo_unsignedIntForKey:(id)key defaultValue:(unsigned int)value;
151 0 : - (unsigned long) oo_unsignedLongForKey:(id)key defaultValue:(unsigned long)value;
152 0 : - (unsigned long long) oo_unsignedLongLongForKey:(id)key defaultValue:(unsigned long long)value;
153 0 : - (NSUInteger) oo_unsignedIntegerForKey:(id)key defaultValue:(NSUInteger)value;
154 :
155 0 : - (BOOL) oo_boolForKey:(id)key defaultValue:(BOOL)value;
156 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
157 0 : - (BOOL) oo_fuzzyBooleanForKey:(id)key defaultValue:(float)value; // Reads a float in the range [0, 1], and returns YES with that probability.
158 : #endif
159 :
160 0 : - (float) oo_floatForKey:(id)key defaultValue:(float)value;
161 0 : - (double) oo_doubleForKey:(id)key defaultValue:(double)value;
162 0 : - (float) oo_nonNegativeFloatForKey:(id)key defaultValue:(float)value;
163 0 : - (double) oo_nonNegativeDoubleForKey:(id)key defaultValue:(double)value;
164 :
165 0 : - (id) oo_objectForKey:(id)key defaultValue:(id)value;
166 0 : - (id) oo_objectOfClass:(Class)class forKey:(id)key defaultValue:(id)value;
167 0 : - (NSString *) oo_stringForKey:(id)key defaultValue:(NSString *)value;
168 0 : - (NSArray *) oo_arrayForKey:(id)key defaultValue:(NSArray *)value;
169 0 : - (NSSet *) oo_setForKey:(id)key defaultValue:(NSSet *)value;
170 0 : - (NSDictionary *) oo_dictionaryForKey:(id)key defaultValue:(NSDictionary *)value;
171 0 : - (NSMutableDictionary *) oo_mutableDictionaryForKey:(id)key defaultValue:(NSDictionary *)value;
172 0 : - (NSData *) oo_dataForKey:(id)key defaultValue:(NSData *)value;
173 :
174 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
175 0 : - (Vector) oo_vectorForKey:(id)key defaultValue:(Vector)value;
176 0 : - (HPVector) oo_hpvectorForKey:(id)key defaultValue:(HPVector)value;
177 0 : - (Quaternion) oo_quaternionForKey:(id)key defaultValue:(Quaternion)value;
178 : #endif
179 :
180 :
181 : // Default: 0
182 0 : - (char) oo_charForKey:(id)key;
183 0 : - (short) oo_shortForKey:(id)key;
184 0 : - (int) oo_intForKey:(id)key;
185 0 : - (long) oo_longForKey:(id)key;
186 0 : - (long long) oo_longLongForKey:(id)key;
187 0 : - (NSInteger) oo_integerForKey:(id)key;
188 :
189 0 : - (unsigned char) oo_unsignedCharForKey:(id)key;
190 0 : - (unsigned short) oo_unsignedShortForKey:(id)key;
191 0 : - (unsigned int) oo_unsignedIntForKey:(id)key;
192 0 : - (unsigned long) oo_unsignedLongForKey:(id)key;
193 0 : - (unsigned long long) oo_unsignedLongLongForKey:(id)key;
194 0 : - (NSUInteger) oo_unsignedIntegerForKey:(id)key;
195 :
196 : // Default: NO
197 0 : - (BOOL) oo_boolForKey:(id)key;
198 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
199 0 : - (BOOL) oo_fuzzyBooleanForKey:(id)key; // Reads a float in the range [0, 1], and returns YES with that probability.
200 : #endif
201 :
202 : // Default: 0.0
203 0 : - (float) oo_floatForKey:(id)key;
204 0 : - (double) oo_doubleForKey:(id)key;
205 0 : - (float) oo_nonNegativeFloatForKey:(id)key;
206 0 : - (double) oo_nonNegativeDoubleForKey:(id)key;
207 :
208 : // Default: nil
209 : // - (id)objectForKey:(id)key; // Already defined
210 0 : - (id) oo_objectOfClass:(Class)class forKey:(id)key;
211 0 : - (NSString *) oo_stringForKey:(id)key;
212 0 : - (NSArray *) oo_arrayForKey:(id)key;
213 0 : - (NSSet *) oo_setForKey:(id)key;
214 0 : - (NSDictionary *) oo_dictionaryForKey:(id)key;
215 0 : - (NSMutableDictionary *) oo_mutableDictionaryForKey:(id)key;
216 0 : - (NSData *) oo_dataForKey:(id)key;
217 :
218 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
219 : // Default: kZeroVector
220 0 : - (Vector) oo_vectorForKey:(id)key;
221 : // Default: kZeroHPVector
222 0 : - (HPVector) oo_hpvectorForKey:(id)key;
223 : // Default: kIdentityQuaternion
224 0 : - (Quaternion) oo_quaternionForKey:(id)key;
225 : #endif
226 :
227 : @end
228 :
229 :
230 : @interface NSUserDefaults (OOExtractor)
231 :
232 0 : - (char) oo_charForKey:(id)key defaultValue:(char)value;
233 0 : - (short) oo_shortForKey:(id)key defaultValue:(short)value;
234 0 : - (int) oo_intForKey:(id)key defaultValue:(int)value;
235 0 : - (long) oo_longForKey:(id)key defaultValue:(long)value;
236 0 : - (long long) oo_longLongForKey:(id)key defaultValue:(long long)value;
237 0 : - (NSInteger) oo_integerForKey:(id)key defaultValue:(NSInteger)value;
238 :
239 0 : - (unsigned char) oo_unsignedCharForKey:(id)key defaultValue:(unsigned char)value;
240 0 : - (unsigned short) oo_unsignedShortForKey:(id)key defaultValue:(unsigned short)value;
241 0 : - (unsigned int) oo_unsignedIntForKey:(id)key defaultValue:(unsigned int)value;
242 0 : - (unsigned long) oo_unsignedLongForKey:(id)key defaultValue:(unsigned long)value;
243 0 : - (unsigned long long) oo_unsignedLongLongForKey:(id)key defaultValue:(unsigned long long)value;
244 0 : - (NSUInteger) oo_unsignedIntegerForKey:(id)key defaultValue:(NSUInteger)value;
245 :
246 0 : - (BOOL) oo_boolForKey:(id)key defaultValue:(BOOL)value;
247 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
248 0 : - (BOOL) oo_fuzzyBooleanForKey:(id)key defaultValue:(float)value; // Reads a float in the range [0, 1], and returns YES with that probability.
249 : #endif
250 :
251 0 : - (float) oo_floatForKey:(id)key defaultValue:(float)value;
252 0 : - (double) oo_doubleForKey:(id)key defaultValue:(double)value;
253 0 : - (float) oo_nonNegativeFloatForKey:(id)key defaultValue:(float)value;
254 0 : - (double) oo_nonNegativeDoubleForKey:(id)key defaultValue:(double)value;
255 :
256 0 : - (id) oo_objectForKey:(id)key defaultValue:(id)value;
257 0 : - (id) oo_objectOfClass:(Class)class forKey:(id)key defaultValue:(id)value;
258 0 : - (NSString *) oo_stringForKey:(id)key defaultValue:(NSString *)value;
259 0 : - (NSArray *) oo_arrayForKey:(id)key defaultValue:(NSArray *)value;
260 0 : - (NSSet *) oo_setForKey:(id)key defaultValue:(NSSet *)value;
261 0 : - (NSDictionary *) oo_dictionaryForKey:(id)key defaultValue:(NSDictionary *)value;
262 0 : - (NSData *) oo_dataForKey:(id)key defaultValue:(NSData *)value;
263 :
264 :
265 : // Default: 0
266 0 : - (char) oo_charForKey:(id)key;
267 0 : - (short) oo_shortForKey:(id)key;
268 0 : - (int) oo_intForKey:(id)key;
269 0 : - (long) oo_longForKey:(id)key;
270 0 : - (long long) oo_longLongForKey:(id)key;
271 :
272 0 : - (unsigned char) oo_unsignedCharForKey:(id)key;
273 0 : - (unsigned short) oo_unsignedShortForKey:(id)key;
274 0 : - (unsigned int) oo_unsignedIntForKey:(id)key;
275 0 : - (unsigned long) oo_unsignedLongForKey:(id)key;
276 0 : - (unsigned long long) oo_unsignedLongLongForKey:(id)key;
277 :
278 : // Default: NO
279 : // - (BOOL) boolForKey:(id)key;
280 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
281 0 : - (BOOL) oo_fuzzyBooleanForKey:(id)key; // Reads a float in the range [0, 1], and returns YES with that probability.
282 : #endif
283 :
284 : // Default: 0.0
285 : // - (float) floatForKey:(id)key;
286 0 : - (double) oo_doubleForKey:(NSString *)key;
287 0 : - (float) oo_nonNegativeFloatForKey:(id)key;
288 0 : - (double) oo_nonNegativeDoubleForKey:(id)key;
289 :
290 : // Default: nil
291 : // - (id) objectForKey:(id)key; // Already defined
292 0 : - (id) oo_objectOfClass:(Class)class forKey:(id)key;
293 : // - (NSString *) stringForKey:(id)key;
294 : // - (NSArray *) arrayForKey:(id)key;
295 0 : - (NSSet *) oo_setForKey:(id)key;
296 : // - (NSDictionary *) dictionaryForKey:(id)key;
297 : // - (NSData *) dataForKey:(id)key;
298 :
299 : @end
300 :
301 :
302 : @interface NSMutableArray (OOInserter)
303 :
304 0 : - (void) oo_addInteger:(long)value;
305 0 : - (void) oo_addUnsignedInteger:(unsigned long)value;
306 0 : - (void) oo_addFloat:(double)value;
307 0 : - (void) oo_addBool:(BOOL)value;
308 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
309 0 : - (void) oo_addVector:(Vector)value;
310 0 : - (void) oo_addQuaternion:(Quaternion)value;
311 : #endif
312 :
313 0 : - (void) oo_insertInteger:(long)value atIndex:(NSUInteger)index;
314 0 : - (void) oo_insertUnsignedInteger:(unsigned long)value atIndex:(NSUInteger)index;
315 0 : - (void) oo_insertFloat:(double)value atIndex:(NSUInteger)index;
316 0 : - (void) oo_insertBool:(BOOL)value atIndex:(NSUInteger)index;
317 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
318 0 : - (void) oo_insertVector:(Vector)value atIndex:(NSUInteger)index;
319 0 : - (void) oo_insertQuaternion:(Quaternion)value atIndex:(NSUInteger)index;
320 : #endif
321 :
322 : @end
323 :
324 :
325 : @interface NSMutableDictionary (OOInserter)
326 :
327 0 : - (void) oo_setInteger:(long)value forKey:(id)key;
328 0 : - (void) oo_setUnsignedInteger:(unsigned long)value forKey:(id)key;
329 0 : - (void) oo_setLongLong:(long long)value forKey:(id)key;
330 0 : - (void) oo_setUnsignedLongLong:(unsigned long long)value forKey:(id)key;
331 0 : - (void) oo_setFloat:(double)value forKey:(id)key;
332 0 : - (void) oo_setBool:(BOOL)value forKey:(id)key;
333 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
334 0 : - (void) oo_setVector:(Vector)value forKey:(id)key;
335 0 : - (void) oo_setHPVector:(HPVector)value forKey:(id)key;
336 0 : - (void) oo_setQuaternion:(Quaternion)value forKey:(id)key;
337 : #endif
338 :
339 : @end
340 :
341 :
342 : @interface NSMutableSet (OOInserter)
343 :
344 0 : - (void)oo_addInteger:(long)value;
345 0 : - (void)oo_addUnsignedInteger:(unsigned long)value;
346 0 : - (void)oo_addFloat:(double)value;
347 0 : - (void)oo_addBool:(BOOL)value;
348 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
349 0 : - (void)oo_addVector:(Vector)value;
350 0 : - (void)oo_addQuaternion:(Quaternion)value;
351 : #endif
352 :
353 : @end
354 :
355 :
356 : // *** Value extraction utilities ***
357 :
358 : /* Utility function to interpret a boolean. May be an NSNumber or any of the
359 : following strings (case-insensitive):
360 : yes
361 : true
362 : on
363 :
364 : no
365 : false
366 : off
367 : */
368 0 : BOOL OOBooleanFromObject(id object, BOOL defaultValue);
369 :
370 :
371 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
372 : /* Utility function to interpret a fuzzy boolean. May be any of the strings
373 : accepted by OOBooleanFromObject(), or a number indicating probability of
374 : a yes (between 0 and 1).
375 : */
376 0 : BOOL OOFuzzyBooleanFromObject(id object, float defaultValue);
377 : #endif
378 :
379 :
380 0 : float OOFloatFromObject(id object, float defaultValue);
381 0 : double OODoubleFromObject(id object, double defaultValue);
382 0 : float OONonNegativeFloatFromObject(id object, float defaultValue);
383 0 : double OONonNegativeDoubleFromObject(id object, double defaultValue);
384 :
385 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
386 : // These take strings, dictionaries or arrays.
387 0 : Vector OOVectorFromObject(id object, Vector defaultValue);
388 0 : HPVector OOHPVectorFromObject(id object, HPVector defaultValue);
389 0 : Quaternion OOQuaternionFromObject(id object, Quaternion defaultValue);
390 :
391 0 : NSDictionary *OOPropertyListFromVector(Vector value);
392 0 : NSDictionary *OOPropertyListFromHPVector(HPVector value);
393 0 : NSDictionary *OOPropertyListFromQuaternion(Quaternion value);
394 : #endif
395 :
396 :
397 : OOINLINE long long OOClampInteger(long long value, long long minValue, long long maxValue) ALWAYS_INLINE_FUNC;
398 0 : long long OOLongLongFromObject(id object, long long defaultValue);
399 0 : unsigned long long OOUnsignedLongLongFromObject(id object, unsigned long long defaultValue);
400 :
401 :
402 0 : OOINLINE long long OOClampInteger(long long value, long long minValue, long long maxValue)
403 : {
404 : return (minValue < value) ? ((value < maxValue) ? value : maxValue) : minValue;
405 : }
406 :
407 :
408 : /* Define an inline function to clamp a give type and its unsigned
409 : counterpart. Example:
410 :
411 : OO_DEFINE_CLAMP_PAIR(char, Char, CHAR)
412 :
413 : expands to
414 :
415 : OOINLINE char OOCharFromObject(id object, char defaultValue)
416 : {
417 : return OOClampInteger(OOLongLongFromObject(object, defaultValue), CHAR_MIN, CHAR_MAX);
418 : }
419 : OOINLINE unsigned char OOUnsignedCharFromObject(id object, unsigned char defaultValue)
420 : {
421 : return OOClampInteger(OOLongLongFromObject(object, defaultValue), 0, UCHAR_MAX);
422 : }
423 : */
424 0 : #define OO_DEFINE_CLAMP(type, typeName, min, max) \
425 : OOINLINE type OO ## typeName ## FromObject(id object, type defaultValue) \
426 : { \
427 : return (type)OOClampInteger(OOLongLongFromObject(object, defaultValue), min, max); \
428 : }
429 :
430 0 : #define OO_DEFINE_CLAMP_PAIR(type, typeName, minMaxSymb) \
431 : OO_DEFINE_CLAMP(type, typeName, minMaxSymb ## _MIN, minMaxSymb ## _MAX) \
432 : OO_DEFINE_CLAMP(unsigned type, Unsigned ## typeName, 0, U ## minMaxSymb ## _MAX)
433 :
434 : OO_DEFINE_CLAMP_PAIR(char, Char, CHAR)
435 : OO_DEFINE_CLAMP_PAIR(short, Short, SHRT)
436 :
437 : /* When ints or longs are as large as long longs, we can't do any clamping
438 : because the clamping code will overflow (unless we add pointless complexity).
439 : Instead, we alias the long long versions which don't clamp. Inlines are
440 : used instead of macros so that the functions always have the precise type
441 : they should; this is necessary for stuff that uses @encode, notably the
442 : SenTestingKit framework.
443 : */
444 0 : #define OO_ALIAS_CLAMP_LONG_LONG(type, typeName) \
445 : static inline type OO##typeName##FromObject(id object, type defaultValue) \
446 : { \
447 : return OOLongLongFromObject(object, defaultValue); \
448 : }
449 0 : #define OO_ALIAS_CLAMP_PAIR_LONG_LONG(type, typeName) \
450 : OO_ALIAS_CLAMP_LONG_LONG(type, typeName) \
451 : OO_ALIAS_CLAMP_LONG_LONG(unsigned type, Unsigned##typeName)
452 :
453 : #if INT_MAX == LLONG_MAX
454 : // Should never get here under Mac OS X, but may under GNUstep.
455 : OO_ALIAS_CLAMP_PAIR_LONG_LONG(int, Int)
456 : #else
457 : OO_DEFINE_CLAMP_PAIR(int, Int, INT)
458 : #endif
459 :
460 : #if LONG_MAX == LLONG_MAX
461 : OO_ALIAS_CLAMP_PAIR_LONG_LONG(long, Long)
462 : #else
463 : OO_DEFINE_CLAMP_PAIR(long, Long, LONG)
464 : #endif
465 :
466 :
467 : #if OOLITE_64_BIT
468 : OOINLINE NSInteger OOIntegerFromObject(id object, NSInteger defaultValue)
469 : {
470 : return OOLongLongFromObject(object, defaultValue);
471 : }
472 :
473 : OOINLINE NSInteger OOUIntegerFromObject(id object, NSUInteger defaultValue)
474 : {
475 : return OOUnsignedLongLongFromObject(object, defaultValue);
476 : }
477 : #else
478 0 : OOINLINE NSInteger OOIntegerFromObject(id object, NSInteger defaultValue)
479 : {
480 : return OOLongFromObject(object, defaultValue);
481 : }
482 :
483 0 : OOINLINE NSInteger OOUIntegerFromObject(id object, NSUInteger defaultValue)
484 : {
485 : return OOUnsignedLongFromObject(object, defaultValue);
486 : }
487 : #endif
488 :
489 :
490 : #undef OO_DEFINE_CLAMP
491 : #undef OO_DEFINE_CLAMP_PAIR
492 : #undef OO_ALIAS_CLAMP_LONG_LONG
493 : #undef OO_ALIAS_CLAMP_PAIR_LONG_LONG
|