Merge pull request #487 from mayerwin/patch-1
[mono.git] / mcs / class / corlib / System.Reflection.Emit / UnmanagedMarshal.cs
1
2 //
3 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
12 // 
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 // 
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 //
24
25 //
26 // System.Reflection.Emit/UnmanagedMarshal.cs
27 //
28 // Author:
29 //   Paolo Molaro (lupus@ximian.com)
30 //
31 // (C) 2001-2002 Ximian, Inc.  http://www.ximian.com
32 //
33
34 #if !FULL_AOT_RUNTIME
35 using System.Reflection.Emit;
36 using System.Runtime.InteropServices;
37 using System;
38
39 namespace System.Reflection.Emit {
40
41         [Obsolete ("An alternate API is available: Emit the MarshalAs custom attribute instead.")]
42         [ComVisible (true)]
43         [Serializable]
44         [StructLayout (LayoutKind.Sequential)]
45         public sealed class UnmanagedMarshal {
46 #pragma warning disable 169, 414
47                 private int count;
48                 private UnmanagedType t;
49                 private UnmanagedType tbase;
50                 string guid;
51                 string mcookie;
52                 string marshaltype;
53                 Type marshaltyperef;
54                 private int param_num;
55                 private bool has_size;
56 #pragma warning restore 169, 414
57                 
58                 private UnmanagedMarshal (UnmanagedType maint, int cnt) {
59                         count = cnt;
60                         t = maint;
61                         tbase = maint;
62                 }
63                 private UnmanagedMarshal (UnmanagedType maint, UnmanagedType elemt) {
64                         count = 0;
65                         t = maint;
66                         tbase = elemt;
67                 }
68                 
69                 public UnmanagedType BaseType {
70                         get {
71                                 if (t == UnmanagedType.LPArray || t == UnmanagedType.SafeArray)
72                                         throw new ArgumentException ();
73                                 return tbase;
74                         }
75                 }
76
77                 public int ElementCount {
78                         get {return count;}
79                 }
80
81                 public UnmanagedType GetUnmanagedType {
82                         get {return t;}
83                 }
84
85                 public Guid IIDGuid {
86                         get {return new Guid (guid);}
87                 }
88
89                 public static UnmanagedMarshal DefineByValArray( int elemCount) {
90                         return new UnmanagedMarshal (UnmanagedType.ByValArray, elemCount);
91                 }
92
93                 public static UnmanagedMarshal DefineByValTStr( int elemCount) {
94                         return new UnmanagedMarshal (UnmanagedType.ByValTStr, elemCount);
95                 }
96
97                 public static UnmanagedMarshal DefineLPArray( UnmanagedType elemType) {
98                         return new UnmanagedMarshal (UnmanagedType.LPArray, elemType);
99                 }
100
101                 public static UnmanagedMarshal DefineSafeArray( UnmanagedType elemType) {
102                         return new UnmanagedMarshal (UnmanagedType.SafeArray, elemType);
103                 }
104
105                 public static UnmanagedMarshal DefineUnmanagedMarshal( UnmanagedType unmanagedType) {
106                         return new UnmanagedMarshal (unmanagedType, unmanagedType);
107                 }
108
109                 internal static UnmanagedMarshal DefineCustom (Type typeref, string cookie, string mtype, Guid id) {
110                         UnmanagedMarshal res = new UnmanagedMarshal (UnmanagedType.CustomMarshaler, UnmanagedType.CustomMarshaler);
111                         res.mcookie = cookie;
112                         res.marshaltype = mtype;
113                         res.marshaltyperef = typeref;
114                         if (id == Guid.Empty)
115                                 res.guid = String.Empty;
116                         else
117                                 res.guid = id.ToString ();
118                         return res;
119                 }
120                 
121                 // sizeConst and sizeParamIndex can be -1 meaning they are not specified
122                 internal static UnmanagedMarshal DefineLPArrayInternal (UnmanagedType elemType, int sizeConst, int sizeParamIndex) {
123                         UnmanagedMarshal res = new UnmanagedMarshal (UnmanagedType.LPArray, elemType);
124                         res.count = sizeConst;
125                         res.param_num = sizeParamIndex;
126                         res.has_size = true;
127
128                         return res;
129                 }
130         }
131 }
132 #endif