using System;
namespace Chernobyl.Switch
{
///
/// Known as an "AND Gate" in boolean logic, this switch will fire the event
/// when both
/// and have been invoked.
///
public class AndSwitch : ISwitch
{
///
/// Constructor.
///
public AndSwitch() {}
///
/// Constructor.
///
/// Methods to assign to the event
/// .
public AndSwitch(params EventHandler[] onSwitchHandlers)
{
foreach (EventHandler eh in onSwitchHandlers)
SwitchedOn += eh;
}
///
/// An event handler that can be assigned to an event. When this method
/// is invoked, it will invoke the
/// event provided has been invoked
/// as well.
///
/// The sender of the event.
/// The arguments for the event.
public void Switch1(object sender, EventArgs e)
{
Switches[0] = true;
if(Switches[1] == true && SwitchedOn != null)
SwitchedOn(this, e);
}
///
/// An event handler that can be assigned to an event. When this method
/// is invoked, it will invoke the
/// event provided has been invoked
/// as well.
///
/// The sender of the event.
/// The argument for the event.
public void Switch2(object sender, EventArgs e)
{
Switches[1] = true;
if(Switches[0] == true && SwitchedOn != null)
SwitchedOn(this, e);
}
///
/// An event that is raised when both
/// and have been invoked.
///
public event EventHandler SwitchedOn;
///
/// An event that is raised right after this switch has been switched
/// off (or reset).
///
public event EventHandler SwitchedOff;
///
/// Resets the switch to its original state or the its state after right
/// after its creation.
///
public void Reset()
{
Switches[0] = false;
Switches[1] = false;
if (SwitchedOff != null)
SwitchedOff(this, EventArgs.Empty);
}
///
/// An event handler that can be assigned to an event. This event handler
/// will reset the switch to its original state. It is exactly the same
/// as calling ISwitch.Reset().
///
/// The sender of the event.
/// The arguments to the event.
public void Reset(object sender, EventArgs e)
{
Reset();
}
///
/// The switches of this switch.
///
bool[] Switches = new bool[2];
}
}