using System; using System.Reflection; using Svelto.Utilities; namespace Svelto.WeakEvents { public class WeakAction : WeakAction { public WeakAction(Action listener) : base(listener.Target, listener.GetMethodInfoEx()) {} public void Invoke(T1 data1, T2 data2) { _data[0] = data1; _data[1] = data2; Invoke_Internal(_data); } readonly object[] _data = new object[2]; } public class WeakAction : WeakActionBase { public WeakAction(Action listener) : base(listener.Target, listener.GetMethodInfoEx()) {} public void Invoke(T data) { _data[0] = data; Invoke_Internal(_data); } readonly object[] _data = new object[1]; } public class WeakAction : WeakActionBase { public WeakAction(Action listener) : base(listener) {} public WeakAction(object listener, MethodInfo method) : base(listener, method) {} public void Invoke() { Invoke_Internal(null); } } public abstract class WeakActionBase { protected readonly DataStructures.WeakReference ObjectRef; protected readonly MethodInfo Method; public bool IsValid { get { return ObjectRef.IsValid; } } protected WeakActionBase(Action listener) : this(listener.Target, listener.GetMethodInfoEx()) {} protected WeakActionBase(object listener, MethodInfo method) { ObjectRef = new DataStructures.WeakReference(listener); Method = method; if (method.IsCompilerGenerated() == true) throw new ArgumentException("Cannot create weak event to anonymous method with closure."); } protected void Invoke_Internal(object[] data) { if (ObjectRef.IsValid) Method.Invoke(ObjectRef.Target, data); else Utility.Console.LogWarning("Target of weak action has been garbage collected"); } } }