Reorganisation of the source tree
[ghc-hetmet.git] / rts / dotnet / Invoker.h
1 //
2 // (c) 2003, sof.
3 //
4 // Dynamic invocation helper classes. The details of how
5 // to access the .NET object model via the Reflection API
6 // is taken care of by Invoker.{h,cpp}
7 //
8 #pragma once
9 #using <mscorlib.dll>
10
11 using namespace System;
12 using namespace System::Reflection;
13 using namespace System::Text;
14 using namespace System::Runtime::InteropServices;
15
16 [assembly:AssemblyKeyFileAttribute(S"invoker.snk")];
17
18 namespace DynInvoke {
19
20 //
21 // Class: TypeName
22 //
23 // Purpose: pairing up an assembly name and the type/class name.
24 //
25 [ComVisible(false)]
26 public __gc class TypeName {
27
28 public:
29   System::String* m_assembly;
30   System::String* m_class;
31   int     m_length;
32
33   TypeName() { 
34     m_assembly = String::Empty;
35     m_class = String::Empty;
36     m_length = 0;
37   }
38
39   void Print() {
40     if (m_assembly && m_assembly != String::Empty ) {
41       Console::Write("[");
42       Console::Write(m_assembly);
43       Console::Write("]");
44     }
45     Console::WriteLine(m_class);
46   }
47   
48   int Length() { return m_length; }
49
50   System::String* toStdString() {
51     System::String* res = new System::String(m_class->ToCharArray());
52     
53     if (m_assembly && m_assembly != String::Empty ){
54       res = String::Concat(res, S",");
55       res = String::Concat(res, m_assembly);
56     }
57     return res;
58   }
59 };
60
61 // 
62 // Class:   InvokeBridge
63 // 
64 // Purpose: Collection of (static) methods for dynamically creating
65 //          objects and accessing methods/fields on them. 
66 //
67 [ClassInterface(ClassInterfaceType::AutoDual),
68 GuidAttribute("39D497D9-60E0-3525-B7F2-7BC096D3A2A3"),
69 ComVisible(true)
70 ]
71 public __gc class InvokeBridge {
72 public:
73   InvokeBridge() {
74     Assembly* corAss      = Assembly::Load("mscorlib.dll"); 
75     System::String*  dir  = System::IO::Path::GetDirectoryName(corAss->Location);
76    
77     m_assemblies = new System::Collections::ArrayList();
78    
79     System::String* fs[] = System::IO::Directory::GetFiles(dir, "*.dll");
80     for (int i=0;i < fs->Length; i++) {
81       try {
82         Assembly* tAss = Assembly::LoadFrom(fs[i]);
83         m_assemblies->Add(tAss->FullName);
84       } catch (Exception* e) {
85         continue;
86       }
87     }
88   }
89
90   //
91   // Method: CreateObject(String* assemName, String* objSpec, Object* args[])
92   //
93   // Purpose: Given a fully qualified name of a class/type, try
94   //          to create an instance of it.
95   //
96   Object* CreateObject(System::String* assemName,
97                        System::String* objSpec,
98                        Object* args[]);
99           
100   //
101   // Method:  InvokeMethod
102   // 
103   // Purpose: Given a pointer to an already created object, look up
104   //          one of its method. If found, invoke the method passing it
105   //          'args' as arguments.
106   //
107   // Comments: the format of the method-spec is "methodName(type1,..,typeN)" [N>=0]
108   //
109   Object* InvokeMethod(Object* obj, 
110                        System::String* methSpec,
111                        Object* args[]);
112                               
113   //
114   // Method:  InvokeStaticMethod
115   // 
116   // Purpose: Invoke a static method, given the fully qualified name
117   //          of the method (and its arguments). If found, invoke the
118   //          method passing it 'args' as arguments.
119   //
120   // Comments: the format of the method-spec is 
121   //              "T1.T2.<..>.Tn.methodName(type1,..,typeN)" [N>=0]
122   //
123   Object* InvokeStaticMethod(System::String* assemName,
124                              System::String* methSpec,
125                              Object* args[]);
126                               
127   //
128   // Method:  GetField
129   //
130   // Purpose: Fetch the (boxed) value of named field of a given object.
131   //
132   Object* GetField(Object* obj, System::String* fieldSpec);
133
134   //
135   // Method:  GetField
136   //
137   // Purpose: Fetch the (boxed) value of named static field.
138   //
139   Object* GetStaticField(System::String* clsName, 
140                          System::String* fieldSpec);
141
142   //
143   // Method:  SetField
144   //
145   // Purpose: Replace the (boxed) value of named field of a given object.
146   //
147   void SetField(Object* obj, System::String* fieldSpec, Object* val);
148             
149   //
150   // Method:  SetStaticField
151   //
152   // Purpose: Replace the (boxed) value of named field of a given object.
153   //
154   void SetStaticField(System::String* clsName,
155                       System::String* fieldSpec,
156                       Object* val);
157             
158
159   // 
160   // Method:  NewString
161   // 
162   // Purpose: construct a System.String object copy in a manner that avoids
163   //          COM Interop from deconstructing it to a BSTR.
164   //
165   System::Object* NewString( System::String* s);
166
167   //
168   // Method:  NewArgArray
169   //
170   // Purpose: create a new array for holding (boxed) arguments to constructors/
171   //          methods.
172   //
173   Array* NewArgArray(int sz);
174   
175   //
176   // Method: SetArg
177   //
178   // Purpose: set an entry in the argument vector.
179   //
180   void SetArg(Object* arr[], Object* val, int idx);
181
182   //
183   // Method: GetArg
184   //
185   // Purpose: get an entry in the argument vector.
186   //
187   Object* GetArg(Object* arr[], int idx);
188
189   System::Type* InvokeBridge::GetType(System::String* typeName);
190
191 protected:
192   System::Collections::ArrayList __gc* m_assemblies;
193   Object* InvokeBridge::CreateInstance(TypeName* typeName,
194                                        Object* args[]);
195 };
196
197 } /* namespace */