Mercurial > hg > vboxdotnet
comparison InteropCodeGen/ComInteropInterfaceGenerator.cs @ 6:e640ca67b819
Added extended COM interop code generator for interfaces and proxies
author | Ivo Smits |
---|---|
date | Fri, 06 May 2011 04:02:43 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
5:00fb4879d273 | 6:e640ca67b819 |
---|---|
1 using System; | |
2 using System.Collections.Generic; | |
3 using System.Text; | |
4 using System.IO; | |
5 | |
6 namespace VBoxSDK { | |
7 class ComInteropInterfaceGenerator { | |
8 public TextWriter Output { get; private set; } | |
9 public Boolean IncludeInheritedMembers { get; set; } | |
10 public Boolean IncludeComAttributes { get; set; } | |
11 public Boolean ComInterfaceTypeIDispatch { get; set; } | |
12 public String TypeModifiers { get; set; } | |
13 | |
14 public ComInteropInterfaceGenerator(TextWriter output) { | |
15 Output = output; | |
16 } | |
17 public void WriteLibrary(LibraryInfo lib) { | |
18 foreach (KeyValuePair<String, EnumInfo> enumi in lib.Enums) { | |
19 Output.WriteLine("{1} enum {0} {{", enumi.Key, TypeModifiers); | |
20 foreach (KeyValuePair<String, Int32> value in enumi.Value.Values) { | |
21 Output.WriteLine("{0} = {1},", value.Key, value.Value); | |
22 } | |
23 Output.WriteLine("}"); | |
24 } | |
25 foreach (KeyValuePair<String, InterfaceInfo> intf in lib.Interfaces) { | |
26 if (IncludeComAttributes) { | |
27 if (ComInterfaceTypeIDispatch) { | |
28 Output.WriteLine("[InterfaceType(ComInterfaceType.InterfaceIsDual)]"); | |
29 } else { | |
30 Output.WriteLine("[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]"); | |
31 } | |
32 Output.WriteLine("[Guid(\"{0}\")]", intf.Value.IID); | |
33 Output.WriteLine("[ComImport()]"); | |
34 } | |
35 Output.Write("{1} interface {0} ", intf.Key, TypeModifiers); | |
36 if (intf.Value.Extends != null) Output.Write(": {0} ", intf.Value.Extends.Name); | |
37 Output.WriteLine("{"); | |
38 WriteInterfaceMembers(intf.Value); | |
39 Output.WriteLine("}"); | |
40 } | |
41 } | |
42 public void WriteInterfaceMembers(InterfaceInfo intf) { | |
43 if (IncludeInheritedMembers && intf.Extends != null && intf.Extends.Name != "IUnknown") WriteInterfaceMembers(intf.Extends); | |
44 WriteInterfaceMembers(intf.Members); | |
45 } | |
46 public void WriteInterfaceMembers(ICollection<InterfaceMemberInfo> members) { | |
47 foreach (InterfaceMemberInfo member in members) { | |
48 if (member is PropertyInfo) { | |
49 PropertyInfo memberi = (PropertyInfo)member; | |
50 if (!memberi.Gettable && !memberi.Settable) continue; | |
51 Output.WriteLine("{0} {1} {{", memberi.Type.Name, memberi.Name); | |
52 if (memberi.Gettable) { | |
53 if (IncludeComAttributes) Output.Write("[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] "); | |
54 if (IncludeComAttributes) WriteTypeComMarshalAttributes(memberi.Type, "return"); | |
55 Output.WriteLine("get;"); | |
56 } | |
57 if (memberi.Settable) { | |
58 if (IncludeComAttributes) Output.Write("[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] "); | |
59 if (IncludeComAttributes) WriteTypeComMarshalAttributes(memberi.Type, "param"); | |
60 Output.WriteLine("set;"); | |
61 } | |
62 Output.WriteLine("}"); | |
63 } else if (member is MethodInfo) { | |
64 MethodInfo memberi = (MethodInfo)member; | |
65 if (IncludeComAttributes) Output.Write("[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] "); | |
66 if (memberi.ReturnType == null) { | |
67 Output.Write("void "); | |
68 } else { | |
69 if (IncludeComAttributes) WriteTypeComMarshalAttributes(memberi.ReturnType, "return"); | |
70 Output.Write("{0} ", memberi.ReturnType.Name); | |
71 } | |
72 Output.Write("{0}(", memberi.Name); | |
73 Boolean first = true; | |
74 foreach (MethodParameterInfo param in memberi.Parameters) { | |
75 if (first) { | |
76 first = false; | |
77 } else { | |
78 Output.Write(", "); | |
79 } | |
80 if (IncludeComAttributes) WriteTypeComMarshalAttributes(param.Type, null); | |
81 if (param.Output && !param.Input) Output.Write("out "); | |
82 else if (param.Reference) Output.Write("ref "); | |
83 Output.Write("{0} p{1}", param.Type.Name, param.Name); | |
84 } | |
85 Output.WriteLine(");"); | |
86 } | |
87 } | |
88 } | |
89 public void WriteTypeComMarshalAttributes(TypeInfo type, String paramType) { | |
90 String MarshalAs = null; | |
91 if (type is InterfaceTypeInfo) { | |
92 MarshalAs = "Interface"; | |
93 } else if (type is StringTypeInfo) { | |
94 MarshalAs = (type as StringTypeInfo).UnmanagedType.ToString(); | |
95 } | |
96 if (MarshalAs != null) { | |
97 Output.Write("["); | |
98 if (paramType != null) Output.Write("{0}: ", paramType); | |
99 Output.Write("MarshalAs(UnmanagedType.{0})] ", MarshalAs); | |
100 } | |
101 } | |
102 } | |
103 } |