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.cs139
1 files changed, 87 insertions, 52 deletions
diff --git a/src/bindings/mono/eo_mono/FunctionWrapper.cs b/src/bindings/mono/eo_mono/FunctionWrapper.cs
index 1ea2097..d37fba9 100644
--- a/src/bindings/mono/eo_mono/FunctionWrapper.cs
+++ b/src/bindings/mono/eo_mono/FunctionWrapper.cs
@@ -1,24 +1,40 @@
1/*
2 * Copyright 2019 by its authors. See AUTHORS.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
1using System; 16using System;
2using System.Runtime.InteropServices; 17using System.Runtime.InteropServices;
3 18
4namespace Efl 19namespace Efl.Eo
5{ 20{
6 21
7namespace Eo 22/// <summary>Class to load functions pointers from a native module.
8{
9
10///<summary>Class to load functions pointers from a native module.
11/// 23///
12///This class has a platform-dependent implementation on whether it 24/// <para>This class has a platform-dependent implementation on whether it
13///is compiled for Windows (using LoadLibrary/GetProcAddress) or Unix 25/// is compiled for Windows (using LoadLibrary/GetProcAddress) or Unix
14///(dlopen/dlsym).</summary> 26/// (dlopen/dlsym).</para>
15public static partial class FunctionInterop 27/// <para>Since EFL 1.23.</para>
28/// </summary>
29internal static partial class FunctionInterop
16{ 30{
17 ///<summary>Loads a function pointer from the given module.</summary> 31 /// <summary>Loads a function pointer from the given module.
18 ///<param name="moduleName">The name of the module containing the function.</param> 32 /// <para>Since EFL 1.23.</para>
19 ///<param name="functionName">The name of the function to search for.</param> 33 /// </summary>
20 ///<returns>A function pointer that can be used with delegates.</returns> 34 /// <param name="moduleName">The name of the module containing the function.</param>
21 public static IntPtr LoadFunctionPointer(string moduleName, string functionName) 35 /// <param name="functionName">The name of the function to search for.</param>
36 /// <returns>A function pointer that can be used with delegates.</returns>
37 internal static IntPtr LoadFunctionPointer(string moduleName, string functionName)
22 { 38 {
23 IntPtr module = NativeModule.LoadLibrary(moduleName); 39 IntPtr module = NativeModule.LoadLibrary(moduleName);
24 Eina.Log.Debug($"searching {module} for {functionName}"); 40 Eina.Log.Debug($"searching {module} for {functionName}");
@@ -27,10 +43,12 @@ public static partial class FunctionInterop
27 return s; 43 return s;
28 } 44 }
29 45
30 ///<summary>Loads a function pointer from the default module.</summary> 46 /// <summary>Loads a function pointer from the default module.
31 ///<param name="functionName">The name of the function to search for.</param> 47 /// <para>Since EFL 1.23.</para>
32 ///<returns>A function pointer that can be used with delegates.</returns> 48 /// </summary>
33 public static IntPtr LoadFunctionPointer(string functionName) 49 /// <param name="functionName">The name of the function to search for.</param>
50 /// <returns>A function pointer that can be used with delegates.</returns>
51 internal static IntPtr LoadFunctionPointer(string functionName)
34 { 52 {
35 Eina.Log.Debug($"searching {null} for {functionName}"); 53 Eina.Log.Debug($"searching {null} for {functionName}");
36 var s = FunctionInterop.dlsym(IntPtr.Zero, functionName); 54 var s = FunctionInterop.dlsym(IntPtr.Zero, functionName);
@@ -39,13 +57,13 @@ public static partial class FunctionInterop
39 } 57 }
40} 58}
41 59
42///<summary>Wraps a native function in a portable manner. 60/// <summary>Wraps a native function in a portable manner.
43///
44///This is intended as a workaround DllImport limitations when switching between mono and dotnet.
45/// 61///
46///The parameter T must be a delegate. 62/// <para>This is intended as a workaround DllImport limitations when switching between mono and dotnet.</para>
47///</summary> 63/// <para>The parameter T must be a delegate.</para>
48public class FunctionWrapper<T> // NOTE: When supporting C# >=7.3, add a where T: System.Delegate? 64/// <para>Since EFL 1.23.</para>
65/// </summary>
66class FunctionWrapper<T> // NOTE: When supporting C# >=7.3, add a where T: System.Delegate?
49{ 67{
50 private Lazy<FunctionLoadResult<T>> loadResult; 68 private Lazy<FunctionLoadResult<T>> loadResult;
51#pragma warning disable 0414 69#pragma warning disable 0414
@@ -72,18 +90,22 @@ public class FunctionWrapper<T> // NOTE: When supporting C# >=7.3, add a where T
72 } 90 }
73 } 91 }
74 92
75 ///<summary>Creates a wrapper for the given function of the given module.</summary> 93 /// <summary>Creates a wrapper for the given function of the given module.
76 ///<param name="moduleName">The name of the module containing the function.</param> 94 /// <para>Since EFL 1.23.</para>
77 ///<param name="functionName">The name of the function to search for.</param> 95 /// </summary>
78 public FunctionWrapper(string moduleName, string functionName) 96 /// <param name="moduleName">The name of the module containing the function.</param>
97 /// <param name="functionName">The name of the function to search for.</param>
98 internal FunctionWrapper(string moduleName, string functionName)
79 : this(new NativeModule(moduleName), functionName) 99 : this(new NativeModule(moduleName), functionName)
80 { 100 {
81 } 101 }
82 102
83 ///<summary>Creates a wrapper for the given function of the given module.</summary> 103 /// <summary>Creates a wrapper for the given function of the given module.
84 ///<param name="module">The module wrapper containing the function.</param> 104 /// <para>Since EFL 1.23.</para>
85 ///<param name="functionName">The name of the function to search for.</param> 105 /// </summary>
86 public FunctionWrapper(NativeModule module, string functionName) 106 /// <param name="module">The module wrapper containing the function.</param>
107 /// <param name="functionName">The name of the function to search for.</param>
108 internal FunctionWrapper(NativeModule module, string functionName)
87 { 109 {
88 this.module = module; 110 this.module = module;
89 loadResult = new Lazy<FunctionLoadResult<T>> 111 loadResult = new Lazy<FunctionLoadResult<T>>
@@ -93,9 +115,11 @@ public class FunctionWrapper<T> // NOTE: When supporting C# >=7.3, add a where T
93 }); 115 });
94 } 116 }
95 117
96 ///<summary>Retrieves the result of function load.</summary> 118 /// <summary>Retrieves the result of function load.
97 ///<returns>The load result.</returns> 119 /// <para>Since EFL 1.23.</para>
98 public FunctionLoadResult<T> Value 120 /// </summary>
121 /// <returns>The load result.</returns>
122 internal FunctionLoadResult<T> Value
99 { 123 {
100 get 124 get
101 { 125 {
@@ -104,8 +128,10 @@ public class FunctionWrapper<T> // NOTE: When supporting C# >=7.3, add a where T
104 } 128 }
105} 129}
106 130
107///<summary>The outcome of the function load process.</summary> 131/// <summary>The outcome of the function load process.
108public enum FunctionLoadResultKind 132/// <para>Since EFL 1.23.</para>
133/// </summary>
134enum FunctionLoadResultKind
109{ 135{
110 ///<summary>Function was loaded successfully.</summary> 136 ///<summary>Function was loaded successfully.</summary>
111 Success, 137 Success,
@@ -115,17 +141,24 @@ public enum FunctionLoadResultKind
115 FunctionNotFound 141 FunctionNotFound
116} 142}
117 143
118///<summary>Represents the result of loading a function pointer.</summary> 144/// <summary>Represents the result of loading a function pointer.
119public class FunctionLoadResult<T> 145/// <para>Since EFL 1.23.</para>
146/// </summary>
147class FunctionLoadResult<T>
120{ 148{
121 ///<summary>The status of the load.</summary> 149 /// <summary>The status of the load.
122 public FunctionLoadResultKind Kind; 150 /// <para>Since EFL 1.23.</para>
151 /// </summary>
152 FunctionLoadResultKind Kind;
123 private T _Delegate; 153 private T _Delegate;
124 154
125 ///<summary>The delegate wrapping the loaded function pointer. 155 /// <summary>The delegate wrapping the loaded function pointer.
126 /// 156 ///
127 ///Throws InvalidOperationException if trying to access while not loaded.</summary> 157 /// <para>Throws InvalidOperationException if trying to access while not loaded.</para>
128 public T Delegate 158 /// <para>Since EFL 1.23.</para>
159 /// </summary>
160 /// <value>The <c>delegate</c> wrapping the native function.</value>
161 internal T Delegate
129 { 162 {
130 get 163 get
131 { 164 {
@@ -138,16 +171,20 @@ public class FunctionLoadResult<T>
138 } 171 }
139 } 172 }
140 173
141 ///<summary>Creates a new load result of the given kind.</summary> 174 /// <summary>Creates a new load result of the given kind.
142 ///<param name="kind">The outcome of the load process.</param> 175 /// <para>Since EFL 1.23.</para>
143 public FunctionLoadResult(FunctionLoadResultKind kind) 176 /// </summary>
177 /// <param name="kind">The outcome of the load process.</param>
178 internal FunctionLoadResult(FunctionLoadResultKind kind)
144 { 179 {
145 this.Kind = kind; 180 this.Kind = kind;
146 } 181 }
147 182
148 ///<summary>Creates a new load result with the given delegate.</summary> 183 /// <summary>Creates a new load result with the given delegate.
149 ///<param name="Delegate">The delegate wrapping the native function.</param> 184 /// <para>Since EFL 1.23.</para>
150 public FunctionLoadResult(T Delegate) 185 /// </summary>
186 /// <param name="Delegate">The delegate wrapping the native function.</param>
187 internal FunctionLoadResult(T Delegate)
151 { 188 {
152 this._Delegate = Delegate; 189 this._Delegate = Delegate;
153 this.Kind = FunctionLoadResultKind.Success; 190 this.Kind = FunctionLoadResultKind.Success;
@@ -155,5 +192,3 @@ public class FunctionLoadResult<T>
155} 192}
156 193
157} 194}
158
159}