Line data Source code
1 0 : /* 2 : 3 : OOFilteringEnumerator.h 4 : By Jens Ayton 5 : 6 : NSEnumerator which takes an existing enumerator and filters out the objects 7 : that return NO from a given method. The method may take 0 or 1 arguments 8 : 9 : Example of use: 10 : NSArray *cats = [self cats]; 11 : NSEnumerator *happyCatEnum = [[cats objectEnumerator] filteredWithSelector:@selector(isHappy)]; 12 : id happyCat = nil; 13 : 14 : while ((happyCat = [happyCatEnum nextObject])) 15 : { 16 : ... 17 : } 18 : 19 : Filters can be trivially chained. For instance, to get happy red cats, use: 20 : NSEnumeratore *happyRedCatEnum = [[[cats objectEnumerator] 21 : filteredWithSelector:@selector(isHappy)] 22 : filteredWithSelector:@selector(hasColor:) 23 : andArgument:[NSColor redColor]]; 24 : 25 : Objects that do not respond to the filter selector are treated as if they had 26 : returned NO. 27 : 28 : Bonus feature: adds NSArray-like (but non-exception-throwing) 29 : makeObjectsPerformSelector: to all enumerators. 30 : 31 : 32 : Copyright (C) 2008-2013 Jens Ayton 33 : 34 : Permission is hereby granted, free of charge, to any person obtaining a copy 35 : of this software and associated documentation files (the "Software"), to deal 36 : in the Software without restriction, including without limitation the rights 37 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 38 : copies of the Software, and to permit persons to whom the Software is 39 : furnished to do so, subject to the following conditions: 40 : 41 : The above copyright notice and this permission notice shall be included in all 42 : copies or substantial portions of the Software. 43 : 44 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 45 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 46 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 47 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 48 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 49 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 50 : SOFTWARE. 51 : 52 : */ 53 : 54 : #import "OOCocoa.h" 55 : 56 : 57 0 : @interface OOFilteringEnumerator: NSEnumerator 58 : { 59 : @private 60 0 : NSEnumerator *_underlyingEnum; 61 0 : SEL _selector; 62 0 : id _argument; 63 0 : BOOL _takesArgument; 64 : } 65 : 66 0 : + (id) filterEnumerator:(NSEnumerator *)enumerator withSelector:(SEL)selector; 67 0 : + (id) filterEnumerator:(NSEnumerator *)enumerator withSelector:(SEL)selector andArgument:(id)argument; 68 : 69 0 : - (id) initWithUnderlyingEnumerator:(NSEnumerator *)enumerator 70 : withSelector:(SEL)selector 71 : takingArgument:(BOOL)takesArgument 72 : argumentValue:(id)argument; 73 : 74 : @end 75 : 76 : 77 : @interface NSEnumerator (OOFilteringEnumerator) 78 : 79 0 : - (id) filteredWithSelector:(SEL)selector; 80 0 : - (id) filteredWithSelector:(SEL)selector andArgument:(id)argument; 81 : 82 : @end 83 : 84 : 85 : @interface NSArray (OOFilteringEnumerator) 86 : 87 0 : - (id) objectEnumeratorFilteredWithSelector:(SEL)selector; 88 0 : - (id) objectEnumeratorFilteredWithSelector:(SEL)selector andArgument:(id)argument; 89 : 90 : @end 91 : 92 : 93 : @interface NSSet (OOFilteringEnumerator) 94 : 95 0 : - (id) objectEnumeratorFilteredWithSelector:(SEL)selector; 96 0 : - (id) objectEnumeratorFilteredWithSelector:(SEL)selector andArgument:(id)argument; 97 : 98 : @end 99 : 100 : 101 : @interface NSDictionary (OOFilteringEnumerator) 102 : 103 0 : - (id) objectEnumeratorFilteredWithSelector:(SEL)selector; 104 0 : - (id) objectEnumeratorFilteredWithSelector:(SEL)selector andArgument:(id)argument; 105 : 106 0 : - (id) keyEnumeratorFilteredWithSelector:(SEL)selector; 107 0 : - (id) keyEnumeratorFilteredWithSelector:(SEL)selector andArgument:(id)argument; 108 : 109 : @end 110 : 111 : 112 : @interface NSEnumerator (OOMakeObjectsPerformSelector) 113 : 114 0 : - (void)makeObjectsPerformSelector:(SEL)selector; 115 0 : - (void)makeObjectsPerformSelector:(SEL)selector withObject:(id)argument; 116 : 117 : @end