MegaBites-AI/Windows-powershell
0308
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System.Collections;5using System.Collections.Generic;6 7namespace System.Management.Automation8{9 internal sealed class SessionStateScopeEnumerator : IEnumerator<SessionStateScope>, IEnumerable<SessionStateScope>10 {11 /// <summary>12 /// Constructs an enumerator for enumerating through the session state scopes13 /// using the appropriate scoping rules (default to dynamic scoping).14 /// </summary>15 /// <param name="scope">16 /// The starting scope to start the enumeration from.17 /// </param>18 internal SessionStateScopeEnumerator(SessionStateScope scope)19 {20 Diagnostics.Assert(scope != null, "Caller to verify scope argument");21 _initialScope = scope;22 }23 24 /// <summary>25 /// Uses the proper scoping rules to get the next scope to do the lookup in.26 /// </summary>27 /// <returns>28 /// True if the enumerator was advanced to the next scope, or false otherwise.29 /// </returns>30 public bool MoveNext()31 {32 // On the first call to MoveNext the enumerator should be before33 // the first scope in the lookup and then advance to the first34 // scope in the lookup35 36 _currentEnumeratedScope = _currentEnumeratedScope == null ? _initialScope : _currentEnumeratedScope.Parent;37 38 // If the current scope is the global scope there is nowhere else39 // to do the lookup, so return false.40 return (_currentEnumeratedScope != null);41 }42 43 /// <summary>44 /// Sets the enumerator to before the first scope.45 /// </summary>46 public void Reset()47 {48 _currentEnumeratedScope = null;49 }50 51 /// <summary>52 /// Gets the current lookup scope.53 /// </summary>54 /// <exception cref="InvalidOperationException">55 /// The enumerator is positioned before the first element of the56 /// collection or after the last element.57 /// </exception>58 SessionStateScope IEnumerator<SessionStateScope>.Current59 {60 get61 {62 if (_currentEnumeratedScope == null)63 {64 throw PSTraceSource.NewInvalidOperationException();65 }66 67 return _currentEnumeratedScope;68 }69 }70 71 object IEnumerator.Current72 {73 get74 {75 return ((IEnumerator<SessionStateScope>)this).Current;76 }77 }78 79 /// <summary>80 /// Gets the IEnumerator for this class.81 /// </summary>82 /// <returns>83 /// The IEnumerator interface for this class.84 /// </returns>85 System.Collections.Generic.IEnumerator<SessionStateScope> System.Collections.Generic.IEnumerable<SessionStateScope>.GetEnumerator()86 {87 return this;88 }89 90 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()91 {92 return this;93 }94 95 public void Dispose()96 {97 Reset();98 }99 100 private readonly SessionStateScope _initialScope;101 private SessionStateScope _currentEnumeratedScope;102 }103}104 