summaryrefslogtreecommitdiff
path: root/src/bindings/mono/eo_mono
diff options
context:
space:
mode:
authorLauro Moura <lauromoura@expertisesolutions.com.br>2019-11-01 10:04:04 -0300
committerLauro Moura <lauromoura@expertisesolutions.com.br>2019-11-01 10:04:04 -0300
commitbd6876c97eb313f6ce5ece0b2b380129f8d76fb1 (patch)
tree3cfeb716d195a528135b0d1761bddb2df1aeb7c5 /src/bindings/mono/eo_mono
parentb80b9213adc57b3e890351824cfef08b6a40de67 (diff)
csharp: Refactor MarshalOwn
Summary: Instead of using the empty interface as tag, split MarshalOwn into two marshalers that can be used directly. Fixes T8395 (CA1040) Test Plan: no new functionality. Use existing tests Reviewers: felipealmeida, brunobelo, segfaultxavi, YOhoho Reviewed By: YOhoho Subscribers: cedric, #reviewers, #committers Tags: #efl Maniphest Tasks: T8395 Differential Revision: https://phab.enlightenment.org/D10457
Diffstat (limited to 'src/bindings/mono/eo_mono')
-rw-r--r--src/bindings/mono/eo_mono/iwrapper.cs164
1 files changed, 133 insertions, 31 deletions
diff --git a/src/bindings/mono/eo_mono/iwrapper.cs b/src/bindings/mono/eo_mono/iwrapper.cs
index 12a7b32..f886167 100644
--- a/src/bindings/mono/eo_mono/iwrapper.cs
+++ b/src/bindings/mono/eo_mono/iwrapper.cs
@@ -20,6 +20,7 @@ using System.Runtime.InteropServices;
20using System.Runtime.CompilerServices; 20using System.Runtime.CompilerServices;
21using System.Collections.Generic; 21using System.Collections.Generic;
22using System.Diagnostics; 22using System.Diagnostics;
23using System.Diagnostics.CodeAnalysis;
23using System.Reflection; 24using System.Reflection;
24using System.Threading; 25using System.Threading;
25using System.Linq; 26using System.Linq;
@@ -1026,67 +1027,168 @@ internal static class ClassRegister
1026 private static readonly object klassAllocLock = new object(); 1027 private static readonly object klassAllocLock = new object();
1027} 1028}
1028 1029
1029interface IOwnershipTag 1030/// <summary>Custom marshaler for Eo objects that do not move ownership between native and managed code.
1031///
1032/// <para>For internal usage by generated code.</para>
1033///
1034/// <para>Since EFL 1.24</para>
1035/// </summary>
1036class MarshalEoNoMove : ICustomMarshaler
1030{ 1037{
1031} 1038 private static ICustomMarshaler instance = new MarshalEoNoMove();
1032 1039
1033class OwnTag : IOwnershipTag 1040 /// <summary>
1034{ 1041 /// Gets an instance of this marshaler.
1035} 1042 /// <para>Since EFL 1.24.</para>
1043 /// </summary>
1044 /// <param name="cookie">Cookie to identify the marshaler. Unused.</param>
1045 /// <returns>The marshaler instance.</returns>
1046 [SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", Justification = "The same marshaler is used for all cases.")]
1047 public static ICustomMarshaler GetInstance(string cookie) => instance;
1036 1048
1037class NonOwnTag : IOwnershipTag 1049 /// <summary>
1038{ 1050 /// Clean ups the managed data.
1039} 1051 /// <para>Since EFL 1.24.</para>
1052 /// </summary>
1053 /// <param name="ManagedObj">The object to be cleaned.</param>
1054 public void CleanUpManagedData(object ManagedObj)
1055 {
1056 }
1040 1057
1041class MarshalEo<U> : ICustomMarshaler 1058 /// <summary>
1042 where U : IOwnershipTag 1059 /// Clean ups the native data if it was created.
1043{ 1060 /// <para>Since EFL 1.24.</para>
1044 internal static ICustomMarshaler GetInstance(string cookie) 1061 /// </summary>
1062 /// <param name="pNativeData">The native data to be cleaned.</param>
1063 public void CleanUpNativeData(IntPtr pNativeData)
1045 { 1064 {
1046 Eina.Log.Debug("MarshalEo.GetInstace cookie " + cookie);
1047 return new MarshalEo<U>();
1048 } 1065 }
1049 1066
1050 public void CleanUpManagedData(object ManagedObj) 1067 /// <summary>
1068 /// Gets the native data size.
1069 /// <para>Since EFL 1.24.</para>
1070 /// </summary>
1071 /// <returns>The data size in bytes.</returns>
1072 public int GetNativeDataSize() => -1;
1073
1074 /// <summary>
1075 /// Marshals the given managed object to its native handle.
1076 /// <para>As this marshaler does not move the reference, the managed code
1077 /// can keep its reference and does not need to incref.</para>
1078 /// <para>Since EFL 1.24.</para>
1079 /// </summary>
1080 /// <param name="ManagedObj">The object to be marshalled.</param>
1081 /// <returns>The marshalled native data.</returns>
1082 public IntPtr MarshalManagedToNative(object ManagedObj)
1083 {
1084 if (ManagedObj == null)
1085 {
1086 return IntPtr.Zero;
1087 }
1088
1089 IWrapper wrapper = ManagedObj as IWrapper;
1090
1091 if (wrapper == null)
1092 {
1093 throw new ArgumentException("Managed object to be marshalled must be an IWrapper.");
1094 }
1095
1096 return wrapper.NativeHandle;
1097 }
1098
1099 /// <summary>
1100 /// Marshals the given native pointer into a managed object.
1101 /// <para>The given native object has its reference count incremented in order to make
1102 /// the C# wrapper capable of accessing it while the wrapper is alive.</para>
1103 /// <para>Since EFL 1.24.</para>
1104 /// </summary>
1105 /// <param name="pNativeData">The native pointer to the EO object.</param>
1106 /// <returns>The managed wrapper for the given native object.</returns>
1107 public object MarshalNativeToManaged(IntPtr pNativeData)
1051 { 1108 {
1052 //Eina.Log.Warning("MarshalEo.CleanUpManagedData not implemented"); 1109 return Efl.Eo.Globals.CreateWrapperFor(pNativeData, shouldIncRef : true);
1053 //throw new NotImplementedException();
1054 } 1110 }
1111}
1055 1112
1056 public void CleanUpNativeData(IntPtr pNativeData) 1113/// <summary>Custom marshaler for Eo objects that move ownership between native and managed code.
1114///
1115/// <para>For internal usage by generated code.</para>
1116///
1117/// <para>Since EFL 1.24</para>
1118/// </summary>
1119class MarshalEoMove : ICustomMarshaler
1120{
1121 private static ICustomMarshaler instance = new MarshalEoMove();
1122
1123 /// <summary>
1124 /// Gets an instance of this marshaler.
1125 /// <para>Since EFL 1.24.</para>
1126 /// </summary>
1127 /// <param name="cookie">Cookie to identify the marshaler. Unused.</param>
1128 /// <returns>The marshaler instance.</returns>
1129 [SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", Justification = "The same marshaler is used for all cases.")]
1130 public static ICustomMarshaler GetInstance(string cookie) => instance;
1131
1132 /// <summary>
1133 /// Clean ups the managed data.
1134 /// <para>Since EFL 1.24.</para>
1135 /// </summary>
1136 /// <param name="ManagedObj">The object to be cleaned.</param>
1137 public void CleanUpManagedData(object ManagedObj)
1057 { 1138 {
1058 //Eina.Log.Warning("MarshalEo.CleanUpNativeData not implemented");
1059 //throw new NotImplementedException();
1060 } 1139 }
1061 1140
1062 public int GetNativeDataSize() 1141 /// <summary>
1142 /// Clean ups the native data if it was created.
1143 /// <para>Since EFL 1.24.</para>
1144 /// </summary>
1145 /// <param name="pNativeData">The native data to be cleaned.</param>
1146 public void CleanUpNativeData(IntPtr pNativeData)
1063 { 1147 {
1064 Eina.Log.Debug("MarshalEo.GetNativeDataSize");
1065 return 0;
1066 //return 8;
1067 } 1148 }
1068 1149
1150 /// <summary>
1151 /// Gets the native data size.
1152 /// <para>Since EFL 1.24.</para>
1153 /// </summary>
1154 /// <returns>The data size in bytes.</returns>
1155 public int GetNativeDataSize() => -1;
1156
1157 /// <summary>
1158 /// Marshals the given managed object to its native handle.
1159 /// <para>The wrapper given as parameter needs to keep a reference to the native
1160 /// object, so the EO has its refcount incremented.</para>
1161 /// <para>Since EFL 1.24.</para>
1162 /// </summary>
1163 /// <param name="ManagedObj">The object to be marshalled.</param>
1164 /// <returns>The marshalled native data.</returns>
1069 public IntPtr MarshalManagedToNative(object ManagedObj) 1165 public IntPtr MarshalManagedToNative(object ManagedObj)
1070 { 1166 {
1071 Eina.Log.Debug("MarshalEo.MarshallManagedToNative");
1072
1073 if (ManagedObj == null) 1167 if (ManagedObj == null)
1074 { 1168 {
1075 return IntPtr.Zero; 1169 return IntPtr.Zero;
1076 } 1170 }
1077 1171
1078 var r = ((IWrapper)ManagedObj).NativeHandle; 1172 IWrapper wrapper = ManagedObj as IWrapper;
1079 if (typeof(U) == typeof(OwnTag)) 1173
1174 if (wrapper == null)
1080 { 1175 {
1081 Efl.Eo.Globals.efl_ref(r); 1176 throw new ArgumentException("Managed object to be marshalled must be an IWrapper.");
1082 } 1177 }
1083 1178
1084 return r; 1179 return Efl.Eo.Globals.efl_ref(wrapper.NativeHandle);
1085 } 1180 }
1086 1181
1182 /// <summary>
1183 /// Marshals the given native pointer into a managed object.
1184 /// <para>The returned wrapper "steals" the reference to keep it alive.</para>
1185 /// <para>Since EFL 1.24.</para>
1186 /// </summary>
1187 /// <param name="pNativeData">The native pointer to the EO object.</param>
1188 /// <returns>The managed wrapper for the given native object.</returns>
1087 public object MarshalNativeToManaged(IntPtr pNativeData) 1189 public object MarshalNativeToManaged(IntPtr pNativeData)
1088 { 1190 {
1089 return Efl.Eo.Globals.CreateWrapperFor(pNativeData, shouldIncRef : typeof(U) != typeof(OwnTag)); 1191 return Efl.Eo.Globals.CreateWrapperFor(pNativeData, shouldIncRef : false);
1090 } 1192 }
1091} 1193}
1092 1194