OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015, Google Inc. |
| 4 * All rights reserved. |
| 5 * |
| 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions are |
| 8 * met: |
| 9 * |
| 10 * * Redistributions of source code must retain the above copyright |
| 11 * notice, this list of conditions and the following disclaimer. |
| 12 * * Redistributions in binary form must reproduce the above |
| 13 * copyright notice, this list of conditions and the following disclaimer |
| 14 * in the documentation and/or other materials provided with the |
| 15 * distribution. |
| 16 * * Neither the name of Google Inc. nor the names of its |
| 17 * contributors may be used to endorse or promote products derived from |
| 18 * this software without specific prior written permission. |
| 19 * |
| 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 * |
| 32 */ |
| 33 |
| 34 #import "GRXNSFastEnumerator.h" |
| 35 |
| 36 @implementation GRXNSFastEnumerator { |
| 37 id<NSFastEnumeration> _container; |
| 38 NSFastEnumerationState _state; |
| 39 // Number of elements of the container currently in the _state.itemsPtr array. |
| 40 NSUInteger _count; |
| 41 // The index of the next object to return from the _state.itemsPtr array. |
| 42 NSUInteger _index; |
| 43 // A "buffer of one element," for the containers that enumerate their elements
one by one. Those |
| 44 // will set _state.itemsPtr to point to this. |
| 45 // The NSFastEnumeration protocol requires it to be __unsafe_unretained, but t
hat's alright |
| 46 // because the only use we'd make of its value is to return it immediately as
the result of |
| 47 // nextObject. |
| 48 __unsafe_unretained id _bufferValue; |
| 49 // Neither NSEnumerator nor NSFastEnumeration instances are required to work c
orrectly when the |
| 50 // underlying container is mutated during iteration. The expectation is that a
n exception is |
| 51 // thrown when that happens. So we check for mutations. |
| 52 unsigned long _mutationFlag; |
| 53 BOOL _mutationFlagIsSet; |
| 54 } |
| 55 |
| 56 - (instancetype)init { |
| 57 return [self initWithContainer:nil]; |
| 58 } |
| 59 |
| 60 // Designated initializer. |
| 61 - (instancetype)initWithContainer:(id<NSFastEnumeration>)container { |
| 62 if ((self = [super init])) { |
| 63 _container = container; |
| 64 } |
| 65 return self; |
| 66 } |
| 67 |
| 68 - (id)nextObject { |
| 69 if (_index == _count) { |
| 70 _index = 0; |
| 71 _count = [_container countByEnumeratingWithState:&_state objects:&_bufferVal
ue count:1]; |
| 72 if (_count == 0) { |
| 73 // Enumeration is over. |
| 74 _container = nil; |
| 75 return nil; |
| 76 } |
| 77 if (_mutationFlagIsSet) { |
| 78 NSAssert(_mutationFlag == *(_state.mutationsPtr), |
| 79 @"container was mutated while being enumerated"); |
| 80 } else { |
| 81 _mutationFlag = *(_state.mutationsPtr); |
| 82 _mutationFlagIsSet = YES; |
| 83 } |
| 84 } |
| 85 return _state.itemsPtr[_index++]; |
| 86 } |
| 87 @end |
OLD | NEW |