Line data Source code
1 0 : /*
2 :
3 : OOCollectionExtractors.m
4 :
5 : Copyright (C) 2007-2013 Jens Ayton and contributors
6 :
7 : Permission is hereby granted, free of charge, to any person obtaining a copy
8 : of this software and associated documentation files (the "Software"), to deal
9 : in the Software without restriction, including without limitation the rights
10 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 : copies of the Software, and to permit persons to whom the Software is
12 : furnished to do so, subject to the following conditions:
13 :
14 : The above copyright notice and this permission notice shall be included in all
15 : copies or substantial portions of the Software.
16 :
17 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 : SOFTWARE.
24 :
25 : */
26 :
27 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
28 : #import "OOCocoa.h"
29 : #import "OOStringParsing.h"
30 : #endif
31 :
32 : #import "OOCollectionExtractors.h"
33 : #include <limits.h>
34 : #import "OOMaths.h"
35 :
36 :
37 : static NSSet *SetForObject(id object, NSSet *defaultValue);
38 : static NSString *StringForObject(id object, NSString *defaultValue);
39 :
40 :
41 : @implementation NSArray (OOExtractor)
42 :
43 : - (char) oo_charAtIndex:(NSUInteger)index defaultValue:(char)value
44 : {
45 : return OOCharFromObject([self oo_objectAtIndex:index], value);
46 : }
47 :
48 :
49 : - (short) oo_shortAtIndex:(NSUInteger)index defaultValue:(short)value
50 : {
51 : return OOShortFromObject([self oo_objectAtIndex:index], value);
52 : }
53 :
54 :
55 : - (int) oo_intAtIndex:(NSUInteger)index defaultValue:(int)value
56 : {
57 : return OOIntFromObject([self oo_objectAtIndex:index], value);
58 : }
59 :
60 :
61 : - (long) oo_longAtIndex:(NSUInteger)index defaultValue:(long)value
62 : {
63 : return OOLongFromObject([self oo_objectAtIndex:index], value);
64 : }
65 :
66 :
67 : - (long long) oo_longLongAtIndex:(NSUInteger)index defaultValue:(long long)value
68 : {
69 : return OOLongLongFromObject([self oo_objectAtIndex:index], value);
70 : }
71 :
72 :
73 : - (NSInteger) oo_integerAtIndex:(NSUInteger)index defaultValue:(NSInteger)value
74 : {
75 : return OOIntegerFromObject([self oo_objectAtIndex:index], value);
76 : }
77 :
78 :
79 : - (unsigned char) oo_unsignedCharAtIndex:(NSUInteger)index defaultValue:(unsigned char)value
80 : {
81 : return OOUnsignedCharFromObject([self oo_objectAtIndex:index], value);
82 : }
83 :
84 :
85 : - (unsigned short) oo_unsignedShortAtIndex:(NSUInteger)index defaultValue:(unsigned short)value
86 : {
87 : return OOUnsignedShortFromObject([self oo_objectAtIndex:index], value);
88 : }
89 :
90 :
91 : - (unsigned int) oo_unsignedIntAtIndex:(NSUInteger)index defaultValue:(unsigned int)value
92 : {
93 : return OOUnsignedIntFromObject([self oo_objectAtIndex:index], value);
94 : }
95 :
96 :
97 : - (unsigned long) oo_unsignedLongAtIndex:(NSUInteger)index defaultValue:(unsigned long)value
98 : {
99 : return OOUnsignedLongFromObject([self oo_objectAtIndex:index], value);
100 : }
101 :
102 :
103 : - (unsigned long long) oo_unsignedLongLongAtIndex:(NSUInteger)index defaultValue:(unsigned long long)value
104 : {
105 : return OOUnsignedLongLongFromObject([self oo_objectAtIndex:index], value);
106 : }
107 :
108 :
109 : - (NSUInteger) oo_unsignedIntegerAtIndex:(NSUInteger)index defaultValue:(NSUInteger)value
110 : {
111 : return OOUIntegerFromObject([self oo_objectAtIndex:index], value);
112 : }
113 :
114 :
115 : - (BOOL) oo_boolAtIndex:(NSUInteger)index defaultValue:(BOOL)value
116 : {
117 : return OOBooleanFromObject([self oo_objectAtIndex:index], value);
118 : }
119 :
120 :
121 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
122 : - (BOOL) oo_fuzzyBooleanAtIndex:(NSUInteger)index defaultValue:(float)value
123 : {
124 : return OOFuzzyBooleanFromObject([self oo_objectAtIndex:index], value);
125 : }
126 : #endif
127 :
128 :
129 : - (float) oo_floatAtIndex:(NSUInteger)index defaultValue:(float)value
130 : {
131 : return OOFloatFromObject([self oo_objectAtIndex:index], value);
132 : }
133 :
134 :
135 : - (double) oo_doubleAtIndex:(NSUInteger)index defaultValue:(double)value
136 : {
137 : return OODoubleFromObject([self oo_objectAtIndex:index], value);
138 : }
139 :
140 :
141 : - (float) oo_nonNegativeFloatAtIndex:(NSUInteger)index defaultValue:(float)value
142 : {
143 : return OONonNegativeFloatFromObject([self oo_objectAtIndex:index], value);
144 : }
145 :
146 :
147 : - (double) oo_nonNegativeDoubleAtIndex:(NSUInteger)index defaultValue:(double)value
148 : {
149 : return OONonNegativeDoubleFromObject([self oo_objectAtIndex:index], value);
150 : }
151 :
152 :
153 : - (id) oo_objectAtIndex:(NSUInteger)index defaultValue:(id)value
154 : {
155 : id objVal = [self oo_objectAtIndex:index];
156 : id result;
157 :
158 : if (objVal != nil) result = objVal;
159 : else result = value;
160 :
161 : return result;
162 : }
163 :
164 :
165 : - (id) oo_objectOfClass:(Class)class atIndex:(NSUInteger)index defaultValue:(id)value
166 : {
167 : id objVal = [self oo_objectAtIndex:index];
168 : NSString *result;
169 :
170 : if ([objVal isKindOfClass:class]) result = objVal;
171 : else result = value;
172 :
173 : return result;
174 : }
175 :
176 :
177 : - (NSString *) oo_stringAtIndex:(NSUInteger)index defaultValue:(NSString *)value
178 : {
179 : return StringForObject([self oo_objectAtIndex:index], value);
180 : }
181 :
182 :
183 : - (NSArray *) oo_arrayAtIndex:(NSUInteger)index defaultValue:(NSArray *)value
184 : {
185 : return [self oo_objectOfClass:[NSArray class] atIndex:index defaultValue:value];
186 : }
187 :
188 :
189 : - (NSSet *) oo_setAtIndex:(NSUInteger)index defaultValue:(NSSet *)value
190 : {
191 : return SetForObject([self oo_objectAtIndex:index], value);
192 : }
193 :
194 :
195 : - (NSDictionary *) oo_dictionaryAtIndex:(NSUInteger)index defaultValue:(NSDictionary *)value
196 : {
197 : return [self oo_objectOfClass:[NSDictionary class] atIndex:index defaultValue:value];
198 : }
199 :
200 :
201 : - (NSData *) oo_dataAtIndex:(NSUInteger)index defaultValue:(NSData *)value
202 : {
203 : return [self oo_objectOfClass:[NSData class] atIndex:index defaultValue:value];
204 : }
205 :
206 :
207 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
208 : - (Vector) oo_vectorAtIndex:(NSUInteger)index defaultValue:(Vector)value
209 : {
210 : return OOVectorFromObject([self oo_objectAtIndex:index], value);
211 : }
212 :
213 :
214 : - (Quaternion) oo_quaternionAtIndex:(NSUInteger)index defaultValue:(Quaternion)value
215 : {
216 : return OOQuaternionFromObject([self oo_objectAtIndex:index], value);
217 : }
218 : #endif
219 :
220 :
221 : - (char) oo_charAtIndex:(NSUInteger)index
222 : {
223 : return [self oo_charAtIndex:index defaultValue:0];
224 : }
225 :
226 :
227 : - (short) oo_shortAtIndex:(NSUInteger)index
228 : {
229 : return [self oo_shortAtIndex:index defaultValue:0];
230 : }
231 :
232 :
233 : - (int) oo_intAtIndex:(NSUInteger)index
234 : {
235 : return [self oo_intAtIndex:index defaultValue:0];
236 : }
237 :
238 :
239 : - (long) oo_longAtIndex:(NSUInteger)index
240 : {
241 : return [self oo_longAtIndex:index defaultValue:0];
242 : }
243 :
244 :
245 : - (long long) oo_longLongAtIndex:(NSUInteger)index
246 : {
247 : return [self oo_longLongAtIndex:index defaultValue:0];
248 : }
249 :
250 :
251 : - (NSInteger) oo_integerAtIndex:(NSUInteger)index
252 : {
253 : return [self oo_integerAtIndex:index defaultValue:0];
254 : }
255 :
256 :
257 : - (unsigned char) oo_unsignedCharAtIndex:(NSUInteger)index
258 : {
259 : return [self oo_unsignedCharAtIndex:index defaultValue:0];
260 : }
261 :
262 :
263 : - (unsigned short) oo_unsignedShortAtIndex:(NSUInteger)index
264 : {
265 : return [self oo_unsignedShortAtIndex:index defaultValue:0];
266 : }
267 :
268 :
269 : - (unsigned int) oo_unsignedIntAtIndex:(NSUInteger)index
270 : {
271 : return [self oo_unsignedIntAtIndex:index defaultValue:0];
272 : }
273 :
274 :
275 : - (unsigned long) oo_unsignedLongAtIndex:(NSUInteger)index
276 : {
277 : return [self oo_unsignedLongAtIndex:index defaultValue:0];
278 : }
279 :
280 :
281 : - (unsigned long long) oo_unsignedLongLongAtIndex:(NSUInteger)index
282 : {
283 : return [self oo_unsignedLongLongAtIndex:index defaultValue:0];
284 : }
285 :
286 :
287 : - (NSUInteger) oo_unsignedIntegerAtIndex:(NSUInteger)index
288 : {
289 : return [self oo_unsignedIntegerAtIndex:index defaultValue:0];
290 : }
291 :
292 :
293 : - (BOOL) oo_boolAtIndex:(NSUInteger)index
294 : {
295 : return [self oo_boolAtIndex:index defaultValue:NO];
296 : }
297 :
298 :
299 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
300 : - (BOOL) oo_fuzzyBooleanAtIndex:(NSUInteger)index
301 : {
302 : return [self oo_fuzzyBooleanAtIndex:index defaultValue:0.0f];
303 : }
304 : #endif
305 :
306 :
307 : - (float) oo_floatAtIndex:(NSUInteger)index
308 : {
309 : return OOFloatFromObject([self oo_objectAtIndex:index], 0.0f);
310 : }
311 :
312 :
313 : - (double) oo_doubleAtIndex:(NSUInteger)index
314 : {
315 : return OODoubleFromObject([self oo_objectAtIndex:index], 0.0);
316 : }
317 :
318 :
319 : - (float) oo_nonNegativeFloatAtIndex:(NSUInteger)index
320 : {
321 : return OONonNegativeFloatFromObject([self oo_objectAtIndex:index], 0.0f);
322 : }
323 :
324 :
325 : - (double) oo_nonNegativeDoubleAtIndex:(NSUInteger)index
326 : {
327 : return OONonNegativeDoubleFromObject([self oo_objectAtIndex:index], 0.0);
328 : }
329 :
330 :
331 : - (id) oo_objectAtIndex:(NSUInteger)index
332 : {
333 : if (index < [self count]) return [self objectAtIndex:index];
334 : else return nil;
335 : }
336 :
337 :
338 : - (id) oo_objectOfClass:(Class)class atIndex:(NSUInteger)index
339 : {
340 : return [self oo_objectOfClass:class atIndex:index defaultValue:nil];
341 : }
342 :
343 :
344 : - (NSString *) oo_stringAtIndex:(NSUInteger)index
345 : {
346 : return [self oo_stringAtIndex:index defaultValue:nil];
347 : }
348 :
349 :
350 : - (NSArray *) oo_arrayAtIndex:(NSUInteger)index
351 : {
352 : return [self oo_arrayAtIndex:index defaultValue:nil];
353 : }
354 :
355 :
356 : - (NSSet *) oo_setAtIndex:(NSUInteger)index
357 : {
358 : return [self oo_setAtIndex:index defaultValue:nil];
359 : }
360 :
361 :
362 : - (NSDictionary *) oo_dictionaryAtIndex:(NSUInteger)index
363 : {
364 : return [self oo_dictionaryAtIndex:index defaultValue:nil];
365 : }
366 :
367 :
368 : - (NSData *) oo_dataAtIndex:(NSUInteger)index
369 : {
370 : return [self oo_dataAtIndex:index defaultValue:nil];
371 : }
372 :
373 :
374 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
375 : - (Vector) oo_vectorAtIndex:(NSUInteger)index
376 : {
377 : return [self oo_vectorAtIndex:index defaultValue:kZeroVector];
378 : }
379 :
380 :
381 : - (Quaternion) oo_quaternionAtIndex:(NSUInteger)index
382 : {
383 : return [self oo_quaternionAtIndex:index defaultValue:kIdentityQuaternion];
384 : }
385 : #endif
386 :
387 : @end
388 :
389 :
390 : @implementation NSDictionary (OOExtractor)
391 :
392 : - (char) oo_charForKey:(id)key defaultValue:(char)value
393 : {
394 : return OOCharFromObject([self objectForKey:key], value);
395 : }
396 :
397 :
398 : - (short) oo_shortForKey:(id)key defaultValue:(short)value
399 : {
400 : return OOShortFromObject([self objectForKey:key], value);
401 : }
402 :
403 :
404 : - (int) oo_intForKey:(id)key defaultValue:(int)value
405 : {
406 : return OOIntFromObject([self objectForKey:key], value);
407 : }
408 :
409 :
410 : - (long) oo_longForKey:(id)key defaultValue:(long)value
411 : {
412 : return OOLongFromObject([self objectForKey:key], value);
413 : }
414 :
415 :
416 : - (long long) oo_longLongForKey:(id)key defaultValue:(long long)value
417 : {
418 : return OOLongLongFromObject([self objectForKey:key], value);
419 : }
420 :
421 :
422 : - (NSInteger) oo_integerForKey:(id)key defaultValue:(NSInteger)value
423 : {
424 : return OOIntegerFromObject([self objectForKey:key], value);
425 : }
426 :
427 :
428 : - (unsigned char) oo_unsignedCharForKey:(id)key defaultValue:(unsigned char)value
429 : {
430 : return OOUnsignedCharFromObject([self objectForKey:key], value);
431 : }
432 :
433 :
434 : - (unsigned short) oo_unsignedShortForKey:(id)key defaultValue:(unsigned short)value
435 : {
436 : return OOUnsignedShortFromObject([self objectForKey:key], value);
437 : }
438 :
439 :
440 : - (unsigned int) oo_unsignedIntForKey:(id)key defaultValue:(unsigned int)value
441 : {
442 : return OOUnsignedIntFromObject([self objectForKey:key], value);
443 : }
444 :
445 :
446 : - (unsigned long) oo_unsignedLongForKey:(id)key defaultValue:(unsigned long)value
447 : {
448 : return OOUnsignedLongFromObject([self objectForKey:key], value);
449 : }
450 :
451 :
452 : - (unsigned long long) oo_unsignedLongLongForKey:(id)key defaultValue:(unsigned long long)value
453 : {
454 : return OOUnsignedLongLongFromObject([self objectForKey:key], value);
455 : }
456 :
457 :
458 : - (NSUInteger) oo_unsignedIntegerForKey:(id)key defaultValue:(NSUInteger)value
459 : {
460 : return OOUIntegerFromObject([self objectForKey:key], value);
461 : }
462 :
463 :
464 : - (BOOL) oo_boolForKey:(id)key defaultValue:(BOOL)value
465 : {
466 : return OOBooleanFromObject([self objectForKey:key], value);
467 : }
468 :
469 :
470 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
471 : - (BOOL) oo_fuzzyBooleanForKey:(id)key defaultValue:(float)value
472 : {
473 : return OOFuzzyBooleanFromObject([self objectForKey:key], value);
474 : }
475 : #endif
476 :
477 :
478 : - (float) oo_floatForKey:(id)key defaultValue:(float)value
479 : {
480 : return OOFloatFromObject([self objectForKey:key], value);
481 : }
482 :
483 :
484 : - (double) oo_doubleForKey:(id)key defaultValue:(double)value
485 : {
486 : return OODoubleFromObject([self objectForKey:key], value);
487 : }
488 :
489 :
490 : - (float) oo_nonNegativeFloatForKey:(id)key defaultValue:(float)value
491 : {
492 : return OONonNegativeFloatFromObject([self objectForKey:key], value);
493 : }
494 :
495 :
496 : - (double) oo_nonNegativeDoubleForKey:(id)key defaultValue:(double)value
497 : {
498 : return OONonNegativeDoubleFromObject([self objectForKey:key], value);
499 : }
500 :
501 :
502 : - (id) oo_objectForKey:(id)key defaultValue:(id)value
503 : {
504 : id objVal = [self objectForKey:key];
505 : id result;
506 :
507 : if (objVal != nil) result = objVal;
508 : else result = value;
509 :
510 : return result;
511 : }
512 :
513 :
514 : - (id) oo_objectOfClass:(Class)class forKey:(id)key defaultValue:(id)value
515 : {
516 : id objVal = [self objectForKey:key];
517 : id result;
518 :
519 : if ([objVal isKindOfClass:class]) result = objVal;
520 : else result = value;
521 :
522 : return result;
523 : }
524 :
525 :
526 : - (NSString *) oo_stringForKey:(id)key defaultValue:(NSString *)value
527 : {
528 : return StringForObject([self objectForKey:key], value);
529 : }
530 :
531 :
532 : - (NSArray *) oo_arrayForKey:(id)key defaultValue:(NSArray *)value
533 : {
534 : return [self oo_objectOfClass:[NSArray class] forKey:key defaultValue:value];
535 : }
536 :
537 :
538 : - (NSSet *) oo_setForKey:(id)key defaultValue:(NSSet *)value
539 : {
540 : return SetForObject([self objectForKey:key], value);
541 : }
542 :
543 :
544 : - (NSDictionary *) oo_dictionaryForKey:(id)key defaultValue:(NSDictionary *)value
545 : {
546 : return [self oo_objectOfClass:[NSDictionary class] forKey:key defaultValue:value];
547 : }
548 :
549 :
550 : - (NSMutableDictionary *) oo_mutableDictionaryForKey:(id)key defaultValue:(NSDictionary *)value
551 : {
552 : return [self oo_objectOfClass:[NSMutableDictionary class] forKey:key defaultValue:value];
553 : }
554 :
555 :
556 :
557 : - (NSData *) oo_dataForKey:(id)key defaultValue:(NSData *)value
558 : {
559 : return [self oo_objectOfClass:[NSData class] forKey:key defaultValue:value];
560 : }
561 :
562 :
563 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
564 : - (Vector) oo_vectorForKey:(id)key defaultValue:(Vector)value
565 : {
566 : return OOVectorFromObject([self objectForKey:key], value);
567 : }
568 :
569 : - (HPVector) oo_hpvectorForKey:(id)key defaultValue:(HPVector)value
570 : {
571 : return OOHPVectorFromObject([self objectForKey:key], value);
572 : }
573 :
574 :
575 : - (Quaternion) oo_quaternionForKey:(id)key defaultValue:(Quaternion)value
576 : {
577 : return OOQuaternionFromObject([self objectForKey:key], value);
578 : }
579 : #endif
580 :
581 :
582 : - (char) oo_charForKey:(id)key
583 : {
584 : return [self oo_charForKey:key defaultValue:0];
585 : }
586 :
587 :
588 : - (short) oo_shortForKey:(id)key
589 : {
590 : return [self oo_shortForKey:key defaultValue:0];
591 : }
592 :
593 :
594 : - (int) oo_intForKey:(id)key
595 : {
596 : return [self oo_intForKey:key defaultValue:0];
597 : }
598 :
599 :
600 : - (long) oo_longForKey:(id)key
601 : {
602 : return [self oo_longForKey:key defaultValue:0];
603 : }
604 :
605 :
606 : - (long long) oo_longLongForKey:(id)key
607 : {
608 : return [self oo_longLongForKey:key defaultValue:0];
609 : }
610 :
611 :
612 : - (NSInteger) oo_integerForKey:(id)key
613 : {
614 : return [self oo_integerForKey:key defaultValue:0];
615 : }
616 :
617 :
618 : - (unsigned char) oo_unsignedCharForKey:(id)key
619 : {
620 : return [self oo_unsignedCharForKey:key defaultValue:0];
621 : }
622 :
623 :
624 : - (unsigned short) oo_unsignedShortForKey:(id)key
625 : {
626 : return [self oo_unsignedShortForKey:key defaultValue:0];
627 : }
628 :
629 :
630 : - (unsigned int) oo_unsignedIntForKey:(id)key
631 : {
632 : return [self oo_unsignedIntForKey:key defaultValue:0];
633 : }
634 :
635 :
636 : - (unsigned long) oo_unsignedLongForKey:(id)key
637 : {
638 : return [self oo_unsignedLongForKey:key defaultValue:0];
639 : }
640 :
641 :
642 : - (NSUInteger) oo_unsignedIntegerForKey:(id)key
643 : {
644 : return [self oo_unsignedIntegerForKey:key defaultValue:0];
645 : }
646 :
647 :
648 : - (unsigned long long) oo_unsignedLongLongForKey:(id)key
649 : {
650 : return [self oo_unsignedLongLongForKey:key defaultValue:0];
651 : }
652 :
653 :
654 : - (BOOL) oo_boolForKey:(id)key
655 : {
656 : return [self oo_boolForKey:key defaultValue:NO];
657 : }
658 :
659 :
660 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
661 : - (BOOL) oo_fuzzyBooleanForKey:(id)key
662 : {
663 : return [self oo_fuzzyBooleanForKey:key defaultValue:0.0f];
664 : }
665 : #endif
666 :
667 :
668 : - (float) oo_floatForKey:(id)key
669 : {
670 : return OOFloatFromObject([self objectForKey:key], 0.0f);
671 : }
672 :
673 :
674 : - (double) oo_doubleForKey:(id)key
675 : {
676 : return OODoubleFromObject([self objectForKey:key], 0.0);
677 : }
678 :
679 :
680 : - (float) oo_nonNegativeFloatForKey:(id)key
681 : {
682 : return OONonNegativeFloatFromObject([self objectForKey:key], 0.0f);
683 : }
684 :
685 :
686 : - (double) oo_nonNegativeDoubleForKey:(id)key
687 : {
688 : return OONonNegativeDoubleFromObject([self objectForKey:key], 0.0);
689 : }
690 :
691 :
692 : - (id) oo_objectOfClass:(Class)class forKey:(id)key
693 : {
694 : return [self oo_objectOfClass:class forKey:key defaultValue:nil];
695 : }
696 :
697 :
698 : - (NSString *) oo_stringForKey:(id)key
699 : {
700 : return [self oo_stringForKey:key defaultValue:nil];
701 : }
702 :
703 :
704 : - (NSArray *) oo_arrayForKey:(id)key
705 : {
706 : return [self oo_arrayForKey:key defaultValue:nil];
707 : }
708 :
709 :
710 : - (NSSet *) oo_setForKey:(id)key
711 : {
712 : return [self oo_setForKey:key defaultValue:nil];
713 : }
714 :
715 :
716 : - (NSDictionary *) oo_dictionaryForKey:(id)key
717 : {
718 : return [self oo_dictionaryForKey:key defaultValue:nil];
719 : }
720 :
721 :
722 : - (NSMutableDictionary *) oo_mutableDictionaryForKey:(id)key
723 : {
724 : return [self oo_mutableDictionaryForKey:key defaultValue:nil];
725 : }
726 :
727 :
728 : - (NSData *) oo_dataForKey:(id)key
729 : {
730 : return [self oo_dataForKey:key defaultValue:nil];
731 : }
732 :
733 :
734 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
735 : - (Vector) oo_vectorForKey:(id)key
736 : {
737 : return [self oo_vectorForKey:key defaultValue:kZeroVector];
738 : }
739 :
740 : - (HPVector) oo_hpvectorForKey:(id)key
741 : {
742 : return [self oo_hpvectorForKey:key defaultValue:kZeroHPVector];
743 : }
744 :
745 :
746 : - (Quaternion) oo_quaternionForKey:(id)key
747 : {
748 : return [self oo_quaternionForKey:key defaultValue:kIdentityQuaternion];
749 : }
750 : #endif
751 :
752 : @end
753 :
754 :
755 : @implementation NSUserDefaults (OOExtractor)
756 :
757 : - (char) oo_charForKey:(id)key defaultValue:(char)value
758 : {
759 : return OOCharFromObject([self objectForKey:key], value);
760 : }
761 :
762 :
763 : - (short) oo_shortForKey:(id)key defaultValue:(short)value
764 : {
765 : return OOShortFromObject([self objectForKey:key], value);
766 : }
767 :
768 :
769 : - (int) oo_intForKey:(id)key defaultValue:(int)value
770 : {
771 : return OOIntFromObject([self objectForKey:key], value);
772 : }
773 :
774 :
775 : - (long) oo_longForKey:(id)key defaultValue:(long)value
776 : {
777 : return OOLongFromObject([self objectForKey:key], value);
778 : }
779 :
780 :
781 : - (long long) oo_longLongForKey:(id)key defaultValue:(long long)value
782 : {
783 : return OOLongLongFromObject([self objectForKey:key], value);
784 : }
785 :
786 :
787 : - (NSInteger) oo_integerForKey:(id)key defaultValue:(NSInteger)value
788 : {
789 : return OOIntegerFromObject([self objectForKey:key], value);
790 : }
791 :
792 :
793 : - (unsigned char) oo_unsignedCharForKey:(id)key defaultValue:(unsigned char)value
794 : {
795 : return OOUnsignedCharFromObject([self objectForKey:key], value);
796 : }
797 :
798 :
799 : - (unsigned short) oo_unsignedShortForKey:(id)key defaultValue:(unsigned short)value
800 : {
801 : return OOUnsignedShortFromObject([self objectForKey:key], value);
802 : }
803 :
804 :
805 : - (unsigned int) oo_unsignedIntForKey:(id)key defaultValue:(unsigned int)value
806 : {
807 : return OOUnsignedIntFromObject([self objectForKey:key], value);
808 : }
809 :
810 :
811 : - (unsigned long) oo_unsignedLongForKey:(id)key defaultValue:(unsigned long)value
812 : {
813 : return OOUnsignedLongFromObject([self objectForKey:key], value);
814 : }
815 :
816 :
817 : - (unsigned long long) oo_unsignedLongLongForKey:(id)key defaultValue:(unsigned long long)value
818 : {
819 : return OOUnsignedLongLongFromObject([self objectForKey:key], value);
820 : }
821 :
822 :
823 : - (NSUInteger) oo_unsignedIntegerForKey:(id)key defaultValue:(NSUInteger)value
824 : {
825 : return OOUIntegerFromObject([self objectForKey:key], value);
826 : }
827 :
828 :
829 : - (BOOL) oo_boolForKey:(id)key defaultValue:(BOOL)value
830 : {
831 : return OOBooleanFromObject([self objectForKey:key], value);
832 : }
833 :
834 :
835 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
836 : - (BOOL) oo_fuzzyBooleanForKey:(id)key defaultValue:(float)value
837 : {
838 : return OOFuzzyBooleanFromObject([self objectForKey:key], value);
839 : }
840 : #endif
841 :
842 :
843 : - (float) oo_floatForKey:(id)key defaultValue:(float)value
844 : {
845 : return OOFloatFromObject([self objectForKey:key], value);
846 : }
847 :
848 :
849 : - (double) oo_doubleForKey:(id)key defaultValue:(double)value
850 : {
851 : return OODoubleFromObject([self objectForKey:key], value);
852 : }
853 :
854 :
855 : - (float) oo_nonNegativeFloatForKey:(id)key defaultValue:(float)value
856 : {
857 : return OONonNegativeFloatFromObject([self objectForKey:key], value);
858 : }
859 :
860 :
861 : - (double) oo_nonNegativeDoubleForKey:(id)key defaultValue:(double)value
862 : {
863 : return OONonNegativeDoubleFromObject([self objectForKey:key], value);
864 : }
865 :
866 :
867 : - (id) oo_objectForKey:(id)key defaultValue:(id)value
868 : {
869 : id objVal = [self objectForKey:key];
870 : id result;
871 :
872 : if (objVal != nil) result = objVal;
873 : else result = value;
874 :
875 : return result;
876 : }
877 :
878 :
879 : - (id) oo_objectOfClass:(Class)class forKey:(id)key defaultValue:(id)value
880 : {
881 : id objVal = [self objectForKey:key];
882 : id result;
883 :
884 : if ([objVal isKindOfClass:class]) result = objVal;
885 : else result = value;
886 :
887 : return result;
888 : }
889 :
890 :
891 : - (NSString *) oo_stringForKey:(id)key defaultValue:(NSString *)value
892 : {
893 : return StringForObject([self objectForKey:key], value);
894 : }
895 :
896 :
897 : - (NSArray *) oo_arrayForKey:(id)key defaultValue:(NSArray *)value
898 : {
899 : return [self oo_objectOfClass:[NSArray class] forKey:key defaultValue:value];
900 : }
901 :
902 :
903 : - (NSSet *) oo_setForKey:(id)key defaultValue:(NSSet *)value
904 : {
905 : return SetForObject([self objectForKey:key], value);
906 : }
907 :
908 :
909 : - (NSDictionary *) oo_dictionaryForKey:(id)key defaultValue:(NSDictionary *)value
910 : {
911 : return [self oo_objectOfClass:[NSDictionary class] forKey:key defaultValue:value];
912 : }
913 :
914 :
915 : - (NSData *) oo_dataForKey:(id)key defaultValue:(NSData *)value
916 : {
917 : return [self oo_objectOfClass:[NSData class] forKey:key defaultValue:value];
918 : }
919 :
920 :
921 : - (char) oo_charForKey:(id)key
922 : {
923 : return [self oo_charForKey:key defaultValue:0];
924 : }
925 :
926 :
927 : - (short) oo_shortForKey:(id)key
928 : {
929 : return [self oo_shortForKey:key defaultValue:0];
930 : }
931 :
932 :
933 : - (int) oo_intForKey:(id)key
934 : {
935 : return [self oo_intForKey:key defaultValue:0];
936 : }
937 :
938 :
939 : - (long) oo_longForKey:(id)key
940 : {
941 : return [self oo_longForKey:key defaultValue:0];
942 : }
943 :
944 :
945 : - (long long) oo_longLongForKey:(id)key
946 : {
947 : return [self oo_longLongForKey:key defaultValue:0];
948 : }
949 :
950 :
951 : - (unsigned char) oo_unsignedCharForKey:(id)key
952 : {
953 : return [self oo_unsignedCharForKey:key defaultValue:0];
954 : }
955 :
956 :
957 : - (unsigned short) oo_unsignedShortForKey:(id)key
958 : {
959 : return [self oo_unsignedShortForKey:key defaultValue:0];
960 : }
961 :
962 :
963 : - (unsigned int) oo_unsignedIntForKey:(id)key
964 : {
965 : return [self oo_unsignedIntForKey:key defaultValue:0];
966 : }
967 :
968 :
969 : - (unsigned long) oo_unsignedLongForKey:(id)key
970 : {
971 : return [self oo_unsignedLongForKey:key defaultValue:0];
972 : }
973 :
974 :
975 : - (unsigned long long) oo_unsignedLongLongForKey:(id)key
976 : {
977 : return [self oo_unsignedLongLongForKey:key defaultValue:0];
978 : }
979 :
980 :
981 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
982 : - (BOOL) oo_fuzzyBooleanForKey:(id)key
983 : {
984 : return [self oo_fuzzyBooleanForKey:key defaultValue:0.0f];
985 : }
986 : #endif
987 :
988 :
989 : - (double) oo_doubleForKey:(NSString *)key
990 : {
991 : return OODoubleFromObject([self objectForKey:key], 0.0);
992 : }
993 :
994 :
995 : - (float) oo_nonNegativeFloatForKey:(id)key
996 : {
997 : return OONonNegativeFloatFromObject([self objectForKey:key], 0.0f);
998 : }
999 :
1000 :
1001 : - (double) oo_nonNegativeDoubleForKey:(id)key
1002 : {
1003 : return OONonNegativeDoubleFromObject([self objectForKey:key], 0.0);
1004 : }
1005 :
1006 :
1007 : - (id) oo_objectOfClass:(Class)class forKey:(id)key
1008 : {
1009 : return [self oo_objectOfClass:class forKey:key defaultValue:nil];
1010 : }
1011 :
1012 :
1013 : - (NSSet *) oo_setForKey:(id)key
1014 : {
1015 : return [self oo_setForKey:key defaultValue:nil];
1016 : }
1017 :
1018 : @end
1019 :
1020 :
1021 : @implementation NSMutableArray (OOInserter)
1022 :
1023 : - (void) oo_addInteger:(long)value
1024 : {
1025 : [self addObject:[NSNumber numberWithLong:value]];
1026 : }
1027 :
1028 :
1029 : - (void) oo_addUnsignedInteger:(unsigned long)value
1030 : {
1031 : [self addObject:[NSNumber numberWithUnsignedLong:value]];
1032 : }
1033 :
1034 :
1035 : - (void) oo_addFloat:(double)value
1036 : {
1037 : [self addObject:[NSNumber numberWithDouble:value]];
1038 : }
1039 :
1040 :
1041 : - (void) oo_addBool:(BOOL)value
1042 : {
1043 : [self addObject:[NSNumber numberWithBool:value]];
1044 : }
1045 :
1046 :
1047 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
1048 : - (void) oo_addVector:(Vector)value
1049 : {
1050 : [self addObject:OOPropertyListFromVector(value)];
1051 : }
1052 :
1053 :
1054 : - (void) oo_addQuaternion:(Quaternion)value
1055 : {
1056 : [self addObject:OOPropertyListFromQuaternion(value)];
1057 : }
1058 : #endif
1059 :
1060 :
1061 : - (void) oo_insertInteger:(long)value atIndex:(NSUInteger)index
1062 : {
1063 : [self insertObject:[NSNumber numberWithLong:value] atIndex:index];
1064 : }
1065 :
1066 :
1067 : - (void) oo_insertUnsignedInteger:(unsigned long)value atIndex:(NSUInteger)index
1068 : {
1069 : [self insertObject:[NSNumber numberWithUnsignedLong:value] atIndex:index];
1070 : }
1071 :
1072 :
1073 : - (void) oo_insertFloat:(double)value atIndex:(NSUInteger)index
1074 : {
1075 : [self insertObject:[NSNumber numberWithDouble:value] atIndex:index];
1076 : }
1077 :
1078 :
1079 : - (void) oo_insertBool:(BOOL)value atIndex:(NSUInteger)index
1080 : {
1081 : [self insertObject:[NSNumber numberWithBool:value] atIndex:index];
1082 : }
1083 :
1084 :
1085 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
1086 : - (void) oo_insertVector:(Vector)value atIndex:(NSUInteger)index
1087 : {
1088 : [self insertObject:OOPropertyListFromVector(value) atIndex:index];
1089 : }
1090 :
1091 :
1092 : - (void) oo_insertQuaternion:(Quaternion)value atIndex:(NSUInteger)index
1093 : {
1094 : [self insertObject:OOPropertyListFromQuaternion(value) atIndex:index];
1095 : }
1096 : #endif
1097 :
1098 : @end
1099 :
1100 :
1101 : @implementation NSMutableDictionary (OOInserter)
1102 :
1103 : - (void) oo_setInteger:(long)value forKey:(id)key
1104 : {
1105 : [self setObject:[NSNumber numberWithLong:value] forKey:key];
1106 : }
1107 :
1108 :
1109 : - (void) oo_setUnsignedInteger:(unsigned long)value forKey:(id)key
1110 : {
1111 : [self setObject:[NSNumber numberWithUnsignedLong:value] forKey:key];
1112 : }
1113 :
1114 :
1115 : - (void) oo_setLongLong:(long long)value forKey:(id)key
1116 : {
1117 : [self setObject:[NSNumber numberWithLongLong:value] forKey:key];
1118 : }
1119 :
1120 :
1121 : - (void) oo_setUnsignedLongLong:(unsigned long long)value forKey:(id)key
1122 : {
1123 : [self setObject:[NSNumber numberWithUnsignedLongLong:value] forKey:key];
1124 : }
1125 :
1126 :
1127 : - (void) oo_setFloat:(double)value forKey:(id)key
1128 : {
1129 : [self setObject:[NSNumber numberWithDouble:value] forKey:key];
1130 : }
1131 :
1132 :
1133 : - (void) oo_setBool:(BOOL)value forKey:(id)key
1134 : {
1135 : [self setObject:[NSNumber numberWithBool:value] forKey:key];
1136 : }
1137 :
1138 :
1139 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
1140 : - (void) oo_setVector:(Vector)value forKey:(id)key
1141 : {
1142 : [self setObject:OOPropertyListFromVector(value) forKey:key];
1143 : }
1144 :
1145 : - (void) oo_setHPVector:(HPVector)value forKey:(id)key
1146 : {
1147 : [self setObject:OOPropertyListFromHPVector(value) forKey:key];
1148 : }
1149 :
1150 : - (void) oo_setQuaternion:(Quaternion)value forKey:(id)key
1151 : {
1152 : [self setObject:OOPropertyListFromQuaternion(value) forKey:key];
1153 : }
1154 : #endif
1155 :
1156 : @end
1157 :
1158 :
1159 : @implementation NSMutableSet (OOInserter)
1160 :
1161 : - (void) oo_addInteger:(long)value
1162 : {
1163 : [self addObject:[NSNumber numberWithLong:value]];
1164 : }
1165 :
1166 :
1167 : - (void) oo_addUnsignedInteger:(unsigned long)value
1168 : {
1169 : [self addObject:[NSNumber numberWithUnsignedLong:value]];
1170 : }
1171 :
1172 :
1173 : - (void) oo_addFloat:(double)value
1174 : {
1175 : [self addObject:[NSNumber numberWithDouble:value]];
1176 : }
1177 :
1178 :
1179 : - (void) oo_addBool:(BOOL)value
1180 : {
1181 : [self addObject:[NSNumber numberWithBool:value]];
1182 : }
1183 :
1184 :
1185 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
1186 : - (void) oo_addVector:(Vector)value
1187 : {
1188 : [self addObject:OOPropertyListFromVector(value)];
1189 : }
1190 :
1191 :
1192 : - (void) oo_addQuaternion:(Quaternion)value
1193 : {
1194 : [self addObject:OOPropertyListFromQuaternion(value)];
1195 : }
1196 : #endif
1197 :
1198 : @end
1199 :
1200 :
1201 0 : long long OOLongLongFromObject(id object, long long defaultValue)
1202 : {
1203 : long long llValue;
1204 :
1205 : if ([object respondsToSelector:@selector(longLongValue)]) llValue = [object longLongValue];
1206 : else if ([object respondsToSelector:@selector(longValue)]) llValue = [object longValue];
1207 : else if ([object respondsToSelector:@selector(intValue)]) llValue = [object intValue];
1208 : else llValue = defaultValue;
1209 :
1210 : return llValue;
1211 : }
1212 :
1213 :
1214 0 : unsigned long long OOUnsignedLongLongFromObject(id object, unsigned long long defaultValue)
1215 : {
1216 : unsigned long long ullValue;
1217 :
1218 : if ([object respondsToSelector:@selector(unsignedLongLongValue)]) ullValue = [object unsignedLongLongValue];
1219 : else if ([object respondsToSelector:@selector(unsignedLongValue)]) ullValue = [object unsignedLongValue];
1220 : else if ([object respondsToSelector:@selector(unsignedIntValue)]) ullValue = [object unsignedIntValue];
1221 : else if ([object respondsToSelector:@selector(intValue)]) ullValue = [object intValue];
1222 : else ullValue = defaultValue;
1223 :
1224 : return ullValue;
1225 : }
1226 :
1227 :
1228 0 : static inline BOOL IsSpaceOrTab(int value)
1229 : {
1230 : return value == ' ' || value == '\t';
1231 : }
1232 :
1233 :
1234 0 : static BOOL IsZeroString(NSString *string)
1235 : {
1236 : /* I don't particularly like regexps, but there are occasions...
1237 : To match NSString's behaviour for intValue etc. with non-zero numbers,
1238 : we need to skip any leading spaces or tabs (but not line breaks), get
1239 : an optional minus sign, then at least one 0. Any trailing junk is
1240 : ignored. It is assumed that this function is called for strings whose
1241 : numerical value has already been determined to be 0.
1242 : */
1243 :
1244 : unsigned long i = 0, count = [string length];
1245 0 : #define PEEK() ((i >= count) ? -1 : [string characterAtIndex:i])
1246 :
1247 : while (IsSpaceOrTab(PEEK())) ++i; // Skip spaces and tabs
1248 : if (PEEK() == ' ') ++i; // Skip optional hyphen-minus
1249 : return PEEK() == '0'; // If this is a 0, it's a numerical string.
1250 :
1251 : #undef PEEK
1252 : }
1253 :
1254 :
1255 0 : static BOOL BooleanFromString(NSString *string, BOOL defaultValue)
1256 : {
1257 : if (NSOrderedSame == [string caseInsensitiveCompare:@"yes"] ||
1258 : NSOrderedSame == [string caseInsensitiveCompare:@"true"] ||
1259 : NSOrderedSame == [string caseInsensitiveCompare:@"on"] ||
1260 : [string doubleValue] != 0.0) // Floating point is used so values like @"0.1" are treated as nonzero.
1261 : {
1262 : return YES;
1263 : }
1264 : else if (NSOrderedSame == [string caseInsensitiveCompare:@"no"] ||
1265 : NSOrderedSame == [string caseInsensitiveCompare:@"false"] ||
1266 : NSOrderedSame == [string caseInsensitiveCompare:@"off"] ||
1267 : IsZeroString(string))
1268 : {
1269 : return NO;
1270 : }
1271 : return defaultValue;
1272 : }
1273 :
1274 :
1275 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
1276 0 : static float FuzzyBooleanProbabilityFromString(NSString *string, float defaultValue)
1277 : {
1278 : if (NSOrderedSame == [string caseInsensitiveCompare:@"yes"] ||
1279 : NSOrderedSame == [string caseInsensitiveCompare:@"true"] ||
1280 : NSOrderedSame == [string caseInsensitiveCompare:@"on"] ||
1281 : [string doubleValue] != 0.0) // Floating point is used so values like @"0.1" are treated as nonzero.
1282 : {
1283 : return 1.0f;
1284 : }
1285 : else if (NSOrderedSame == [string caseInsensitiveCompare:@"no"] ||
1286 : NSOrderedSame == [string caseInsensitiveCompare:@"false"] ||
1287 : NSOrderedSame == [string caseInsensitiveCompare:@"off"] ||
1288 : IsZeroString(string))
1289 : {
1290 : return 0.0f;
1291 : }
1292 : return defaultValue;
1293 : }
1294 : #endif
1295 :
1296 :
1297 0 : BOOL OOBooleanFromObject(id object, BOOL defaultValue)
1298 : {
1299 : BOOL result;
1300 :
1301 : if ([object isKindOfClass:[NSString class]])
1302 : {
1303 : result = BooleanFromString(object, defaultValue);
1304 : }
1305 : else
1306 : {
1307 : if ([object respondsToSelector:@selector(boolValue)]) result = [object boolValue];
1308 : else if ([object respondsToSelector:@selector(intValue)]) result = [object intValue] != 0;
1309 : else result = defaultValue;
1310 : }
1311 :
1312 : return result;
1313 : }
1314 :
1315 :
1316 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
1317 0 : BOOL OOFuzzyBooleanFromObject(id object, float defaultValue)
1318 : {
1319 : float probability;
1320 :
1321 : if ([object isKindOfClass:[NSString class]])
1322 : {
1323 : probability = [object floatValue];
1324 :
1325 : // If our string represents zero, it might be erroneous input or simply yes/no,
1326 : // true/false or on/off valid boolean strings. Act on it.
1327 : if (probability == 0.0f && !IsZeroString(object))
1328 : {
1329 : probability = FuzzyBooleanProbabilityFromString(object, defaultValue);
1330 : }
1331 : }
1332 : else
1333 : {
1334 : probability = OOFloatFromObject(object, defaultValue);
1335 : }
1336 :
1337 : /* This will always be NO for negative values and YES for values
1338 : greater than 1, as expected. randf() is always less than 1, so
1339 : < is the correct operator here.
1340 : */
1341 : return randf() < probability;
1342 : }
1343 : #endif
1344 :
1345 :
1346 0 : float OOFloatFromObject(id object, float defaultValue)
1347 : {
1348 : float result;
1349 :
1350 : if ([object respondsToSelector:@selector(floatValue)])
1351 : {
1352 : result = [object floatValue];
1353 : if (result == 0.0f && [object isKindOfClass:[NSString class]] && !IsZeroString(object)) result = defaultValue;
1354 : }
1355 : else if ([object respondsToSelector:@selector(doubleValue)]) result = [object doubleValue];
1356 : else if ([object respondsToSelector:@selector(intValue)]) result = [object intValue];
1357 : else result = defaultValue;
1358 :
1359 : return result;
1360 : }
1361 :
1362 :
1363 0 : double OODoubleFromObject(id object, double defaultValue)
1364 : {
1365 : double result;
1366 :
1367 : if ([object respondsToSelector:@selector(doubleValue)])
1368 : {
1369 : result = [object doubleValue];
1370 : if (result == 0.0 && [object isKindOfClass:[NSString class]] && !IsZeroString(object)) result = defaultValue;
1371 : }
1372 : else if ([object respondsToSelector:@selector(floatValue)]) result = [object floatValue];
1373 : else if ([object respondsToSelector:@selector(intValue)]) result = [object intValue];
1374 : else result = defaultValue;
1375 :
1376 : return result;
1377 : }
1378 :
1379 :
1380 0 : float OONonNegativeFloatFromObject(id object, float defaultValue)
1381 : {
1382 : float result;
1383 :
1384 : if ([object respondsToSelector:@selector(floatValue)]) result = [object floatValue];
1385 : else if ([object respondsToSelector:@selector(doubleValue)]) result = [object doubleValue];
1386 : else if ([object respondsToSelector:@selector(intValue)]) result = [object intValue];
1387 : else return defaultValue; // Don't clamp default
1388 :
1389 : return fmax(result, 0.0f);
1390 : }
1391 :
1392 :
1393 0 : double OONonNegativeDoubleFromObject(id object, double defaultValue)
1394 : {
1395 : double result;
1396 :
1397 : if ([object respondsToSelector:@selector(doubleValue)]) result = [object doubleValue];
1398 : else if ([object respondsToSelector:@selector(floatValue)]) result = [object floatValue];
1399 : else if ([object respondsToSelector:@selector(intValue)]) result = [object intValue];
1400 : else return defaultValue; // Don't clamp default
1401 :
1402 : return fmax(result, 0.0);
1403 : }
1404 :
1405 :
1406 : #ifndef OOCOLLECTIONEXTRACTORS_SIMPLE
1407 0 : Vector OOVectorFromObject(id object, Vector defaultValue)
1408 : {
1409 : Vector result = defaultValue;
1410 : NSDictionary *dict = nil;
1411 :
1412 : if ([object isKindOfClass:[OONativeVector class]])
1413 : {
1414 : result = [object getVector];
1415 : }
1416 : else if ([object isKindOfClass:[NSString class]])
1417 : {
1418 : // This will only write result if a valid vector is found, and will write an error message otherwise.
1419 : ScanVectorFromString(object, &result);
1420 : }
1421 : else if ([object isKindOfClass:[NSArray class]] && [object count] == 3)
1422 : {
1423 : result.x = [object oo_floatAtIndex:0];
1424 : result.y = [object oo_floatAtIndex:1];
1425 : result.z = [object oo_floatAtIndex:2];
1426 : }
1427 : else if ([object isKindOfClass:[NSDictionary class]])
1428 : {
1429 : dict = object;
1430 : // Require at least one of the keys x, y, or z
1431 : if ([dict objectForKey:@"x"] != nil ||
1432 : [dict objectForKey:@"y"] != nil ||
1433 : [dict objectForKey:@"z"] != nil)
1434 : {
1435 : // Note: uses 0 for unknown components rather than components of defaultValue.
1436 : result.x = [dict oo_floatForKey:@"x" defaultValue:0.0f];
1437 : result.y = [dict oo_floatForKey:@"y" defaultValue:0.0f];
1438 : result.z = [dict oo_floatForKey:@"z" defaultValue:0.0f];
1439 : }
1440 : }
1441 :
1442 : return result;
1443 : }
1444 :
1445 0 : HPVector OOHPVectorFromObject(id object, HPVector defaultValue)
1446 : {
1447 : HPVector result = defaultValue;
1448 : NSDictionary *dict = nil;
1449 :
1450 : if ([object isKindOfClass:[NSString class]])
1451 : {
1452 : // This will only write result if a valid vector is found, and will write an error message otherwise.
1453 : ScanHPVectorFromString(object, &result);
1454 : }
1455 : else if ([object isKindOfClass:[NSArray class]] && [object count] == 3)
1456 : {
1457 : result.x = [object oo_doubleAtIndex:0];
1458 : result.y = [object oo_doubleAtIndex:1];
1459 : result.z = [object oo_doubleAtIndex:2];
1460 : }
1461 : else if ([object isKindOfClass:[NSDictionary class]])
1462 : {
1463 : dict = object;
1464 : // Require at least one of the keys x, y, or z
1465 : if ([dict objectForKey:@"x"] != nil ||
1466 : [dict objectForKey:@"y"] != nil ||
1467 : [dict objectForKey:@"z"] != nil)
1468 : {
1469 : // Note: uses 0 for unknown components rather than components of defaultValue.
1470 : result.x = [dict oo_doubleForKey:@"x" defaultValue:0.0];
1471 : result.y = [dict oo_doubleForKey:@"y" defaultValue:0.0];
1472 : result.z = [dict oo_doubleForKey:@"z" defaultValue:0.0];
1473 : }
1474 : }
1475 :
1476 : return result;
1477 : }
1478 :
1479 :
1480 0 : Quaternion OOQuaternionFromObject(id object, Quaternion defaultValue)
1481 : {
1482 : Quaternion result = defaultValue;
1483 : NSDictionary *dict = nil;
1484 :
1485 : if ([object isKindOfClass:[NSString class]])
1486 : {
1487 : // This will only write result if a valid quaternion is found, and will write an error message otherwise.
1488 : ScanQuaternionFromString(object, &result);
1489 : }
1490 : else if ([object isKindOfClass:[NSArray class]] && [object count] == 4)
1491 : {
1492 : result.w = [object oo_floatAtIndex:0];
1493 : result.x = [object oo_floatAtIndex:1];
1494 : result.y = [object oo_floatAtIndex:2];
1495 : result.z = [object oo_floatAtIndex:3];
1496 : }
1497 : else if ([object isKindOfClass:[NSDictionary class]])
1498 : {
1499 : dict = object;
1500 : // Require at least one of the keys w, x, y, or z
1501 : if ([dict objectForKey:@"w"] != nil ||
1502 : [dict objectForKey:@"x"] != nil ||
1503 : [dict objectForKey:@"y"] != nil ||
1504 : [dict objectForKey:@"z"] != nil)
1505 : {
1506 : // Note: uses 0 for unknown components rather than components of defaultValue.
1507 : result.w = [dict oo_floatForKey:@"w" defaultValue:0.0f];
1508 : result.x = [dict oo_floatForKey:@"x" defaultValue:0.0f];
1509 : result.y = [dict oo_floatForKey:@"y" defaultValue:0.0f];
1510 : result.z = [dict oo_floatForKey:@"z" defaultValue:0.0f];
1511 : }
1512 : }
1513 :
1514 : return result;
1515 : }
1516 :
1517 :
1518 0 : NSDictionary *OOPropertyListFromVector(Vector value)
1519 : {
1520 : return [NSDictionary dictionaryWithObjectsAndKeys:
1521 : [NSNumber numberWithFloat:value.x], @"x",
1522 : [NSNumber numberWithFloat:value.y], @"y",
1523 : [NSNumber numberWithFloat:value.z], @"z",
1524 : nil];
1525 : }
1526 :
1527 0 : NSDictionary *OOPropertyListFromHPVector(HPVector value)
1528 : {
1529 : return [NSDictionary dictionaryWithObjectsAndKeys:
1530 : [NSNumber numberWithDouble:value.x], @"x",
1531 : [NSNumber numberWithDouble:value.y], @"y",
1532 : [NSNumber numberWithDouble:value.z], @"z",
1533 : nil];
1534 : }
1535 :
1536 :
1537 0 : NSDictionary *OOPropertyListFromQuaternion(Quaternion value)
1538 : {
1539 : return [NSDictionary dictionaryWithObjectsAndKeys:
1540 : [NSNumber numberWithFloat:value.w], @"w",
1541 : [NSNumber numberWithFloat:value.x], @"x",
1542 : [NSNumber numberWithFloat:value.y], @"y",
1543 : [NSNumber numberWithFloat:value.z], @"z",
1544 : nil];
1545 : }
1546 : #endif
1547 :
1548 :
1549 0 : static NSSet *SetForObject(id object, NSSet *defaultValue)
1550 : {
1551 : if ([object isKindOfClass:[NSArray class]]) return [NSSet setWithArray:object];
1552 : else if ([object isKindOfClass:[NSSet class]]) return [[object copy] autorelease];
1553 :
1554 : return defaultValue;
1555 : }
1556 :
1557 :
1558 0 : static NSString *StringForObject(id object, NSString *defaultValue)
1559 : {
1560 : if ([object isKindOfClass:[NSString class]]) return object;
1561 : else if ([object respondsToSelector:@selector(stringValue)]) return [object stringValue];
1562 :
1563 : return defaultValue;
1564 : }
|