|
using System;
using System.Diagnostics;
namespace IntentionalLogic
{
[Serializable]
public struct IntentionalBool
{
[Flags]
private enum State : byte
{ False, Unknown, NotUnknown, True };
private State state;
private IntentionalBool(State state)
{
Debug.Assert(state <= State.True);
this.state = state;
}
public static IntentionalBool True
{
get { return new IntentionalBool(State.True); }
}
public static IntentionalBool False
{
get { return new IntentionalBool(State.False);}
}
public static IntentionalBool Unknown
{
get { return new IntentionalBool(State.Unknown); }
}
public static IntentionalBool NotUnknown
{
get { return new IntentionalBool(State.NotUnknown); }
}
public bool IsKnown
{
get { return state == State.False | state == State.True; }
}
public static bool operator == (IntentionalBool p, IntentionalBool q)
{
return p.state == q.state;
}
public static bool operator != (IntentionalBool p, IntentionalBool q)
{
return p.state != q.state;
}
public override bool Equals(object obj)
{
return obj is IntentionalBool && ((IntentionalBool) obj).state == state;
}
public override int GetHashCode()
{
return (int) state;
}
public override string ToString()
{
return state.ToString();
}
/// <exception cref="ArgumentNullException">value is null</exception>
/// <exception cref="ArgumentException">value is not one of
/// "True", "Unknown", "NotUnknown", "False"</exception>
public static IntentionalBool Parse(string value)
{
return new IntentionalBool((State) Enum.Parse(typeof(State), value));
}
public static IntentionalBool operator & (IntentionalBool p, IntentionalBool q)
{
return new IntentionalBool(p.state & q.state);
}
public static IntentionalBool operator | (IntentionalBool p, IntentionalBool q)
{
return new IntentionalBool(p.state | q.state);
}
public static IntentionalBool operator ^ (IntentionalBool p, IntentionalBool q)
{
return new IntentionalBool(p.state ^ q.state);
}
public static IntentionalBool operator ! (IntentionalBool p)
{
return new IntentionalBool(p.state ^ State.True);
}
public static bool operator true (IntentionalBool p)
{
return p.state == State.True;
}
public static bool operator false (IntentionalBool p)
{
return p.state == State.False;
}
public static implicit operator IntentionalBool (bool b)
{
return b ? True : False;
}
public static explicit operator bool (IntentionalBool p)
{
return p.state == State.True;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.