|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- using System;
- using System.Reflection;
- using Svelto.Utilities;
-
- namespace Svelto.WeakEvents
- {
- public class WeakAction<T1, T2> : WeakAction
- {
- public WeakAction(Action<T1, T2> 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<T> : WeakActionBase
- {
- public WeakAction(Action<T> 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<object> 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<object>(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");
- }
- }
- }
|