using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using Yaulw.Tools;
namespace Yaulw.Other
{
///
/// Easy StateManager to store and retrieve various Application States using an Enum
///
///
/// internal enum State
/// {
/// App_Started_bool,
/// App_ErrorsOccured_bool,
/// SpecialMode_CommandLine_Mode_bool,
/// }
///
/// internal static StateM AppState = new StateM(typeof(State));
///
///
///
public class StateM : IDisposable
{
#region Private Members
// Private Parsing Enums
private Type _StateKeys = null;
private StringDictionary _sdCurrentState = new StringDictionary(); // For State String Convertible Types
private Dictionary _sdCurrentStateObj = new Dictionary(); // For State Object Types
private bool _disposed = false;
private object _lock = new object();
#endregion
#region Construction
///
/// The StateM needs to know what Keys to Get/Set
///
/// Pass an Enum used to determine StateKeys
public StateM(Type Enum_StateKeys)
{
if (Enum_StateKeys.IsEnum)
{
_StateKeys = Enum_StateKeys;
}
else
throw new ArgumentException("StateKeys must be an Enum");
}
///
/// Finalizer
///
~StateM()
{
Dispose(true);
}
#endregion
#region Public Properties
///
/// True if User Has passed in State Values
///
public bool HasStates { get { return (_sdCurrentState.Count >= 1 || _sdCurrentStateObj.Count >= 1); } }
#endregion
#region Public Methods
///
/// Main Entry Function to Retrieve an State Value
///
/// Should be a System Type like string, bool, int32, double, decimal, etc...
/// State Value you want to retrieve
/// Default value to use if nothing was retrieved * Error occured *
/// value or default value, if not found
public T GetStateValue(Enum stateKey, T DefaultValue)
{
lock (_lock)
{
T RetVal = DefaultValue;
if (ObjTool.IsOfTypeConvertibleToString(DefaultValue))
{
string Value = String.Empty;
if (GetStateValue(stateKey, out Value) && !String.IsNullOrEmpty(Value))
RetVal = ObjTool.ConvertStringToObj(Value);
}
else
{
object o = null;
if (GetStateValue(stateKey, out o) && (o != null))
RetVal = (T)o;
}
return RetVal;
}
}
///
/// Main Entry Function to Set a State Value
///
/// Should be a System Type like string, bool, int32, double, decimal, etc...
/// State Value you want to set
/// Value you want to set
/// true if successful, false otherwise
public bool SetStateValue(Enum stateKey, T Value)
{
lock (_lock)
{
bool bSuccess = false;
if (ObjTool.IsOfTypeConvertibleToString(Value))
{
string strValue = ObjTool.ConvertObjToString(Value);
bSuccess = SetStateValue(stateKey, strValue);
}
else
{
bSuccess = SetStateValue(stateKey, (object)Value);
}
return bSuccess;
}
}
#endregion
#region Private Helper Methods
///
/// Private GetStateValue Getter Function
///
/// pass in the State to look for
/// Returns the value or String.Empty, if none
/// true if the State Value Exists, false otherwise
private bool GetStateValue(Enum stateKey, out string Value)
{
lock (_lock)
{
Value = String.Empty;
if (_sdCurrentState.ContainsKey(stateKey.ToString()))
{
Value = _sdCurrentState[stateKey.ToString()];
return true;
}
return false;
}
}
///
/// Private GetStateValue Getter Function
///
/// pass in the State to look for
/// Returns the object or null, if noone
/// true if the State Value Exists, false otherwise
private bool GetStateValue(Enum stateKey, out object o)
{
lock (_lock)
{
o = null;
if (_sdCurrentStateObj.ContainsKey(stateKey.ToString()))
{
o = _sdCurrentStateObj[stateKey.ToString()];
return true;
}
return false;
}
}
///
/// Private SetStateValue Setter Function
///
/// pass in the State to Set
/// The Value to Set
/// true if the State Value was set, false otherwise
private bool SetStateValue(Enum stateKey, string Value)
{
lock (_lock)
{
try
{
_sdCurrentState[stateKey.ToString()] = Value;
return true;
}
catch (Exception) { /* ignore */ }
return false;
}
}
///
/// Private SetStateValue Setter Function
///
/// pass in the State to Set
/// The Object to Set
/// true if the State Value was set, false otherwise
private bool SetStateValue(Enum stateKey, object o)
{
lock (_lock)
{
try
{
_sdCurrentStateObj[stateKey.ToString()] = o;
return true;
}
catch (Exception) { /* ignore */ }
return false;
}
}
#endregion
#region IDisposable Members
///
/// Dispose the State Object
///
public void Dispose()
{
Dispose(true);
// Use SupressFinalize in case a subclass
// of this type implements a finalizer
GC.SuppressFinalize(this);
}
///
/// Dispose the State Object
///
/// true, if called from within
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
foreach (object o in _sdCurrentStateObj)
{
if (o is IDisposable)
((IDisposable)o).Dispose();
}
_sdCurrentStateObj.Clear();
}
// Indicate that the instance has been disposed.
_sdCurrentState = null;
_sdCurrentStateObj = null;
_disposed = true;
}
}
#endregion
}
}