Implement MachineKey.Protect and MachineKey.Unprotect
[mono.git] / mcs / class / System / System.ComponentModel / EnumConverter.cs
1 //
2 // System.ComponentModel.EnumConverter.cs
3 //
4 // Authors:
5 //   Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //   Ivan N. Zlatev (contact@i-nz.net)
8 //
9 // (C) 2002 Ximian, Inc (http://www.ximian.com)
10 // (C) 2003 Andreas Nahr
11 // (C) 2007 Ivan N. Zlatev
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 using System;
35 using System.Collections;
36 using System.Globalization;
37 using System.Reflection;
38 using System.ComponentModel.Design.Serialization;
39
40 namespace System.ComponentModel
41 {
42         public class EnumConverter : TypeConverter
43         {
44                 private Type type;
45                 private StandardValuesCollection stdValues;
46
47                 public EnumConverter (Type type)
48                 {
49                         this.type = type;
50                 }
51
52                 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
53                 {
54                         if (destinationType == typeof (InstanceDescriptor))
55                                 return true;
56
57                         if (destinationType == typeof (Enum[]))
58                                 return true;
59
60                         return base.CanConvertTo (context, destinationType);
61                 }
62
63                 public override object ConvertTo (ITypeDescriptorContext context,
64                                                   CultureInfo culture,
65                                                   object value,
66                                                   Type destinationType)
67                 {
68                         if (destinationType == typeof (string) && value != null) {
69                                 // we need to be able to convert enum names,
70                                 // integral values and other enum fields
71                                 if (value is IConvertible) {
72                                         Type underlyingType = Enum.GetUnderlyingType (type);
73                                         if (underlyingType != value.GetType ()) {
74                                                 value = ((IConvertible) value).ToType (
75                                                         underlyingType, culture);
76                                         }
77                                 }
78
79                                 if (!IsFlags && !IsValid (context, value))
80                                         throw CreateValueNotValidException (value);
81
82                                 return Enum.Format (type, value, "G");
83                         } else if (destinationType == typeof (InstanceDescriptor) && value != null) {
84                                 string enumString = ConvertToString (context, culture, value);
85                                 if (IsFlags && enumString.IndexOf (",") != -1) {
86                                         if (value is IConvertible) {
87                                                 Type underlyingType = Enum.GetUnderlyingType (type);
88                                                 object enumValue = ((IConvertible)value).ToType (underlyingType, culture);
89                                                 MethodInfo toObjectMethod = typeof (Enum).GetMethod ("ToObject", new Type[] { typeof (Type), 
90                                                                                                                                                           underlyingType });
91                                                 return new InstanceDescriptor (toObjectMethod, new object[] { type, enumValue });
92                                         }
93                                 } else {
94                                         FieldInfo f = type.GetField (enumString);
95                                         if (f != null)
96                                                 return new InstanceDescriptor (f, null);
97                                 }
98                         } else if (destinationType == typeof (Enum[]) && value != null) {
99                                 if (!IsFlags) {
100                                         return new Enum[] { (Enum) Enum.ToObject (type, value) };
101                                 } else {
102                                         long valueLong = Convert.ToInt64 (
103                                                 (Enum) value, culture);
104                                         Array enumValArray = Enum.GetValues (type);
105                                         long[] enumValues = new long[enumValArray.Length];
106                                         for (int i=0; i < enumValArray.Length; i++)
107                                                 enumValues[i] = Convert.ToInt64 (enumValArray.GetValue (i));
108
109                                         ArrayList enums = new ArrayList ();
110                                         bool interrupt = false;
111                                         while (!interrupt) {
112                                                 // interrupt the loop unless we find a match to avoid
113                                                 // looping indefinitely
114                                                 interrupt = true;
115                                                 foreach (long val in enumValues) {
116                                                         if ((val != 0 && ((val & valueLong) == val)) || val == valueLong) {
117                                                                 enums.Add (Enum.ToObject (type, val));
118                                                                 valueLong &= (~val);
119                                                                 interrupt = false;
120                                                         }
121                                                 }
122                                                 if (valueLong == 0) // nothing left to do
123                                                         interrupt = true;
124                                         }
125
126                                         // add item for remainder
127                                         if (valueLong != 0)
128                                                 enums.Add (Enum.ToObject (type, valueLong));
129
130                                         return enums.ToArray (typeof(Enum));
131                                 }
132                         }
133                         
134                         return base.ConvertTo (context, culture, value, destinationType);
135                 }
136
137                 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
138                 {
139                         if (sourceType == typeof (string))
140                                 return true;
141
142                         if (sourceType == typeof (Enum[]))
143                                 return true;
144
145                         return base.CanConvertFrom (context, sourceType);
146                 }
147
148                 public override object ConvertFrom (ITypeDescriptorContext context,
149                                                     CultureInfo culture,
150                                                     object value)
151                 {
152                         if (value is string) {
153                                 string name = value as string;
154                                 try {
155                                         if (name.IndexOf (',') == -1)
156                                                 return Enum.Parse (type, name, true);
157
158                                         long val = 0;
159                                         string [] names = name.Split (',');
160                                         foreach (string n in names) {
161                                                 Enum e = (Enum) Enum.Parse (type, n, true);
162                                                 val |= Convert.ToInt64 (e, culture);
163                                         }
164                                         return Enum.ToObject (type, val);
165                                 } catch (Exception ex) {
166                                         throw new FormatException (name + " is " +
167                                                 "not a valid value for " +
168                                                 type.Name, ex);
169                                 }
170                         } else if (value is Enum[]) {
171                                 long val = 0;
172                                 foreach (Enum e in (Enum[])value)
173                                         val |= Convert.ToInt64 (e, culture);
174                                 return Enum.ToObject (type, val);
175                         }
176
177                         return base.ConvertFrom (context, culture, value);
178                 }
179
180                 public override bool IsValid (ITypeDescriptorContext context, object value)
181                 {
182                         return Enum.IsDefined (type, value);
183                 }
184
185                 public override bool GetStandardValuesSupported (ITypeDescriptorContext context)
186                 {
187                         return true;
188                 }
189
190                 public override bool GetStandardValuesExclusive (ITypeDescriptorContext context)
191                 {
192                         return !IsFlags;
193                 }
194
195                 public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
196                 {
197                         if (stdValues == null) {
198                                 Array values = Enum.GetValues (type);
199                                 Array.Sort (values);
200                                 stdValues = new StandardValuesCollection (values);
201                         }
202                         return stdValues;
203                 }
204
205                 protected virtual IComparer Comparer {
206                         get { return new EnumConverter.EnumComparer (); }
207                 }
208
209                 protected Type EnumType {
210                         get { return type; }
211                 }
212
213                 protected TypeConverter.StandardValuesCollection Values {
214                         get { return stdValues;  }
215                         set { stdValues = value; }
216                 }
217
218                 ArgumentException CreateValueNotValidException (object value)
219                 {
220                         string msg = string.Format (CultureInfo.InvariantCulture,
221                                 "The value '{0}' is not a valid value for the " +
222                                 "enum '{1}'", value, type.Name);
223                         return new ArgumentException (msg);
224                 }
225
226                 bool IsFlags {
227                         get {
228                                 return (type.IsDefined (typeof (FlagsAttribute),
229                                         false));
230                         }
231                 }
232
233                 private class EnumComparer : IComparer
234                 {
235                         int IComparer.Compare (object compareObject1, object compareObject2) 
236                         {
237                                 string CompareString1 = (compareObject1 as string);
238                                 string CompareString2 = (compareObject2 as string);
239                                 if ((CompareString1 == null) || (CompareString2 == null))
240                                         return Collections.Comparer.Default.Compare (compareObject1, compareObject2);
241                                 return CultureInfo.InvariantCulture.CompareInfo.Compare (CompareString1, CompareString2);
242                         }
243                 }
244         }
245 }