summaryrefslogtreecommitdiff
path: root/src/bindings/mono/eo_mono/FunctionWrapper.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/bindings/mono/eo_mono/FunctionWrapper.cs')
-rw-r--r--src/bindings/mono/eo_mono/FunctionWrapper.cs87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/bindings/mono/eo_mono/FunctionWrapper.cs b/src/bindings/mono/eo_mono/FunctionWrapper.cs
new file mode 100644
index 0000000..d42f1e3
--- /dev/null
+++ b/src/bindings/mono/eo_mono/FunctionWrapper.cs
@@ -0,0 +1,87 @@
1using System;
2using System.Runtime.InteropServices;
3
4namespace Efl { namespace Eo {
5
6public partial class FunctionInterop
7{
8 public static IntPtr LoadFunctionPointer(string moduleName, string functionName)
9 {
10 NativeModule module = new NativeModule(moduleName);
11 Console.WriteLine("searching {0} in library {1}", module.Module, functionName);
12 var s = FunctionInterop.dlsym(module.Module, functionName);
13 Console.WriteLine("searching {0} in library {1}, result {2}", module.Module, functionName, s);
14 return s;
15 }
16 public static IntPtr LoadFunctionPointer(string functionName)
17 {
18 Console.WriteLine("searching {0} in library {1}", null, functionName);
19 var s = FunctionInterop.dlsym(IntPtr.Zero, functionName);
20 Console.WriteLine("searching {0} in library {1}, result {2}", null, functionName, s);
21 return s;
22 }
23}
24
25public class FunctionWrapper<T>
26{
27 private Lazy<FunctionLoadResult<T>> loadResult;
28 private NativeModule module; // so it doesn't get unloaded
29
30 private static FunctionLoadResult<T> LazyInitialization(NativeModule module, string functionName)
31 {
32 if (module.Module == IntPtr.Zero)
33 return new FunctionLoadResult<T>(FunctionLoadResultKind.LibraryNotFound);
34 else
35 {
36 IntPtr funcptr = FunctionInterop.LoadFunctionPointer(module.Module, functionName);
37 if (funcptr == IntPtr.Zero)
38 return new FunctionLoadResult<T>(FunctionLoadResultKind.FunctionNotFound);
39 else
40 return new FunctionLoadResult<T>(Marshal.GetDelegateForFunctionPointer<T>(funcptr));
41 }
42 }
43
44 public FunctionWrapper(string moduleName, string functionName)
45 : this (new NativeModule(moduleName), functionName)
46 {
47 }
48
49 public FunctionWrapper(NativeModule module, string functionName)
50 {
51 this.module = module;
52 loadResult = new Lazy<FunctionLoadResult<T>>
53 (() =>
54 {
55 return LazyInitialization(module, functionName);
56 });
57 }
58
59 public FunctionLoadResult<T> Value
60 {
61 get
62 {
63 return loadResult.Value;
64 }
65 }
66}
67
68public enum FunctionLoadResultKind { Success, LibraryNotFound, FunctionNotFound }
69
70public class FunctionLoadResult<T>
71{
72 public FunctionLoadResultKind Kind;
73 public T Delegate;
74
75 public FunctionLoadResult(FunctionLoadResultKind kind)
76 {
77 this.Kind = kind;
78 }
79 public FunctionLoadResult(T Delegate)
80 {
81 this.Delegate = Delegate;
82 this.Kind = FunctionLoadResultKind.Success;
83 }
84}
85
86
87} }