2010-02-03 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mcs / class / CustomMarshalers / System.Runtime.InteropServices.CustomMarshalers / EnumeratorToEnumVariantMarshaler.cs
1 //
2 // System.Runtime.InteropServices.CustomMarshalers.EnumeratorToEnumVariantMarshaler
3 //
4 // Authors:
5 //      Martin Willemoes Hansen (mwh@sysrq.dk)
6 //      Jonathan Chambers (joncham@gmail.com)
7 //
8 // (C) 2003 Martin Willemoes Hansen
9 // (C) 2007 Jonathan Chambers
10 //
11
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 using System.Collections;
35 using System.Runtime.CompilerServices;
36 using System.Runtime.InteropServices;
37
38 namespace System.Runtime.InteropServices.CustomMarshalers
39 {
40         public class EnumeratorToEnumVariantMarshaler : ICustomMarshaler
41         {
42                 static EnumeratorToEnumVariantMarshaler instance;
43                 public void CleanUpManagedData (object pManagedObj) {
44                         throw new NotImplementedException ();
45                 }
46
47                 public void CleanUpNativeData (IntPtr pNativeData) {
48                         Marshal.Release (pNativeData);
49                 }
50
51                 public static ICustomMarshaler GetInstance (string pstrCookie) {
52                         if (instance == null)
53                                 instance = new EnumeratorToEnumVariantMarshaler ();
54                         return instance;
55                 }
56
57                 public int GetNativeDataSize () {
58                         throw new NotImplementedException ();
59                 }
60
61                 public IntPtr MarshalManagedToNative (object pManagedObj) {
62                         throw new NotImplementedException ();
63                 }
64
65                 public object MarshalNativeToManaged (IntPtr pNativeData) {
66                         IEnumVARIANT ienumvariant = (IEnumVARIANT)Marshal.GetObjectForIUnknown (pNativeData);
67                         VARIANTEnumerator e = new VARIANTEnumerator (ienumvariant);
68                         return e;
69                 }
70
71                 [ComImport]
72                 [Guid ("00020404-0000-0000-C000-000000000046")]
73                 [InterfaceType (ComInterfaceType.InterfaceIsIUnknown)]
74                 interface IEnumVARIANT
75                 {
76                         [MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
77                         void Next (int celt, [MarshalAs (UnmanagedType.Struct)]out object rgvar, out uint pceltFetched);
78                         [MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
79                         void Skip (uint celt);
80                         [MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
81                         void Reset ();
82                         [return: MarshalAs (UnmanagedType.Interface)]
83                         [MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
84                         IEnumVARIANT Clone ();
85
86                 }
87
88                 class VARIANTEnumerator : IEnumerator
89                 {
90                         IEnumVARIANT com_enum;
91                         object current;
92                         public VARIANTEnumerator (IEnumVARIANT com_enum) {
93                                 this.com_enum = com_enum;
94                         }
95                         #region IEnumerator Members
96
97                         public object Current {
98                                 get {
99                                         return current;
100                                 }
101                         }
102
103                         public bool MoveNext () {
104                                 object val;
105                                 uint fetched = 0;
106                                 com_enum.Next (1, out val, out fetched);
107                                 if (fetched == 0)
108                                         return false;
109                                 current = val;
110                                 return true;
111                         }
112
113                         public void Reset () {
114                                 com_enum.Reset ();
115                         }
116
117                         #endregion
118                 }
119         }
120 }