From 09ce8d32299ddb82508371ca6fcf0dfd6782df98 Mon Sep 17 00:00:00 2001 From: "H. Utku Maden" Date: Tue, 9 Apr 2024 10:50:52 +0300 Subject: [PATCH] Add invalid cast exceptions to frame conversion operators. --- Quik/CommandMachine/Frame.cs | 185 ++++++++++++++++++++++++++--------- 1 file changed, 138 insertions(+), 47 deletions(-) diff --git a/Quik/CommandMachine/Frame.cs b/Quik/CommandMachine/Frame.cs index c0ec7e1..58ecf4b 100644 --- a/Quik/CommandMachine/Frame.cs +++ b/Quik/CommandMachine/Frame.cs @@ -9,22 +9,22 @@ namespace Quik.CommandMachine [FieldOffset(0)] private FrameType _type; - [FieldOffset(sizeof(FrameType) + 0*sizeof(int))] + [FieldOffset(sizeof(FrameType) + 0 * sizeof(int))] private int _i1; - [FieldOffset(sizeof(FrameType) + 1*sizeof(int))] + [FieldOffset(sizeof(FrameType) + 1 * sizeof(int))] private int _i2; - [FieldOffset(sizeof(FrameType) + 2*sizeof(int))] + [FieldOffset(sizeof(FrameType) + 2 * sizeof(int))] private int _i3; - [FieldOffset(sizeof(FrameType) + 3*sizeof(int))] + [FieldOffset(sizeof(FrameType) + 3 * sizeof(int))] private int _i4; - [FieldOffset(sizeof(FrameType) + 0*sizeof(float))] + [FieldOffset(sizeof(FrameType) + 0 * sizeof(float))] private float _f1; - [FieldOffset(sizeof(FrameType) + 1*sizeof(float))] + [FieldOffset(sizeof(FrameType) + 1 * sizeof(float))] private float _f2; - [FieldOffset(sizeof(FrameType) + 2*sizeof(float))] + [FieldOffset(sizeof(FrameType) + 2 * sizeof(float))] private float _f3; - [FieldOffset(sizeof(FrameType) + 3*sizeof(float))] + [FieldOffset(sizeof(FrameType) + 3 * sizeof(float))] private float _f4; [FieldOffset(24)] @@ -36,7 +36,7 @@ namespace Quik.CommandMachine _type == FrameType.IVec2 || _type == FrameType.IVec3 || _type == FrameType.IVec4; - public bool IsFloat => + public bool IsFloat => _type == FrameType.Vec1 || _type == FrameType.Vec2 || _type == FrameType.Vec3 || @@ -48,16 +48,16 @@ namespace Quik.CommandMachine { switch (_type) { - case FrameType.None: - return 0; - default: - return 1; - case FrameType.Vec2: case FrameType.IVec2: - return 2; - case FrameType.Vec3: case FrameType.IVec3: - return 3; - case FrameType.Vec4: case FrameType.IVec4: - return 4; + case FrameType.None: + return 0; + default: + return 1; + case FrameType.Vec2: case FrameType.IVec2: + return 2; + case FrameType.Vec3: case FrameType.IVec3: + return 3; + case FrameType.Vec4: case FrameType.IVec4: + return 4; } } } @@ -78,10 +78,11 @@ namespace Quik.CommandMachine _type = FrameType.None }; + #region Constructors public Frame(Command command) : this() { _type = FrameType.Command; - _i1 = (int)command; + _i1 = (int)command; } public Frame(object o) @@ -195,6 +196,8 @@ namespace Quik.CommandMachine _f4 = f4; } + #endregion + public T As() { return (T)_object; @@ -204,12 +207,12 @@ namespace Quik.CommandMachine { switch (i) { - case 0: return _f1; - case 1: return _f2; - case 2: return _f3; - case 3: return _f4; - default: - throw new ArgumentOutOfRangeException(); + case 0: return _f1; + case 1: return _f2; + case 2: return _f3; + case 3: return _f4; + default: + throw new ArgumentOutOfRangeException(); } } @@ -217,30 +220,118 @@ namespace Quik.CommandMachine { switch (i) { - case 0: return _i1; - case 1: return _i2; - case 2: return _i3; - case 3: return _i4; - default: - throw new ArgumentOutOfRangeException(); + case 0: return _i1; + case 1: return _i2; + case 2: return _i3; + case 3: return _i4; + default: + throw new ArgumentOutOfRangeException(); } } - 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);