Commit c65138d0 authored by Ben Lambell's avatar Ben Lambell

Improve support for diagnostics, tolerance of NULLs

parent 7b80be2c
......@@ -10,8 +10,7 @@ namespace Recurse.Common.Collections
public static Enumeration<T> Get<T>(IEnumerable<T> items)
{
if (items == null)
throw new ArgumentNullException(nameof(items));
return null;
return items as Enumeration<T> ?? new Enumeration<T>(items);
}
......
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace Recurse.Common.Diagnostics
{
public class StackLog
{
#region Instance Members
private IList<StackLog> Entries { get; } = new List<StackLog>();
private string Text { get; }
public StackLog(string text)
{
Text = text;
}
#endregion
private const int TabWidth = 4;
private static Stack<StackLog> Stack = new Stack<StackLog>();
private static readonly StackLog Root = new StackLog("Start Logging.");
private static StackLog Current => Stack.Count == 0 ? Root : Stack.Peek();
private static IEnumerable<string> TraceLines
{
get
{
var depth = 0;
foreach (var entry in Stack.Reverse())
{
var text = entry.Text.PadLeft(entry.Text.Length + depth * TabWidth);
yield return text;
depth++;
}
}
}
public static string Trace => string.Join("\n", TraceLines);
public static string AllLogs => string.Join("\n", Root.Entries.SelectMany(entry => GetLines(entry, 0)));
public static string CurrentLog => string.Join("\n", GetLines(Root.Entries.Last(), 0));
[Conditional("DEBUG")]
public static void Start(string text)
{
var entry = new StackLog(text);
Current.Entries.Add(entry);
Stack.Push(entry);
}
[Conditional("DEBUG")]
public static void End()
{
if (Stack.Count != 0)
{
Stack.Pop();
}
}
[Conditional("DEBUG")]
public static void Write(string text)
{
var entry = new StackLog(text);
Current.Entries.Add(entry);
}
private static IEnumerable<string> GetLines(StackLog entry, int depth)
{
var text = entry.Text.PadLeft(entry.Text.Length + depth * TabWidth);
yield return text;
foreach (var child in entry.Entries)
{
foreach (var line in GetLines(child, depth + 1))
{
yield return line;
}
}
}
}
}
......@@ -7,28 +7,29 @@
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Recurse.Common.Messaging
{
[Serializable]
public class ReceiverCollection<TReceiver> : IReceiverCollection<TReceiver>
public class ReceiverCollection<TReceiver> : IReceiverCollection<TReceiver>, IEnumerable<TReceiver>
{
private List<object> _receivers;
public virtual void Add(TReceiver receiver)
{
if (receiver == null)
throw new ArgumentNullException();
GetOrCreateReceivers().Add(receiver);
throw new ArgumentNullException(nameof(receiver));
AddElement(receiver);
}
public virtual void AddWeak(TReceiver receiver)
{
if (receiver == null)
throw new ArgumentNullException();
GetOrCreateReceivers().Add(new WeakReference(receiver));
throw new ArgumentNullException(nameof(receiver));
AddElement(new WeakReference(receiver));
}
public virtual void Remove(TReceiver receiver)
......@@ -43,7 +44,7 @@ namespace Recurse.Common.Messaging
public void ForEach(Action<TReceiver> action)
{
foreach (var receiver in GetReceivers().ToArray())
foreach (var receiver in this.ToArray())
{
try
{
......@@ -56,42 +57,31 @@ namespace Recurse.Common.Messaging
}
}
private List<object> GetOrCreateReceivers()
private void AddElement(object element)
{
if (_receivers == null)
_receivers = new List<object>();
return _receivers;
_receivers.Add(element);
}
private IEnumerable<TReceiver> GetReceivers()
public IEnumerator<TReceiver> GetEnumerator()
{
if (_receivers != null)
{
for (var i = 0; i < _receivers.Count; i++)
foreach (var element in _receivers)
{
var current = _receivers[i];
var weakReference = current as WeakReference;
if (weakReference != null)
{
var receiver = (TReceiver)weakReference.Target;
if (receiver != null)
{
yield return receiver;
}
else
{
_receivers.RemoveAt(i);
}
}
else
{
var receiver = (TReceiver)current;
var receiver = element is WeakReference ?
(TReceiver)((WeakReference)element).Target :
(TReceiver)element;
if (receiver != null)
yield return receiver;
}
}
_receivers.RemoveAll(r => r is WeakReference && ((WeakReference)r).Target == null);
}
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
private static bool Contains(object receiver, TReceiver target)
{
var weakReference = receiver as WeakReference;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment