Add invalid cast exceptions to frame conversion operators.
This commit is contained in:
parent
4dff6eba91
commit
09ce8d3229
@ -78,6 +78,7 @@ namespace Quik.CommandMachine
|
||||
_type = FrameType.None
|
||||
};
|
||||
|
||||
#region Constructors
|
||||
public Frame(Command command) : this()
|
||||
{
|
||||
_type = FrameType.Command;
|
||||
@ -195,6 +196,8 @@ namespace Quik.CommandMachine
|
||||
_f4 = f4;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public T As<T>()
|
||||
{
|
||||
return (T)_object;
|
||||
@ -226,21 +229,109 @@ namespace Quik.CommandMachine
|
||||
}
|
||||
}
|
||||
|
||||
public static explicit operator int(in Frame frame) => frame._i1;
|
||||
public static explicit operator float(in Frame frame) => frame._f1;
|
||||
public static explicit operator Command(in Frame frame) => (Command)frame._i1;
|
||||
public static explicit operator QVec2(in Frame frame) =>
|
||||
frame.IsFloat ? new QVec2(frame._f1, frame._f2) : new QVec2(frame._i1, frame._i2);
|
||||
public static explicit operator QColor(in Frame frame) =>
|
||||
new QColor((byte)frame._i1, (byte)frame._i2, (byte)frame._i3, (byte)frame._i4);
|
||||
public static explicit operator QRectangle(in Frame frame) =>
|
||||
frame.IsFloat ?
|
||||
new QRectangle(frame._f1, frame._f2, frame._f3, frame._f4) :
|
||||
new QRectangle(frame._i1, frame._i2, frame._i3, frame._i4);
|
||||
public static explicit operator QLine(in Frame frame) =>
|
||||
frame.IsFloat ?
|
||||
new QLine(frame._f1, frame._f2, frame._f3, frame._f4) :
|
||||
new QLine(frame._i1, frame._i2, frame._i3, frame._i4);
|
||||
#region Frame->T Conversion
|
||||
|
||||
public static explicit operator int(in Frame frame)
|
||||
{
|
||||
switch (frame.Type)
|
||||
{
|
||||
default:
|
||||
throw new InvalidCastException();
|
||||
case FrameType.Command:
|
||||
case FrameType.IVec1:
|
||||
case FrameType.IVec2:
|
||||
case FrameType.IVec3:
|
||||
case FrameType.IVec4:
|
||||
return frame._i1;
|
||||
case FrameType.Vec1:
|
||||
case FrameType.Vec2:
|
||||
case FrameType.Vec3:
|
||||
case FrameType.Vec4:
|
||||
return (int)frame._f1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static explicit operator float(in Frame frame)
|
||||
{
|
||||
switch (frame.Type)
|
||||
{
|
||||
default:
|
||||
throw new InvalidCastException();
|
||||
case FrameType.IVec1:
|
||||
case FrameType.IVec2:
|
||||
case FrameType.IVec3:
|
||||
case FrameType.IVec4:
|
||||
return frame._i1;
|
||||
case FrameType.Vec1:
|
||||
case FrameType.Vec2:
|
||||
case FrameType.Vec3:
|
||||
case FrameType.Vec4:
|
||||
return frame._f1;
|
||||
}
|
||||
}
|
||||
|
||||
public static explicit operator Command(in Frame frame)
|
||||
{
|
||||
if (frame.Type != FrameType.Command)
|
||||
{
|
||||
throw new InvalidCastException("Not a command frame.");
|
||||
}
|
||||
|
||||
return (Command)frame._i1;
|
||||
}
|
||||
|
||||
public static explicit operator QVec2(in Frame frame)
|
||||
{
|
||||
switch (frame.Type)
|
||||
{
|
||||
default:
|
||||
throw new InvalidCastException();
|
||||
case FrameType.IVec2:
|
||||
case FrameType.IVec3:
|
||||
case FrameType.IVec4:
|
||||
return new QVec2(frame._i1, frame._i2);
|
||||
case FrameType.Vec2:
|
||||
case FrameType.Vec3:
|
||||
case FrameType.Vec4:
|
||||
return new QVec2(frame._f1, frame._f2);
|
||||
}
|
||||
}
|
||||
|
||||
public static explicit operator QColor(in Frame frame)
|
||||
{
|
||||
if (frame.Type != FrameType.IVec4)
|
||||
throw new InvalidCastException();
|
||||
|
||||
return new QColor((byte)frame._i1, (byte)frame._i2, (byte)frame._i3, (byte)frame._i4);
|
||||
}
|
||||
|
||||
public static explicit operator QRectangle(in Frame frame)
|
||||
{
|
||||
switch (frame.Type)
|
||||
{
|
||||
default:
|
||||
throw new InvalidCastException();
|
||||
case FrameType.IVec4:
|
||||
return new QRectangle(frame._i1, frame._i2, frame._i3, frame._i4);
|
||||
case FrameType.Vec4:
|
||||
return new QRectangle(frame._f1, frame._f2, frame._f3, frame._f4);
|
||||
}
|
||||
}
|
||||
|
||||
public static explicit operator QLine(in Frame frame)
|
||||
{
|
||||
switch (frame.Type)
|
||||
{
|
||||
default:
|
||||
throw new InvalidCastException();
|
||||
case FrameType.IVec4:
|
||||
return new QLine(frame._i1, frame._i2, frame._i3, frame._i4);
|
||||
case FrameType.Vec4:
|
||||
return new QLine(frame._f1, frame._f2, frame._f3, frame._f4);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
public static explicit operator Frame(int i) => new Frame(i);
|
||||
public static explicit operator Frame(float f) => new Frame(f);
|
||||
|
Loading…
Reference in New Issue
Block a user