using System; using System.Collections; using System.Collections.Generic; namespace Chernobyl.Collections.Generic { /// /// A special enumerator that moves over lists of /// types. This enumerator shields the user /// from the instance and skips over items /// in the list that are not alive (see ). /// /// The type that is held within the /// that will be iterated over with this /// . This type must be a reference type because /// this is required by public class WeakReferenceEnumerator : IEnumerator where T : class { /// /// Initializes a new instance of the /// class. /// /// The . public WeakReferenceEnumerator(IEnumerator> enumerator) { TheEnumerator = enumerator; } /// /// Gets the element in the collection at the current position of the /// enumerator. /// /// The element in the collection at the current position of /// the enumerator. public T Current { get { return TheEnumerator.Current.Target; } } /// /// Performs application-defined tasks associated with freeing, /// releasing, or resetting unmanaged resources. /// public void Dispose() { TheEnumerator.Dispose(); } /// /// Gets the element in the collection at the current position of the /// enumerator. /// /// /// The element in the collection at the current position of /// the enumerator. object IEnumerator.Current { get { return TheEnumerator.Current.Target; } } /// /// Advances the enumerator to the next element of the collection. /// /// /// true if the enumerator was successfully advanced to the next element; /// false if the enumerator has passed the end of the collection or if /// no alive objects exist. /// /// The collection /// was modified after the enumerator was created. public bool MoveNext() { // keep going until we run out of items to move through or until // we find an alive item bool result = false; do { result = TheEnumerator.MoveNext(); } while (result == true && TheEnumerator.Current.IsAlive == false); return result; } /// /// Sets the enumerator to its initial position, which is before the /// first element in the collection. /// /// The collection /// was modified after the enumerator was created. public void Reset() { TheEnumerator.Reset(); } /// /// The used to iterate through the /// items in the list. /// IEnumerator> TheEnumerator { get; set; } } }