Hardware/Parameter.cs
author moel.mich
Sun, 15 Aug 2010 14:46:58 +0000
changeset 167 b7cc9d09aefe
parent 166 fa9dfbfc4145
child 182 4801e9eaf979
permissions -rw-r--r--
Fixed some Code Analysis warnings.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     5   The contents of this file are subject to the Mozilla Public License Version
     6   1.1 (the "License"); you may not use this file except in compliance with
     7   the License. You may obtain a copy of the License at
     8  
     9   http://www.mozilla.org/MPL/
    10 
    11   Software distributed under the License is distributed on an "AS IS" basis,
    12   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    13   for the specific language governing rights and limitations under the License.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    17   The Initial Developer of the Original Code is 
    18   Michael Möller <m.moeller@gmx.ch>.
    19   Portions created by the Initial Developer are Copyright (C) 2009-2010
    20   the Initial Developer. All Rights Reserved.
    21 
    22   Contributor(s):
    23 
    24   Alternatively, the contents of this file may be used under the terms of
    25   either the GNU General Public License Version 2 or later (the "GPL"), or
    26   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    27   in which case the provisions of the GPL or the LGPL are applicable instead
    28   of those above. If you wish to allow use of your version of this file only
    29   under the terms of either the GPL or the LGPL, and not to allow others to
    30   use your version of this file under the terms of the MPL, indicate your
    31   decision by deleting the provisions above and replace them with the notice
    32   and other provisions required by the GPL or the LGPL. If you do not delete
    33   the provisions above, a recipient may use your version of this file under
    34   the terms of any one of the MPL, the GPL or the LGPL.
    35  
    36 */
    37 
    38 using System;
    39 using System.Globalization;
    40 using System.Collections.Generic;
    41 
    42 namespace OpenHardwareMonitor.Hardware {
    43 
    44   internal struct ParameterDescription {
    45     private string name;
    46     private string description;
    47     private float defaultValue;    
    48 
    49     public ParameterDescription(string name, string description, 
    50       float defaultValue) {
    51       this.name = name;
    52       this.description = description;
    53       this.defaultValue = defaultValue;
    54     }
    55 
    56     public string Name { get { return name; } }
    57 
    58     public string Description { get { return description; } }
    59 
    60     public float DefaultValue { get { return defaultValue; } }
    61   }
    62 
    63   internal class Parameter : IParameter {
    64     private ISensor sensor;
    65     private ParameterDescription description;
    66     private float value;
    67     private bool isDefault;
    68     private ISettings settings;
    69 
    70     public Parameter(ParameterDescription description, ISensor sensor, 
    71       ISettings settings) 
    72     {
    73       this.sensor = sensor;
    74       this.description = description;
    75       this.settings = settings;
    76       this.isDefault = !settings.Contains(Identifier.ToString());
    77       this.value = description.DefaultValue;
    78       if (!this.isDefault) {
    79         if (!float.TryParse(settings.GetValue(Identifier.ToString(), "0"),
    80           NumberStyles.Float,
    81           CultureInfo.InvariantCulture,
    82           out this.value))
    83           this.value = description.DefaultValue;
    84       }
    85     }
    86 
    87     public ISensor Sensor {
    88       get {
    89         return sensor;
    90       }
    91     }
    92 
    93     public Identifier Identifier {
    94       get {
    95         return new Identifier(sensor.Identifier, "parameter",
    96           Name.Replace(" ", "").ToLowerInvariant());
    97       }
    98     }
    99 
   100     public string Name { get { return description.Name; } }
   101 
   102     public string Description { get { return description.Description; } }
   103 
   104     public float Value {
   105       get {
   106         return value;
   107       }
   108       set {
   109         this.isDefault = false;
   110         this.value = value;
   111         this.settings.SetValue(Identifier.ToString(), value.ToString(
   112           CultureInfo.InvariantCulture));
   113       }
   114     }
   115 
   116     public float DefaultValue { 
   117       get { return description.DefaultValue; } 
   118     }
   119 
   120     public bool IsDefault {
   121       get { return isDefault; }
   122       set {
   123         this.isDefault = value;
   124         if (value) {
   125           this.value = description.DefaultValue;
   126           this.settings.Remove(Identifier.ToString());
   127         }
   128       }
   129     }
   130 
   131     public void Accept(IVisitor visitor) {
   132       if (visitor == null)
   133         throw new ArgumentNullException("visitor");
   134       visitor.VisitParameter(this);
   135     }
   136 
   137     public void Traverse(IVisitor visitor) { }
   138   }
   139 }