3using System.Collections.Generic;
6using System.Collections;
7using System.Threading.Tasks;
29 protected Dictionary<string, Variable>
variables =
new Dictionary<string, Variable>();
30 private Stack<Dictionary<string, Variable>> stack =
null;
31 private TextWriter consoleOut =
null;
33 private readonly SemaphoreSlim semaphore =
new SemaphoreSlim(1);
34 private volatile bool active =
true;
58 if (this.variables.TryGetValue(Name, out
Variable))
62 if (!(this.contextVariables is
null))
82 if (this.variables.ContainsKey(Name))
86 if (!(this.contextVariables is
null))
100 public object this[
string Name]
105 return v.ValueObject;
112 this.
Add(Name, value);
128 lock (this.variables)
130 if (this.variables.TryGetValue(Name, out Result))
133 this.variables[Name] = Result =
new Variable(Name, Value);
147 public virtual bool Remove(
string VariableName)
151 lock (this.variables)
153 return this.variables.Remove(VariableName);
167 lock (this.variables)
169 this.variables.Clear();
184 if (this.stack is
null)
185 this.stack =
new Stack<Dictionary<string, Variable>>();
187 this.stack.Push(this.variables);
189 Dictionary<string, Variable> Clone =
new Dictionary<string, Variable>();
190 foreach (KeyValuePair<string, Variable> P
in this.variables)
191 Clone[P.Key] =
new Variable(P.Key, P.Value.ValueElement);
193 this.variables = Clone;
206 if (this.stack is
null)
209 this.variables = this.stack.Pop();
220 get => this.consoleOut;
221 set => this.consoleOut = value;
229 get => this.contextVariables;
230 set => this.contextVariables = value;
240 set => this.printer = value;
251 [Obsolete(
"Use the LockAsync method for better performance of processing asynchronous tasks.")]
279 [Obsolete(
"Use the LockAsync method for better performance of processing asynchronous tasks.")]
298 if (!await this.semaphore.WaitAsync(Timeout))
299 throw new TimeoutException(
"Unique access to variables connection was not granted.");
310 this.semaphore.Release();
324 lock (this.variables)
327 this.variables.Values.CopyTo(
Variables, 0);
346 private class VariableEnumerator : IEnumerator<Variable>
348 private readonly
Variable[] variables;
349 private int pos = -1;
360 return this.variables[this.pos];
364 object IEnumerator.Current
368 return this.variables[this.pos];
372 public void Dispose()
376 public bool MoveNext()
378 return ++this.pos < this.variables.Length;
391 IEnumerator IEnumerable.GetEnumerator()
440 if (!(this.OnPreview is
null))
443 if (this.contextVariables is
Variables v)
444 return v.HandlesPreview;
471 if (this.contextVariables is
Variables v)
502 get {
return !(this.OnStatus is
null); }
Static class managing the application event log. Applications and services log events on this static ...
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Exception thrown when a script has been aborted.
Base class for script exceptions.
Class managing a script expression.
Event arguments for preview events.
Event arguments for status events.
Contains information about a variable.
void SetValue(object Value)
Sets the value of the variable.
string Name
Name of variable.
TextWriter ConsoleOut
Console out interface. Can be used by functions and script to output data to the console.
virtual void Push()
Pushes the current set of variables to the stack. This state is restored by calling Pop....
virtual void Pop()
Pops a previously stored set of variables from the stack. Variables are stored on the stack by callin...
IContextVariables ContextVariables
Variables available during the current context.
void Preview(Expression Expression, IElement Result)
Reports a preview of the final result.
bool HandlesPreview
If previews are desired.
bool HandlesStatus
If status messages are desired.
void Status(Expression Expression, string Result)
Reports current status of execution.
void CopyTo(Variables Variables)
Copies available variables to another variable collection.
void CancelAbort()
Allows new script to be evaluated using this collection of variables.
virtual Variable Add(string Name, object Value)
Adds a variable to the collection.
Task LockAsync()
Locks the collection. The collection is by default thread safe. But if longer transactions require un...
async Task LockAsync(int Timeout)
Locks the collection. The collection is by default thread safe. But if longer transactions require un...
virtual void Clear()
Removes all variables from the collection.
ValuePrinter Printer
Delegate that converts values to strings for (implicit) printing. Default is null,...
virtual bool ContainsVariable(string Name)
If the collection contains a variable with a given name.
Dictionary< string, Variable > variables
Internal set of variables.
Variables(params Variable[] Variables)
Collection of variables.
Variable[] AvailableVariables
Returns an array of available variables.
void Release()
Releases the collection, previously locked through a call to Lock().
PreviewEventHandler OnPreview
Event raised when there is a new value to preview.
virtual bool TryGetVariable(string Name, out Variable Variable)
Tries to get a variable object, given its name.
void Lock()
Locks the collection. The collection is by default thread safe. But if longer transactions require un...
StatusEventHandler OnStatus
Event raised when a status message has been reported.
virtual bool Remove(string VariableName)
Removes a varaiable from the collection.
void Lock(int Timeout)
Locks the collection. The collection is by default thread safe. But if longer transactions require un...
void Abort()
Aborts the execution of script using this collection of variables.
IEnumerator< Variable > GetEnumerator()
Returns an enumerator that iterates through a collection.
Basic interface for all types of elements.
Variables available in a specific context.
bool TryGetVariable(string Name, out Variable Variable)
Tries to get a variable object, given its name.
bool ContainsVariable(string Name)
If the collection contains a variable with a given name.
delegate Task PreviewEventHandler(object Sender, PreviewEventArgs e)
Delegate for preview events.
delegate Task< string > ValuePrinter(object Value, Variables Variables)
Converts a value to a printable string.
delegate Task StatusEventHandler(object Sender, StatusEventArgs e)
Delegate for status events.