[System] Process.WaitForExit now triggers event Exited.
[mono.git] / mcs / class / corlib / System.Runtime.Versioning / VersioningHelper.cs
1 //
2 // System.Runtime.Versioning.VersioningHelper class
3 //
4 // Authors
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 namespace System.Runtime.Versioning {
30
31         public static class VersioningHelper {
32
33                 static private int GetDomainId ()
34                 {
35 #if NET_2_1
36                         return 0;
37 #else
38                         return AppDomain.CurrentDomain.Id;
39 #endif
40                 }
41
42                 static private int GetProcessId ()
43                 {
44                         // TODO - System.Diagnostics.Process class is located in System.dll
45                         return 0;
46                 }
47
48                 static string SafeName (string name, bool process, bool appdomain)
49                 {
50                         if (process && appdomain) {
51                                 return String.Concat (name, "_", GetProcessId ().ToString (),
52                                         "_", GetDomainId ().ToString ());
53                         } else if (process) {
54                                 return String.Concat (name, "_", GetProcessId ().ToString ());
55                         } else if (appdomain) {
56                                 return String.Concat (name, "_", GetDomainId ().ToString ());
57                         }
58                         // nothing, return original string
59                         return name;
60                 }
61
62                 static private string ConvertFromMachine (string name, ResourceScope to, Type type)
63                 {
64                         switch (to) {
65                         case ResourceScope.Machine:
66                                 return SafeName (name, false, false);
67                         case ResourceScope.Process:
68                                 return SafeName (name, true, false);
69                         case ResourceScope.AppDomain:
70                                 return SafeName (name, true, true);
71                         default:
72                                 throw new ArgumentException ("to");
73                         }
74                 }
75
76                 static private string ConvertFromProcess (string name, ResourceScope to, Type type)
77                 {
78                         if ((to < ResourceScope.Process) || (to >= ResourceScope.Private))
79                                 throw new ArgumentException ("to");
80                         bool ad = ((to & ResourceScope.AppDomain) == ResourceScope.AppDomain);
81                         return SafeName (name, false, ad);
82                 }
83
84                 static private string ConvertFromAppDomain (string name, ResourceScope to, Type type)
85                 {
86                         if ((to < ResourceScope.AppDomain) || (to >= ResourceScope.Private))
87                                 throw new ArgumentException ("to");
88                         return SafeName (name, false, false);
89                 }
90
91                 [MonoTODO ("process id is always 0")]
92                 static public string MakeVersionSafeName (string name, ResourceScope from, ResourceScope to)
93                 {
94                         return MakeVersionSafeName (name, from, to, null);
95                 }
96
97                 [MonoTODO ("type?")]
98                 static public string MakeVersionSafeName (string name, ResourceScope from, ResourceScope to, Type type)
99                 {
100                         if ((from & ResourceScope.Private) != 0) {
101                                 to &= ~(ResourceScope.Private | ResourceScope.Assembly);
102                         } else if ((from & ResourceScope.Assembly) != 0) {
103                                 to &= ~ResourceScope.Assembly;
104                         }
105                         
106                         string result = (name == null) ? String.Empty : name;
107                         switch (from) {
108                         case ResourceScope.Machine:
109                         case ResourceScope.Machine | ResourceScope.Private:
110                         case ResourceScope.Machine | ResourceScope.Assembly:
111                                 return ConvertFromMachine (result, to, type);
112                         case ResourceScope.Process:
113                         case ResourceScope.Process | ResourceScope.Private:
114                         case ResourceScope.Process | ResourceScope.Assembly:
115                                 return ConvertFromProcess (result, to, type);
116                         case ResourceScope.AppDomain:
117                         case ResourceScope.AppDomain | ResourceScope.Private:
118                         case ResourceScope.AppDomain | ResourceScope.Assembly:
119                                 return ConvertFromAppDomain (result, to, type);
120                         default:
121                                 throw new ArgumentException ("from");
122                         }
123                 }
124         }
125 }