refactor: Move a few low-level DataStream types to DataStreamCore
So the lex/yacc generators can reference them
This commit is contained in:
1 parent
d1f5ae86fd
commit
ac1ede5986
10 files changed
+6
-30
No files matched your search
@@ -0,0 +1,181 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
//using BCG.Utilities;
|
||||
//using BCG.Utilities.General;
|
||||
using mbts;
|
||||
using mbts.spark;
|
||||
|
||||
namespace BCG
|
||||
{
|
||||
/// <summary>
|
||||
/// Base class for all the sreams
|
||||
/// </summary>
|
||||
public class DataStream
|
||||
{
|
||||
internal static event EventHandler onFireStart;
|
||||
protected bool myTestMode = Convert.ToBoolean (DataStreamLoadFileConfig.instance.isTestMode);
|
||||
protected bool myFirstSubImplemented = true,
|
||||
myLastSubImplemented = true;
|
||||
protected IDictionary myDelMap = new Hashtable ();
|
||||
private Guid myID = Guid.Empty;
|
||||
public DateTime last_fired_time = DateTime.MinValue;
|
||||
public int n_updates = 0;
|
||||
|
||||
public DataStream ()
|
||||
:
|
||||
this (null)
|
||||
{}
|
||||
|
||||
public DataStream (DataStream realSource)
|
||||
{
|
||||
myRealSource = realSource;
|
||||
myOnChangeDelegate = null;
|
||||
myPendingAdds = new Stack();
|
||||
}
|
||||
|
||||
public int numberOfSubscribers
|
||||
{
|
||||
get
|
||||
{
|
||||
return
|
||||
myOnChangeDelegate != null?
|
||||
myOnChangeDelegate.GetInvocationList().Length
|
||||
:
|
||||
0;
|
||||
}
|
||||
}
|
||||
|
||||
public delegate void ChangeDelegate(object newValue);
|
||||
public event ChangeDelegate onChange
|
||||
{
|
||||
add
|
||||
{
|
||||
ChangeDelegate currValue = value;
|
||||
if (myTestMode)
|
||||
{
|
||||
ChangeDelegateWrapper w = new ChangeDelegateWrapper (value);
|
||||
currValue = new ChangeDelegate (w.wrapperFunction);
|
||||
myDelMap.Add (value, currValue);
|
||||
}
|
||||
if (myRealSource != null)
|
||||
myRealSource.onChange += currValue;
|
||||
else
|
||||
{
|
||||
myOnChangeDelegate =
|
||||
(ChangeDelegate) Delegate.Combine (myOnChangeDelegate,currValue);
|
||||
|
||||
|
||||
if (myOnChangeDelegate.GetInvocationList().Length == 1) // first subscriber
|
||||
{
|
||||
myPendingAdds.Push(new ArrayList());
|
||||
myOnFirstSubscriber();
|
||||
ArrayList calledDelegates = (ArrayList) myPendingAdds.Pop();
|
||||
|
||||
object v = myGetValueForInitialSubscription();
|
||||
//BlueTrace.WriteViewStreamSubscribe(this.ID, v != null);
|
||||
if (v != null && calledDelegates.IndexOf(currValue) == -1)
|
||||
currValue (v);
|
||||
}
|
||||
else
|
||||
{
|
||||
object v = myGetValueForInitialSubscription();
|
||||
// BlueTrace.WriteViewStreamSubscribe(this.ID, v != null);
|
||||
if (v != null)
|
||||
currValue (v);
|
||||
}
|
||||
}
|
||||
}
|
||||
remove
|
||||
{
|
||||
ChangeDelegate currValue = value;
|
||||
// BlueTrace.WriteViewStreamUnsubscribe(this.ID);
|
||||
if (myTestMode)
|
||||
{
|
||||
currValue = myDelMap[value] as ChangeDelegate;
|
||||
myDelMap.Remove (value);
|
||||
}
|
||||
if (myRealSource != null)
|
||||
myRealSource.onChange -= currValue;
|
||||
else
|
||||
{
|
||||
myOnChangeDelegate =
|
||||
(ChangeDelegate) Delegate.Remove (myOnChangeDelegate,currValue);
|
||||
|
||||
// this is need to avoid a callback in a reentrant add.
|
||||
if (myPendingAdds.Count > 0)
|
||||
((ArrayList) (myPendingAdds.Peek())).Add(currValue);
|
||||
|
||||
if (myOnChangeDelegate == null) // last subscriber
|
||||
{
|
||||
myOnLastSubscriber();
|
||||
myLastValue = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual object myGetValueForInitialSubscription()
|
||||
{return myLastValue;}
|
||||
|
||||
protected void mySetRealSource (DataStream src)
|
||||
{myRealSource = src;}
|
||||
|
||||
protected virtual void myOnFirstSubscriber() {myFirstSubImplemented = false;}
|
||||
protected virtual void myOnLastSubscriber () {myLastSubImplemented = false;}
|
||||
protected virtual void myFireEvent(object o)
|
||||
{
|
||||
last_fired_time = DateTime.Now;
|
||||
n_updates++;
|
||||
// BlueEventLog.logAssert (myRealSource == null);
|
||||
myLastValue = o;
|
||||
if (myOnChangeDelegate != null)
|
||||
{
|
||||
// this is need to avoid a callback in a reentrant add.
|
||||
if (myPendingAdds.Count > 0)
|
||||
foreach (Delegate d in myOnChangeDelegate.GetInvocationList())
|
||||
((ArrayList) (myPendingAdds.Peek())).Add(d);
|
||||
if (onFireStart != null)
|
||||
onFireStart(this, EventArgs.Empty);
|
||||
myOnChangeDelegate (o);
|
||||
}
|
||||
}
|
||||
|
||||
public Guid ID
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this.myID == Guid.Empty) // && BlueTraceHelper.IsEnabled(BlueTraceLevel.Verbose, BlueTraceKeywords.Streams))
|
||||
{
|
||||
// BlueTraceHelper.FastInitGuid(ref this.myID);
|
||||
// BlueTrace.WriteViewStreamCreate(this.GetType().ToString(), this.myID);
|
||||
}
|
||||
return this.myID;
|
||||
}
|
||||
}
|
||||
|
||||
// this was made internal for testing purposes only.
|
||||
// don't mess with it.
|
||||
internal ChangeDelegate myOnChangeDelegate;
|
||||
protected object myLastValue;
|
||||
protected DataStream myRealSource;
|
||||
private Stack myPendingAdds;
|
||||
}
|
||||
|
||||
internal class ChangeDelegateWrapper
|
||||
{
|
||||
private HighPerformanceTimer myTimer = new HighPerformanceTimer ();
|
||||
private DataStream.ChangeDelegate myDel;
|
||||
|
||||
internal ChangeDelegateWrapper (DataStream.ChangeDelegate del) {myDel = del;}
|
||||
|
||||
internal void wrapperFunction (object o)
|
||||
{
|
||||
myTimer.start ();
|
||||
myDel (o);
|
||||
myTimer.stop ();
|
||||
string minfo = "Type = " + myDel.Method.DeclaringType.FullName + " , method = " + myDel.Method.Name;
|
||||
// BlueEventLog.logEvent (minfo + " , time = " + myTimer.lastIntervalTime.ToString (), BlueEventLevel.Performance);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user