76 lines
2.9 KiB
C#
76 lines
2.9 KiB
C#
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Mail;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Firebird2D.EventPipelines
|
|
{
|
|
/// <summary>
|
|
/// Eventpypeline which transports an event to every Subscyber
|
|
/// </summary>
|
|
/// <typeparam name="T">Event Arguments for the Pipeline event</typeparam>
|
|
public class Pipeline <T> : I_Pipeline where T : EventArgs
|
|
{
|
|
/// <summary>
|
|
/// Class witch owns the Pipeline and has the right to send events or delete the pipeline
|
|
/// </summary>
|
|
public object PipelineMaster { get; }
|
|
|
|
//EventHandler to which the subscribers subribe.
|
|
private event EventHandler<T>? OnEvent;
|
|
|
|
/// <summary>
|
|
/// Pipeline Constructor
|
|
/// </summary>
|
|
/// <param name="pipelineMaster">Every pipeline needs a Master which is able to send events or Destroy the pipeline</param>
|
|
public Pipeline(object pipelineMaster)
|
|
{
|
|
PipelineMaster = pipelineMaster;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check if no one subscribed to the event
|
|
/// </summary>
|
|
public bool IsEventEmpty { get { return OnEvent != null; } }
|
|
|
|
/// <summary>
|
|
/// Subscribe to this event Pipeline
|
|
/// </summary>
|
|
/// <param name="handler">Handler witch should be Invoked when the event is send</param>
|
|
public void Subscribe(Delegate handler)
|
|
{
|
|
OnEvent += (EventHandler<T>)handler;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Unsubscribe from this event Pipeline
|
|
/// </summary>
|
|
/// <param name="handler">Handler witch should be removed from the Pipeline</param>
|
|
public void Unsubscribe(Delegate handler)
|
|
{
|
|
OnEvent -= (EventHandler<T>)handler;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Method to send an event through the pipeline
|
|
/// </summary>
|
|
/// <param name="sender">Sneder object must be Pipelinemaster or the Pipeline throws an exception</param>
|
|
/// <param name="args">Arguments that should be sent</param>
|
|
/// <exception cref="ArgumentNullException">Exception when sender or arguments ar null</exception>
|
|
/// <exception cref="ArgumentException">Exception wehn Arguments are not equal the Pipeline type</exception>
|
|
/// <exception cref="InvalidDataException">Exception when sender is not Pipeline master</exception>
|
|
public void SendEvent(object sender, T args)
|
|
{
|
|
if (sender == null || args == null) throw new ArgumentNullException(nameof(sender));
|
|
if (args is not T) throw new ArgumentException($"Invalid EventArgs type. Expected {typeof(T)}");
|
|
if (sender.Equals(PipelineMaster)) throw new InvalidDataException("Only the pipeline master is allowed to send events through this pipeline.");
|
|
|
|
OnEvent?.Invoke(sender, args);
|
|
|
|
}
|
|
}
|
|
}
|