Converted source files to UTF-8 (without byte order mark). Use UTF-8 as the default...
[mono.git] / mcs / class / System / System.Diagnostics / Switch.cs
1 //
2 // System.Diagnostics.Switch.cs
3 //
4 // Comments from John R. Hicks <angryjohn69@nc.rr.com> original implementation 
5 // can be found at: /mcs/docs/apidocs/xml/en/System.Diagnostics
6 //
7 // Author:
8 //      John R. Hicks  (angryjohn69@nc.rr.com)
9 //      Jonathan Pryor (jonpryor@vt.edu)
10 //
11 // (C) 2001-2002
12 //
13
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System.Collections;
36
37 namespace System.Diagnostics
38 {
39         public abstract class Switch
40         {
41                 private string name = "";
42                 private string description = "";
43                 private int switchSetting = 0;
44
45                 // MS Behavior is that (quoting from MSDN for OnSwitchSettingChanged()):
46                 //              "...It is invoked the first time a switch reads its value from the
47                 //              configuration file..."
48                 // The docs + testing implies two things:
49                 //      1. The value of the switch is not read in from the constructor
50                 //      2. The value is instead read in on the first time get_SwitchSetting is
51                 //              invoked
52                 // Assuming that OnSwitchSettingChanged() is invoked on a .config file
53                 // read and on all changes
54                 //
55                 // Thus, we need to keep track of whether or not switchSetting has been
56                 // initialized.  Using `switchSetting=-1' seems logical, but if someone
57                 // actually wants to use -1 as a switch value that would cause problems.
58                 private bool initialized = false;
59
60                 protected Switch(string displayName, string description)
61                 {
62                         this.name = displayName;
63                         this.description = description;
64                 }
65
66                 public string Description {
67                         get {return description;}
68                 }
69
70                 public string DisplayName {
71                         get {return name;}
72                 }
73
74                 protected int SwitchSetting {
75                         get {
76                                 if (!initialized) {
77                                         initialized = true;
78                                         GetConfigFileSetting ();
79                                         OnSwitchSettingChanged ();
80                                 }
81                                 return switchSetting;
82                         }
83                         set {
84                                 if(switchSetting != value) {
85                                         switchSetting = value;
86                                         OnSwitchSettingChanged();
87                                 }
88                                 initialized = true;
89                         }
90                 }
91
92                 private void GetConfigFileSetting ()
93                 {
94                         try {
95                                 IDictionary d = (IDictionary) DiagnosticsConfiguration.Settings ["switches"];
96                                 
97                                 // Load up the specified switch
98                                 if (d != null)
99                                         switchSetting = int.Parse (d [name].ToString());
100                         } catch {
101                                 switchSetting = 0;
102                         }
103                 }
104
105                 protected virtual void OnSwitchSettingChanged()
106                 {
107                         // Do nothing.  This is merely provided for derived classes to know when
108                         // the value of SwitchSetting has changed.
109                 }
110         }
111 }
112