|
using System;
using System.Collections;
using System.Runtime.CompilerServices;
using Yap.Data.Client.Properties;
namespace Yap.Data.Client
{
/// <summary>
/// Represents the scope stack for command execution.
/// </summary>
internal class ScopeStack
{
[ThreadStatic]
private static ScopeStack _Instance;
/// <summary>
/// Gets the <see cref="Yap.Data.Client.ScopeStack"/> for current thread.
/// </summary>
public static ScopeStack Instance
{
[MethodImpl(MethodImplOptions.Synchronized)]
get
{
if (_Instance == null)
{
_Instance = new ScopeStack();
}
return _Instance;
}
}
private readonly Stack _CurrentStack;
/// <summary>
/// Registers specified scope making it the active one.
/// </summary>
/// <param name="scope">The scope to register.</param>
public void RegisterScope(Scope scope)
{
_CurrentStack.Push(scope);
}
/// <summary>
/// Unregisters specified scope.
/// </summary>
/// <remarks>Specified scope should be on top of the current thread's scope stack to be unregistered.</remarks>
/// <param name="scope">The scope to unregister.</param>
public void UnregisterScope(Scope scope)
{
if (GetActiveScope() != scope)
{
throw new InvalidOperationException(
Resources.ItIsNotAllowedToUnregisterInactiveScopeException);
}
_CurrentStack.Pop();
}
/// <summary>
/// Gets the active scope from stack.
/// </summary>
/// <returns></returns>
public Scope GetActiveScope()
{
if (_CurrentStack.Count == 0)
{
return null;
}
return _CurrentStack.Peek() as Scope;
}
/// <summary>
/// Gets the value indicating whether active scope exists.
/// </summary>
public Boolean HasActiveScope
{
get
{
return GetActiveScope() != null;
}
}
private ScopeStack()
{
_CurrentStack = new Stack();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.