From 9ab2edffee0b6081b1a7e4fb74349e395f24f110 Mon Sep 17 00:00:00 2001 From: "H. Utku Maden" Date: Sat, 14 Dec 2024 19:46:47 +0300 Subject: [PATCH] Change the IDrawCommand interface. --- Dashboard.Drawing/DrawCommand.cs | 19 +++++++++++++++++++ Dashboard.Drawing/DrawQueue.cs | 16 ++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/Dashboard.Drawing/DrawCommand.cs b/Dashboard.Drawing/DrawCommand.cs index 683ae5a..2aba61c 100644 --- a/Dashboard.Drawing/DrawCommand.cs +++ b/Dashboard.Drawing/DrawCommand.cs @@ -29,6 +29,8 @@ namespace Dashboard.Drawing /// The parameter array. /// The parameters object. object? GetParams(DrawQueue queue, ReadOnlySpan param); + + int WriteParams(DrawQueue queue, object? obj, Span param); } public interface IDrawCommand : IDrawCommand @@ -39,6 +41,8 @@ namespace Dashboard.Drawing /// The parameter array. /// The parameters object. new T? GetParams(DrawQueue queue, ReadOnlySpan param); + + new int WriteParams(DrawQueue queue, T? obj, Span param); } public sealed class DrawCommand : IDrawCommand @@ -57,6 +61,11 @@ namespace Dashboard.Drawing { return null; } + + public int WriteParams(DrawQueue queue, object? obj, Span param) + { + return 0; + } } public sealed class DrawCommand : IDrawCommand @@ -80,6 +89,16 @@ namespace Dashboard.Drawing return t; } + public int WriteParams(DrawQueue queue, T? obj, Span param) + { + return obj!.Serialize(queue, param); + } + + int IDrawCommand.WriteParams(DrawQueue queue, object? obj, Span param) + { + return WriteParams(queue, (T?)obj, param); + } + object? IDrawCommand.GetParams(DrawQueue queue, ReadOnlySpan param) { return GetParams(queue, param); diff --git a/Dashboard.Drawing/DrawQueue.cs b/Dashboard.Drawing/DrawQueue.cs index a7d73ef..6ceb823 100644 --- a/Dashboard.Drawing/DrawQueue.cs +++ b/Dashboard.Drawing/DrawQueue.cs @@ -186,6 +186,15 @@ namespace Dashboard.Drawing param.Serialize(Queue, bytes); Write(command, bytes); } + + public void Write(T2 command, T1 param) where T2 : IDrawCommand + { + int length = command.WriteParams(Queue, param, Span.Empty); + Span bytes = stackalloc byte[length]; + + command.WriteParams(Queue, param, bytes); + Write(command, bytes); + } } private class HashList : IReadOnlyList @@ -249,5 +258,12 @@ namespace Dashboard.Drawing /// The command to write. /// Any data associated with the command. void Write(IDrawCommand command, T param) where T : IParameterSerializer; + + /// + /// Write into the command stream. + /// + /// The command to write. + /// Any data associated with the command. + void Write(T2 command, T1 param) where T2 : IDrawCommand; } }