Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / Orcas / SP / wpf / src / Core / CSharp / System / Windows / Media / Animation / WeakRefEnumerator.cs / 1 / WeakRefEnumerator.cs
//------------------------------------------------------------------------------
// Microsoft Windows Client Platform
// Copyright (c) Microsoft Corporation, 2003
//
// File: WeakRefEnumerator.cs
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace System.Windows.Media.Animation
{
///
/// An enumerator for collections of weak references.
///
///
/// The enumerator returns only live objects and removes dead
/// references from the list as it walks.
///
internal struct WeakRefEnumerator
{
#region Internal implementation
#region Construction
///
/// Creates an enumerator for the given list.
///
///
/// The list to enumerate.
///
internal WeakRefEnumerator(List list)
{
_list = list;
_readIndex = 0;
_writeIndex = 0;
_current = default(T);
#if DEBUG
_valid = false;
#endif // DEBUG
}
#endregion // Construction
#region Properties
///
/// Gets the current element in the collection.
///
///
/// The current element in the collection.
///
internal T Current
{
get
{
#if DEBUG
Debug.Assert(_valid);
#endif // DEBUG
return _current;
}
}
///
/// Gets the index of the current element in the collection.
///
///
/// The index of the current element in the collection.
///
internal int CurrentIndex
{
get
{
#if DEBUG
Debug.Assert(_valid);
#endif // DEBUG
return _writeIndex - 1;
}
}
#endregion // Properties
#region Methods
///
/// This method must be called when the enumerator is no longer
/// in use, if the last call to MoveNext returned true. The
/// enumerator is no longer valid after this call.
///
///
/// When MoveNext returns false, the collection being enumerated is
/// left in a good, known state, so nothing additional needs to be
/// done. However, before MoveNext returns false the collection is
/// in an intermediate state, so if the enumerator isn't advanced
/// all the way to the end then this method must be called to clean
/// up the collection and make sure that it ends in a known state.
/// Calling this method in the case where MoveNext has returned
/// false is OK. In that case, the method is a no-op.
///
internal void Dispose()
{
// Remove only those elements that we've previously seen.
// This means removing the range between the read and write
// indices, as that's dead space in the collection.
if (_readIndex != _writeIndex)
{
_list.RemoveRange(_writeIndex, _readIndex - _writeIndex);
_readIndex = _writeIndex = _list.Count;
}
_current = default(T);
#if DEBUG
_valid = false;
#endif // DEBUG
}
///
/// 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.
///
///
/// Elements that have been GC'ed are removed from the list.
///
internal bool MoveNext()
{
// Get a reference to the next element that has not yet been
// garbage-collected
while (_readIndex < _list.Count)
{
WeakReference currentRef = _list[_readIndex];
_current = (T)currentRef.Target;
if ( (object)_current != null)
{
// Found a live object. First compress the list, which
// is necessary if we've previously seen GC'ed objects.
if (_writeIndex != _readIndex)
{
_list[_writeIndex] = currentRef;
}
// Update internal state and return the found element
_readIndex++;
_writeIndex++;
#if DEBUG
_valid = true;
#endif // DEBUG
return true;
}
else
{
// This object was garbage-collected, so keep looking
_readIndex++;
}
}
// If we get here we didn't have any more live elements in the
// collection, so we should return false. This is also a good
// opportunity to clean up the list as necessary.
Dispose();
return false;
}
#endregion // Methods
#region Data
private List _list;
private T _current;
private int _readIndex;
private int _writeIndex;
#if DEBUG
private bool _valid;
#endif // DEBUG
#endregion // Data
#endregion // Internal implementation
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------------------------
// Microsoft Windows Client Platform
// Copyright (c) Microsoft Corporation, 2003
//
// File: WeakRefEnumerator.cs
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace System.Windows.Media.Animation
{
///
/// An enumerator for collections of weak references.
///
///
/// The enumerator returns only live objects and removes dead
/// references from the list as it walks.
///
internal struct WeakRefEnumerator
{
#region Internal implementation
#region Construction
///
/// Creates an enumerator for the given list.
///
///
/// The list to enumerate.
///
internal WeakRefEnumerator(List list)
{
_list = list;
_readIndex = 0;
_writeIndex = 0;
_current = default(T);
#if DEBUG
_valid = false;
#endif // DEBUG
}
#endregion // Construction
#region Properties
///
/// Gets the current element in the collection.
///
///
/// The current element in the collection.
///
internal T Current
{
get
{
#if DEBUG
Debug.Assert(_valid);
#endif // DEBUG
return _current;
}
}
///
/// Gets the index of the current element in the collection.
///
///
/// The index of the current element in the collection.
///
internal int CurrentIndex
{
get
{
#if DEBUG
Debug.Assert(_valid);
#endif // DEBUG
return _writeIndex - 1;
}
}
#endregion // Properties
#region Methods
///
/// This method must be called when the enumerator is no longer
/// in use, if the last call to MoveNext returned true. The
/// enumerator is no longer valid after this call.
///
///
/// When MoveNext returns false, the collection being enumerated is
/// left in a good, known state, so nothing additional needs to be
/// done. However, before MoveNext returns false the collection is
/// in an intermediate state, so if the enumerator isn't advanced
/// all the way to the end then this method must be called to clean
/// up the collection and make sure that it ends in a known state.
/// Calling this method in the case where MoveNext has returned
/// false is OK. In that case, the method is a no-op.
///
internal void Dispose()
{
// Remove only those elements that we've previously seen.
// This means removing the range between the read and write
// indices, as that's dead space in the collection.
if (_readIndex != _writeIndex)
{
_list.RemoveRange(_writeIndex, _readIndex - _writeIndex);
_readIndex = _writeIndex = _list.Count;
}
_current = default(T);
#if DEBUG
_valid = false;
#endif // DEBUG
}
///
/// 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.
///
///
/// Elements that have been GC'ed are removed from the list.
///
internal bool MoveNext()
{
// Get a reference to the next element that has not yet been
// garbage-collected
while (_readIndex < _list.Count)
{
WeakReference currentRef = _list[_readIndex];
_current = (T)currentRef.Target;
if ( (object)_current != null)
{
// Found a live object. First compress the list, which
// is necessary if we've previously seen GC'ed objects.
if (_writeIndex != _readIndex)
{
_list[_writeIndex] = currentRef;
}
// Update internal state and return the found element
_readIndex++;
_writeIndex++;
#if DEBUG
_valid = true;
#endif // DEBUG
return true;
}
else
{
// This object was garbage-collected, so keep looking
_readIndex++;
}
}
// If we get here we didn't have any more live elements in the
// collection, so we should return false. This is also a good
// opportunity to clean up the list as necessary.
Dispose();
return false;
}
#endregion // Methods
#region Data
private List _list;
private T _current;
private int _readIndex;
private int _writeIndex;
#if DEBUG
private bool _valid;
#endif // DEBUG
#endregion // Data
#endregion // Internal implementation
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- DBCSCodePageEncoding.cs
- ConfigurationPropertyAttribute.cs
- BooleanToVisibilityConverter.cs
- EntityContainerRelationshipSetEnd.cs
- ImageSource.cs
- CodeNamespaceCollection.cs
- ListView.cs
- Nullable.cs
- RSAProtectedConfigurationProvider.cs
- odbcmetadatafactory.cs
- ReceiveErrorHandling.cs
- Executor.cs
- ScriptComponentDescriptor.cs
- SqlUtils.cs
- HtmlContainerControl.cs
- XmlNotation.cs
- ExtendedPropertyDescriptor.cs
- FamilyMapCollection.cs
- DataGridViewButtonColumn.cs
- XmlDocument.cs
- DataGridRowClipboardEventArgs.cs
- DeferredTextReference.cs
- XmlSchemaDocumentation.cs
- ToolStripSplitStackLayout.cs
- RsaSecurityToken.cs
- RequestStatusBarUpdateEventArgs.cs
- ResourceExpressionBuilder.cs
- HtmlInputCheckBox.cs
- BufferAllocator.cs
- ScrollPatternIdentifiers.cs
- FrugalMap.cs
- MethodToken.cs
- BinaryUtilClasses.cs
- ScriptModule.cs
- DataControlFieldsEditor.cs
- NotificationContext.cs
- SecureConversationDriver.cs
- ByteKeyFrameCollection.cs
- CompiledQuery.cs
- QueryParameter.cs
- ErrorTableItemStyle.cs
- ProfileServiceManager.cs
- ApplicationServicesHostFactory.cs
- Substitution.cs
- IndexedString.cs
- RightsManagementInformation.cs
- PerfCounterSection.cs
- HighlightComponent.cs
- ImageCreator.cs
- DbSetClause.cs
- ColumnMap.cs
- AQNBuilder.cs
- ProcessModelInfo.cs
- PageEventArgs.cs
- BitmapSource.cs
- wpf-etw.cs
- TraversalRequest.cs
- TargetException.cs
- listitem.cs
- ProfileGroupSettings.cs
- iisPickupDirectory.cs
- Hyperlink.cs
- ValidationSummary.cs
- __TransparentProxy.cs
- SecurityContextTokenCache.cs
- ObjectStateManager.cs
- SurrogateSelector.cs
- FixedHyperLink.cs
- HttpListenerContext.cs
- MouseEvent.cs
- EventDescriptor.cs
- HwndAppCommandInputProvider.cs
- EllipseGeometry.cs
- CqlWriter.cs
- XmlWrappingReader.cs
- LineProperties.cs
- ActivityCodeGenerator.cs
- ResourceProviderFactory.cs
- HMAC.cs
- FolderLevelBuildProviderCollection.cs
- NetworkStream.cs
- SqlDataSource.cs
- LineVisual.cs
- DataBindingExpressionBuilder.cs
- ContainerVisual.cs
- Pair.cs
- ClientSponsor.cs
- BinaryObjectWriter.cs
- Debug.cs
- EnumValidator.cs
- TextServicesCompartmentEventSink.cs
- DbConnectionPoolIdentity.cs
- Binding.cs
- Component.cs
- AudioBase.cs
- Nullable.cs
- DbConnectionPoolOptions.cs
- EmptyStringExpandableObjectConverter.cs
- EntryPointNotFoundException.cs
- GraphicsState.cs