Files
PowerShell/src/System.Management.Automation/utils/ObjectWriter.cs
T
xtqqczze 9212aac0fa Use nameof operator (#12716)
# PR Summary

Using *Roslynator Command Line Tool version 0.1.0.4*

* Fix RCS1015:
  * `"argument"` → `nameof(argument)`
  * `enum.ToString()` → `nameof(enum)`

[RCS1015.log](https://github.com/PowerShell/PowerShell/files/4646102/RCS1015.log)


## PR Context

<!-- Provide a little reasoning as to why this Pull Request helps and why you have opened it. -->

## PR Checklist

- [x] [PR has a meaningful title](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission)
    - Use the present tense and imperative mood when describing your changes
- [x] [Summarized changes](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission)
- [x] [Make sure all `.h`, `.cpp`, `.cs`, `.ps1` and `.psm1` files have the correct copyright header](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission)
- [x] This PR is ready to merge and is not [Work in Progress](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---work-in-progress).
    - If the PR is work in progress, please add the prefix `WIP:` or `[ WIP ]` to the beginning of the title (the `WIP` bot will keep its status check at `Pending` while the prefix is present) and remove the prefix when the PR is ready.
- **[Breaking changes](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#making-breaking-changes)**
    - [x] None
    - **OR**
    - [ ] [Experimental feature(s) needed](https://github.com/MicrosoftDocs/PowerShell-Docs/blob/staging/reference/6/Microsoft.PowerShell.Core/About/about_Experimental_Features.md)
        - [ ] Experimental feature name(s): <!-- Experimental feature name(s) here -->
- **User-facing changes**
    - [x] Not Applicable
    - **OR**
    - [ ] [Documentation needed](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission)
        - [ ] Issue filed: <!-- Number/link of that issue here -->
- **Testing - New and feature**
    - [x] N/A or can only be tested interactively
    - **OR**
    - [ ] [Make sure you've added a new test if existing tests do not effectively test the code changed](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#before-submitting)
- **Tooling**
    - [x] I have considered the user experience from a tooling perspective and don't believe tooling will be impacted.
    - **OR**
    - [ ] I have considered the user experience from a tooling perspective and enumerated concerns in the summary. This may include:
        - Impact on [PowerShell Editor Services](https://github.com/PowerShell/PowerShellEditorServices) which is used in the [PowerShell extension](https://github.com/PowerShell/vscode-powershell) for VSCode (which runs in a different PS Host).
        - Impact on Completions (both in the console and in editors) - one of PowerShell's most powerful features.
        - Impact on [PSScriptAnalyzer](https://github.com/PowerShell/PSScriptAnalyzer) (which provides linting & formatting in the editor extensions).
        - Impact on [EditorSyntax](https://github.com/PowerShell/EditorSyntax) (which provides syntax highlighting with in VSCode, GitHub, and many other editors).
2020-05-19 18:57:52 +00:00

236 lines
7.4 KiB
C#

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
namespace System.Management.Automation.Internal
{
using System;
using System.Threading;
using System.Runtime.InteropServices;
using System.Management.Automation.Runspaces;
/// <summary>
/// A ObjectWriter for an ObjectStream.
/// </summary>
/// <remarks>
/// This class is not safe for multi-threaded operations.
/// </remarks>
internal class ObjectWriter : PipelineWriter
{
/// <summary>
/// Construct with an existing ObjectStream.
/// </summary>
/// <param name="stream">The stream to write.</param>
/// <exception cref="ArgumentNullException">Thrown if the specified stream is null.</exception>
public ObjectWriter([In, Out] ObjectStreamBase stream)
{
if (stream == null)
{
throw new ArgumentNullException(nameof(stream));
}
_stream = stream;
#if (false)
stream.WriteReady += new EventHandler (this.OnWriteReady);
#endif
}
#region Properties
/// <summary>
/// Waitable handle for caller's to block until buffer space is available in the underlying stream.
/// </summary>
public override WaitHandle WaitHandle
{
get
{
return _stream.WriteHandle;
}
}
/// <summary>
/// Check if the stream is open for further writes.
/// </summary>
/// <value>true if the underlying stream is open, otherwise; false.</value>
/// <remarks>
/// Attempting to write to the underlying stream if IsOpen is false throws
/// a <see cref="PipelineClosedException"/>.
/// </remarks>
public override bool IsOpen
{
get
{
return _stream.IsOpen;
}
}
/// <summary>
/// Returns the number of objects in the underlying stream.
/// </summary>
public override int Count
{
get
{
return _stream.Count;
}
}
/// <summary>
/// Get the capacity of the stream.
/// </summary>
/// <value>
/// The capacity of the stream.
/// </value>
/// <remarks>
/// The capacity is the number of objects that stream may contain at one time. Once this
/// limit is reached, attempts to write into the stream block until buffer space
/// becomes available.
/// </remarks>
public override int MaxCapacity
{
get
{
return _stream.MaxCapacity;
}
}
#endregion Properties
#region Methods
/// <summary>
/// Close the stream.
/// </summary>
/// <remarks>
/// Causes subsequent calls to IsOpen to return false and calls to
/// a write operation to throw an ObjectDisposedException.
/// All calls to Close() after the first call are silently ignored.
/// </remarks>
/// <exception cref="ObjectDisposedException">
/// The stream is already disposed
/// </exception>
public override void Close()
{
_stream.Close();
// 2003/09/02-JonN I removed setting _stream
// to null, now all of the tests for null can come out.
}
/// <summary>
/// Flush the data from the stream. Closed streams may be flushed,
/// but disposed streams may not.
/// </summary>
/// <exception cref="ObjectDisposedException">
/// The underlying stream is disposed
/// </exception>
public override void Flush()
{
_stream.Flush();
}
/// <summary>
/// Write a single object into the underlying stream.
/// </summary>
/// <param name="obj">The object to add to the stream.</param>
/// <returns>
/// One, if the write was successful, otherwise;
/// zero if the stream was closed before the object could be written,
/// or if the object was AutomationNull.Value.
/// </returns>
/// <exception cref="PipelineClosedException">
/// The underlying stream is closed
/// </exception>
/// <remarks>
/// AutomationNull.Value is ignored
/// </remarks>
public override int Write(object obj)
{
return _stream.Write(obj);
}
/// <summary>
/// Write objects to the underlying stream.
/// </summary>
/// <param name="obj">Object or enumeration to read from.</param>
/// <param name="enumerateCollection">
/// If enumerateCollection is true, and <paramref name="obj"/>
/// is an enumeration according to LanguagePrimitives.GetEnumerable,
/// the objects in the enumeration will be unrolled and
/// written separately. Otherwise, <paramref name="obj"/>
/// will be written as a single object.
/// </param>
/// <returns>The number of objects written.</returns>
/// <exception cref="PipelineClosedException">
/// The underlying stream is closed
/// </exception>
/// <remarks>
/// If the enumeration contains elements equal to
/// AutomationNull.Value, they are are ignored.
/// This can cause the return value to be less than the size of
/// the collection.
/// </remarks>
public override int Write(object obj, bool enumerateCollection)
{
return _stream.Write(obj, enumerateCollection);
}
#if (false)
/// <summary>
/// Handle WriteReady events from the underlying stream.
/// </summary>
/// <param name="sender">The stream raising the event.</param>
/// <param name="args">Standard event args.</param>
private void OnWriteReady (object sender, EventArgs args)
{
if (WriteReady != null)
{
// call any event handlers on this, replacing the
// ObjectStream sender with 'this' since receivers
// are expecting an PipelineWriter
WriteReady (this, args);
}
}
#endif
#endregion Methods
#region Private fields
/// <summary>
/// The underlying stream.
/// </summary>
/// <remarks>Can never be null</remarks>
private ObjectStreamBase _stream;
#endregion Private Fields
}
/// <summary>
/// A ObjectWriter for a PSDataCollection ObjectStream.
/// </summary>
/// <remarks>
/// PSDataCollection is introduced after 1.0. PSDataCollection
/// is used to store data from the last command in
/// the pipeline and hence the writer will not
/// support certain features like Flush().
/// </remarks>
internal class PSDataCollectionWriter<T> : ObjectWriter
{
#region Constructors
/// <summary>
/// Construct with an existing PSDataCollectionStream.
/// </summary>
/// <param name="stream">The stream to write.</param>
/// <exception cref="ArgumentNullException">
/// Thrown if the specified stream is null
/// </exception>
public PSDataCollectionWriter(PSDataCollectionStream<T> stream)
: base(stream)
{
}
#endregion
}
}