Fix null sessions in HttpContextWrapper.Session
[mono.git] / mcs / class / corlib / System / Variant.cs
1 //
2 // System.Variant
3 //
4 // Authors:
5 //   Jonathan Chambers <jonathan.chambers@ansys.com>
6 //
7 // Copyright (C) 2006 Novell (http://www.novell.com)
8 //
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Runtime.InteropServices;
31
32 namespace System
33 {
34         [StructLayout(LayoutKind.Explicit)]
35         internal unsafe struct Variant
36         {
37                 [FieldOffset(0)]
38                 public short vt;
39
40                 [FieldOffset(2)]
41                 public ushort wReserved1;
42
43                 [FieldOffset(4)]
44                 public ushort wReserved2;
45
46                 [FieldOffset(6)]
47                 public ushort wReserved3;
48
49                 [FieldOffset(8)]
50                 public long llVal;
51
52                 [FieldOffset(8)]
53                 public int lVal;
54
55                 [FieldOffset(8)]
56                 public byte bVal;
57
58                 [FieldOffset(8)]
59                 public short iVal;
60
61                 [FieldOffset(8)]
62                 public float fltVal;
63
64                 [FieldOffset(8)]
65                 public double dblVal;
66
67                 [FieldOffset(8)]
68                 public short boolVal;
69
70                 [FieldOffset(8)]
71                 public IntPtr bstrVal;
72
73                 [FieldOffset(8)]
74                 public sbyte cVal;
75
76                 [FieldOffset(8)]
77                 public ushort uiVal;
78
79                 [FieldOffset(8)]
80                 public uint ulVal;
81
82                 [FieldOffset(8)]
83                 public ulong ullVal;
84
85                 [FieldOffset(8)]
86                 public int intVal;
87
88                 [FieldOffset(8)]
89                 public uint uintVal;
90
91                 [FieldOffset(8)]
92                 public IntPtr pdispVal;
93
94                 [FieldOffset(8)]
95                 public BRECORD bRecord;
96
97                 public void SetValue(object obj) {
98                         vt = (short)VarEnum.VT_EMPTY;
99                         if (obj == null)
100                                 return;
101
102                         Type t = obj.GetType();
103                         if (t.IsEnum)
104                                 t = Enum.GetUnderlyingType (t);
105
106                         if (t == typeof(sbyte))
107                         {
108                                 vt = (short)VarEnum.VT_I1;
109                                 cVal = (sbyte)obj;
110                         }
111                         else if (t == typeof(byte))
112                         {
113                                 vt = (short)VarEnum.VT_UI1;
114                                 bVal = (byte)obj;
115                         }
116                         else if (t == typeof(short))
117                         {
118                                 vt = (short)VarEnum.VT_I2;
119                                 iVal = (short)obj;
120                         }
121                         else if (t == typeof(ushort))
122                         {
123                                 vt = (short)VarEnum.VT_UI2;
124                                 uiVal = (ushort)obj;
125                         }
126                         else if (t == typeof(int))
127                         {
128                                 vt = (short)VarEnum.VT_I4;
129                                 lVal = (int)obj;
130                         }
131                         else if (t == typeof(uint))
132                         {
133                                 vt = (short)VarEnum.VT_UI4;
134                                 ulVal = (uint)obj;
135                         }
136                         else if (t == typeof(long))
137                         {
138                                 vt = (short)VarEnum.VT_I8;
139                                 llVal = (long)obj;
140                         }
141                         else if (t == typeof(ulong))
142                         {
143                                 vt = (short)VarEnum.VT_UI8;
144                                 ullVal = (ulong)obj;
145                         }
146                         else if (t == typeof(float))
147                         {
148                                 vt = (short)VarEnum.VT_R4;
149                                 fltVal = (float)obj;
150                         }
151                         else if (t == typeof(double))
152                         {
153                                 vt = (short)VarEnum.VT_R8;
154                                 dblVal = (double)obj;
155                         }
156                         else if (t == typeof(string))
157                         {
158                                 vt = (short)VarEnum.VT_BSTR;
159                                 bstrVal = Marshal.StringToBSTR((string)obj);
160                         }
161                         else if (t == typeof(bool))
162                         {
163                                 vt = (short)VarEnum.VT_BOOL;
164                                 lVal = ((bool)obj) ? -1 : 0;
165                         }
166                         else if (t == typeof (BStrWrapper))
167                         {
168                                 vt = (short)VarEnum.VT_BSTR;
169                                 bstrVal = Marshal.StringToBSTR(((BStrWrapper)obj).WrappedObject);
170                         }
171                         else if (t == typeof (UnknownWrapper))
172                         {
173                                 vt = (short)VarEnum.VT_UNKNOWN;
174                                 pdispVal = Marshal.GetIUnknownForObject(((UnknownWrapper)obj).WrappedObject);
175                         }
176                         else if (t == typeof (DispatchWrapper))
177                         {
178                                 vt = (short)VarEnum.VT_DISPATCH;
179                                 pdispVal = Marshal.GetIDispatchForObject(((DispatchWrapper)obj).WrappedObject);
180                         }
181                         else
182                         {
183                                 try 
184                                 {
185                                         pdispVal = Marshal.GetIDispatchForObject(obj);
186                                         vt = (short)VarEnum.VT_DISPATCH;
187                                         return;
188                                 }
189                                 catch { }
190                                 try 
191                                 {
192                                         vt = (short)VarEnum.VT_UNKNOWN;
193                                         pdispVal = Marshal.GetIUnknownForObject(obj);
194                                 }
195                                 catch (Exception ex)
196                                 {
197                                         throw new NotImplementedException(string.Format("Variant couldn't handle object of type {0}", obj.GetType()), ex);
198                                 }
199                         }
200                 }
201
202                 public object GetValue() {
203                         object obj = null;
204                         switch ((VarEnum)vt)
205                         {
206                         case VarEnum.VT_I1:
207                         obj = cVal;
208                         break;
209                         case VarEnum.VT_UI1:
210                                 obj = bVal;
211                                 break;
212                         case VarEnum.VT_I2:
213                                 obj = iVal;
214                                 break;
215                         case VarEnum.VT_UI2:
216                                 obj = uiVal;
217                                 break;
218                         case VarEnum.VT_I4:
219                                 obj = lVal;
220                                 break;
221                         case VarEnum.VT_UI4:
222                                 obj = ulVal;
223                                 break;
224                         case VarEnum.VT_I8:
225                                 obj = llVal;
226                                 break;
227                         case VarEnum.VT_UI8:
228                                 obj = ullVal;
229                                 break;
230                         case VarEnum.VT_R4:
231                                 obj = fltVal;
232                                 break;
233                         case VarEnum.VT_R8:
234                                 obj = dblVal;
235                                 break;
236                         case VarEnum.VT_BOOL:
237                                 obj = !(boolVal == 0);
238                                 break;
239                         case VarEnum.VT_BSTR:
240                                 obj = Marshal.PtrToStringBSTR(bstrVal);
241                                 break;
242                         case VarEnum.VT_UNKNOWN:
243                         case VarEnum.VT_DISPATCH:
244                                 if (pdispVal != IntPtr.Zero)
245                                         obj = Marshal.GetObjectForIUnknown(pdispVal);
246                                 break;
247                         }
248                         return obj;
249                 }
250
251                 public void Clear ()
252                 {
253                         if ((VarEnum)vt == VarEnum.VT_BSTR) {
254                                 Marshal.FreeBSTR (bstrVal);
255                         }
256                         else if ((VarEnum)vt == VarEnum.VT_DISPATCH || (VarEnum)vt == VarEnum.VT_UNKNOWN) {
257                                 if (pdispVal != IntPtr.Zero)
258                                         Marshal.Release (pdispVal);
259                         }
260                 }
261         }
262
263         [StructLayout(LayoutKind.Sequential)]
264         internal unsafe struct BRECORD
265         {
266         #pragma warning disable 169
267                 IntPtr pvRecord;
268                 IntPtr pRecInfo;
269         #pragma warning restore 169
270         }
271 }