From 2eccbc05780f7e6cfadc70f44021b344473c483a Mon Sep 17 00:00:00 2001 From: glopesdev Date: Mon, 22 May 2023 09:36:44 +0100 Subject: [PATCH 1/2] Allow derived classes to specify base path --- Bonsai.System/Bonsai.System.csproj | 2 +- Bonsai.System/IO/StreamSink.cs | 39 +++++++++++++++++++++++++----- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/Bonsai.System/Bonsai.System.csproj b/Bonsai.System/Bonsai.System.csproj index c31aaf5b5..d885cef0f 100644 --- a/Bonsai.System/Bonsai.System.csproj +++ b/Bonsai.System/Bonsai.System.csproj @@ -5,7 +5,7 @@ Bonsai System Library containing reactive infrastructure to interface with the underlying operating system. Bonsai Rx Reactive Extensions IO Serial Port Resources net462;netstandard2.0 - 2.7.1 + 2.8.0 diff --git a/Bonsai.System/IO/StreamSink.cs b/Bonsai.System/IO/StreamSink.cs index 035325aaa..04689a5b2 100644 --- a/Bonsai.System/IO/StreamSink.cs +++ b/Bonsai.System/IO/StreamSink.cs @@ -10,8 +10,8 @@ namespace Bonsai.IO { /// - /// Provides a non-generic base class for sinks that write the elements from the input sequence - /// into a named stream (e.g. a named pipe). + /// Provides a non-generic base class for sinks that write all elements from the + /// source sequence to a named stream. /// [Combinator] [DefaultProperty(nameof(Path))] @@ -19,7 +19,7 @@ namespace Bonsai.IO public abstract class StreamSink { /// - /// Gets or sets the identifier of the named stream on which to write the elements. + /// Gets or sets the identifier of the stream on which to write the elements. /// /// /// If the identifier uses the named pipe prefix \\.\pipe\, a corresponding @@ -94,9 +94,9 @@ static Stream CreateStream(string path, bool overwrite, CancellationToken cancel } /// - /// Writes all elements of an observable sequence to the specified stream - /// using the specified selector function. + /// Writes all elements of an observable sequence to a stream. /// + /// The type of the elements in the source sequence. /// The sequence of elements to write. /// /// The transform function used to convert each element of the sequence into the type @@ -106,7 +106,35 @@ static Stream CreateStream(string path, bool overwrite, CancellationToken cancel /// An observable sequence that is identical to the source sequence but where /// there is an additional side effect of writing the elements to a stream. /// + /// + /// A valid path must be specified. protected IObservable Process(IObservable source, Func selector) + { + return Process(source, selector, Path); + } + + /// + /// Writes all elements of an observable sequence into the specified stream. + /// + /// The type of the elements in the source sequence. + /// The sequence of elements to write. + /// + /// The transform function used to convert each element of the sequence into the type + /// of inputs accepted by the stream writer. + /// + /// + /// The identifier of the stream on which to write the elements. + /// + /// + /// An observable sequence that is identical to the source sequence but where + /// there is an additional side effect of writing the elements to the named stream. + /// + /// + /// A valid path must be specified. + protected IObservable Process( + IObservable source, + Func selector, + string path) { if (source == null) { @@ -120,7 +148,6 @@ protected IObservable Process(IObservable source, return Observable.Create(observer => { - var path = Path; if (string.IsNullOrEmpty(path)) { throw new InvalidOperationException("A valid path must be specified."); From 9990af2d7d0792e8862555a570fd0d7e116c776e Mon Sep 17 00:00:00 2001 From: glopesdev Date: Mon, 22 May 2023 09:38:37 +0100 Subject: [PATCH 2/2] Allow derived classes to specify base file name --- Bonsai.System/IO/FileSink.cs | 44 ++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/Bonsai.System/IO/FileSink.cs b/Bonsai.System/IO/FileSink.cs index a56d355dd..25dd0f392 100644 --- a/Bonsai.System/IO/FileSink.cs +++ b/Bonsai.System/IO/FileSink.cs @@ -7,8 +7,8 @@ namespace Bonsai.IO { /// - /// Provides a non-generic base class for sinks that write the elements from the input sequence - /// into a file. + /// Provides a non-generic base class for sinks that write all elements from the + /// source sequence to a file. /// [Combinator] [DefaultProperty(nameof(FileName))] @@ -77,9 +77,10 @@ public abstract class FileSink : FileSink where TWriter : clas protected abstract void Write(TWriter writer, TSource input); /// - /// Writes all elements of an observable sequence into the specified file. + /// Writes all elements of an observable sequence to a file. /// - /// The source sequence for which to write elements. + /// The type of the elements in the source sequence. + /// The sequence of elements to write to the file. /// /// The transform function used to convert each element of the sequence into the type /// of inputs accepted by the file writer. @@ -88,16 +89,42 @@ public abstract class FileSink : FileSink where TWriter : clas /// An observable sequence that is identical to the sequence /// but where there is an additional side effect of writing the elements to a file. /// + /// protected IObservable Process(IObservable source, Func selector) + { + return Process(source, selector, FileName); + } + + /// + /// Writes all elements of an observable sequence to the specified file. + /// + /// The type of the elements in the source sequence. + /// The sequence of elements to write to the file. + /// + /// The transform function used to convert each element of the sequence into the type + /// of inputs accepted by the file writer. + /// + /// + /// The name of the file on which to write the elements. + /// + /// + /// An observable sequence that is identical to the sequence + /// but where there is an additional side effect of writing the elements to a file. + /// + /// + protected IObservable Process( + IObservable source, + Func selector, + string fileName) { if (source == null) { - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); } if (selector == null) { - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); } return Observable.Create(observer => @@ -113,7 +140,6 @@ protected IObservable Process(IObservable source, var runningWriter = disposable.Writer; if (runningWriter == null) { - var fileName = FileName; if (string.IsNullOrEmpty(fileName)) { throw new InvalidOperationException("A valid file path must be specified."); @@ -145,9 +171,9 @@ protected IObservable Process(IObservable source, } /// - /// Writes all elements of an observable sequence into the specified file. + /// Writes all elements of an observable sequence to the specified file. /// - /// The source sequence for which to write elements. + /// The sequence of elements to write to the file. /// /// An observable sequence that is identical to the sequence /// but where there is an additional side effect of writing the elements to a file.