/********************************************************************++ Copyright (c) Microsoft Corporation. All rights reserved. --********************************************************************/ using System.Diagnostics.PerformanceData; using System.Diagnostics.CodeAnalysis; namespace System.Management.Automation.PerformanceData { /// /// A struct that encapuslates the information pertaining to a given counter /// like name,type and id. /// [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes")] public struct CounterInfo { #region Private Members #endregion #region Constructors /// /// Constructor /// public CounterInfo(int id, CounterType type, string name) { Id = id; Type = type; Name = name; } /// /// Constructor /// public CounterInfo(int id, CounterType type) { Id = id; Type = type; Name = null; } #endregion #region Properties /// /// Getter for Counter Name property /// public string Name { get; } /// /// Getter for Counter Id property. /// public int Id { get; } /// /// Getter for Counter Type property. /// [SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods")] public CounterType Type { get; } #endregion } /// /// An abstract class that forms the base class for any CounterSetRegistrar type. /// Any client that needs to register a new type of perf counter category with the /// PSPerfCountersMgr, should create an instance of CounterSetRegistrarBase's /// derived non-abstract type. /// The created instance is then passed to PSPerfCounterMgr's AddCounterSetInstance() /// method. /// /// public abstract class CounterSetRegistrarBase { #region Private Members #endregion #region Protected Members /// /// A reference to the encapsulated counter set instance. /// [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")] protected CounterSetInstanceBase _counterSetInstanceBase; /// /// Method that creates an instance of the CounterSetInstanceBase's derived type. /// This method is invoked by the PSPerfCountersMgr to retrieve the appropriate /// instance of CounterSet to register with its internal datastructure. /// protected abstract CounterSetInstanceBase CreateCounterSetInstance(); #endregion #region Constructors /// /// Constructor that creates an instance of CounterSetRegistrarBase derived type /// based on Provider Id, counterSetId, counterSetInstanceType, a collection /// with counters information and an optional counterSetName. /// [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")] protected CounterSetRegistrarBase( Guid providerId, Guid counterSetId, CounterSetInstanceType counterSetInstType, CounterInfo[] counterInfoArray, string counterSetName = null) { ProviderId = providerId; CounterSetId = counterSetId; CounterSetInstType = counterSetInstType; CounterSetName = counterSetName; if ((counterInfoArray == null) || (counterInfoArray.Length == 0)) { throw new ArgumentNullException("counterInfoArray"); } CounterInfoArray = new CounterInfo[counterInfoArray.Length]; for (int i = 0; i < counterInfoArray.Length; i++) { CounterInfoArray[i] = new CounterInfo( counterInfoArray[i].Id, counterInfoArray[i].Type, counterInfoArray[i].Name ); } this._counterSetInstanceBase = null; } /// /// Copy constructor /// protected CounterSetRegistrarBase( CounterSetRegistrarBase srcCounterSetRegistrarBase) { if (srcCounterSetRegistrarBase == null) { throw new ArgumentNullException("srcCounterSetRegistrarBase"); } ProviderId = srcCounterSetRegistrarBase.ProviderId; CounterSetId = srcCounterSetRegistrarBase.CounterSetId; CounterSetInstType = srcCounterSetRegistrarBase.CounterSetInstType; CounterSetName = srcCounterSetRegistrarBase.CounterSetName; CounterInfo[] counterInfoArrayRef = srcCounterSetRegistrarBase.CounterInfoArray; CounterInfoArray = new CounterInfo[counterInfoArrayRef.Length]; for (int i = 0; i < counterInfoArrayRef.Length; i++) { CounterInfoArray[i] = new CounterInfo( counterInfoArrayRef[i].Id, counterInfoArrayRef[i].Type, counterInfoArrayRef[i].Name); } } #endregion #region Properties /// /// Getter method for ProviderId property /// public Guid ProviderId { get; } /// /// Getter method for CounterSetId property /// public Guid CounterSetId { get; } /// /// Getter method for CounterSetName property /// public string CounterSetName { get; } /// /// Getter method for CounterSetInstanceType property /// public CounterSetInstanceType CounterSetInstType { get; } /// /// Getter method for array of counters information property /// [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")] public CounterInfo[] CounterInfoArray { get; } /// /// Getter method that returns an instance of the CounterSetInstanceBase's /// derived type /// public CounterSetInstanceBase CounterSetInstance { get { return _counterSetInstanceBase ?? (_counterSetInstanceBase = CreateCounterSetInstance()); } } #endregion #region Public Methods /// /// Method that disposes the referenced instance of the CounterSetInstanceBase's derived type. /// This method is invoked by the PSPerfCountersMgr to dispose the appropriate /// instance of CounterSet from its internal datastructure as part of PSPerfCountersMgr /// cleanup procedure. /// public abstract void DisposeCounterSetInstance(); #endregion } /// /// PSCounterSetRegistrar implements the abstract methods of CounterSetRegistrarBase. /// Any client that needs to register a new type of perf counter category with the /// PSPerfCountersMgr, should create an instance of PSCounterSetRegistrar. /// The created instance is then passed to PSPerfCounterMgr's AddCounterSetInstance() /// method. /// public class PSCounterSetRegistrar : CounterSetRegistrarBase { #region Constructors /// /// Constructor that creates an instance of PSCounterSetRegistrar. /// [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")] public PSCounterSetRegistrar( Guid providerId, Guid counterSetId, CounterSetInstanceType counterSetInstType, CounterInfo[] counterInfoArray, string counterSetName = null) : base(providerId, counterSetId, counterSetInstType, counterInfoArray, counterSetName) { } /// /// Copy Constructor /// public PSCounterSetRegistrar( PSCounterSetRegistrar srcPSCounterSetRegistrar) : base(srcPSCounterSetRegistrar) { if (srcPSCounterSetRegistrar == null) { throw new ArgumentNullException("srcPSCounterSetRegistrar"); } } #endregion #region CounterSetRegistrarBase Overrides #region Protected Methods /// /// Method that creates an instance of the CounterSetInstanceBase's derived type. /// protected override CounterSetInstanceBase CreateCounterSetInstance() { return new PSCounterSetInstance(this); } #endregion #region Public Methods /// /// Method that disposes the referenced instance of the CounterSetInstanceBase's derived type. /// public override void DisposeCounterSetInstance() { base._counterSetInstanceBase.Dispose(); } #endregion #endregion } }