[EflSharp] Update Circle and efl cs files (#945)
[platform/core/csapi/tizenfx.git] / internals / src / EflSharp / EflSharp / efl / eina_common.cs
1 #pragma warning disable 1591
2
3 using System;
4 using System.Text;
5 using System.Runtime.InteropServices;
6
7 namespace Eina
8 {
9
10 namespace Callbacks
11 {
12
13 public delegate int EinaCompareCb(IntPtr data1, IntPtr data2);
14 public delegate void EinaFreeCb(IntPtr data);
15
16 }
17
18 internal static class NativeCustomExportFunctions
19 {
20     [DllImport(efl.Libs.CustomExports)] public static extern void
21         efl_mono_native_free(IntPtr ptr);
22     [DllImport(efl.Libs.CustomExports)] public static extern void
23         efl_mono_native_free_ref(IntPtr ptr);
24     [DllImport(efl.Libs.CustomExports)] public static extern IntPtr
25         efl_mono_native_alloc(uint count);
26     [DllImport(efl.Libs.CustomExports)] public static extern IntPtr
27         efl_mono_native_memset(IntPtr ptr, uint fill, uint count);
28     [DllImport(efl.Libs.CustomExports)] public static extern IntPtr
29         efl_mono_native_alloc_copy(IntPtr val, uint size);
30     [DllImport(efl.Libs.CustomExports)] public static extern IntPtr
31         efl_mono_native_strdup(string str);
32
33     [DllImport(efl.Libs.CustomExports)] public static extern IntPtr
34         efl_mono_native_ptr_compare_addr_get();
35     [DllImport(efl.Libs.CustomExports)] public static extern IntPtr
36         efl_mono_native_str_compare_addr_get();
37
38     [DllImport(efl.Libs.CustomExports)] public static extern IntPtr
39         efl_mono_native_free_addr_get();
40     [DllImport(efl.Libs.CustomExports)] public static extern IntPtr
41         efl_mono_native_efl_unref_addr_get();
42 }
43
44 /// <summary>Wrapper around native memory DllImport'd functions</summary>
45 public static class MemoryNative
46 {
47     public static void Free(IntPtr ptr)
48     {
49         NativeCustomExportFunctions.efl_mono_native_free(ptr);
50     }
51
52     public static void FreeRef(IntPtr ptr)
53     {
54         NativeCustomExportFunctions.efl_mono_native_free_ref(ptr);
55     }
56
57     // This public api uses int as Marshal.SizeOf return an int instead of uint.
58     public static IntPtr Alloc(int count)
59     {
60         return NativeCustomExportFunctions.efl_mono_native_alloc(Convert.ToUInt32(count));
61     }
62
63     public static void Memset(IntPtr ptr, int fill, int count)
64     {
65         NativeCustomExportFunctions.efl_mono_native_memset(ptr, Convert.ToUInt32(fill), Convert.ToUInt32(count));
66     }
67
68     public static IntPtr AllocCopy(IntPtr ptr, int count)
69     {
70         return NativeCustomExportFunctions.efl_mono_native_alloc_copy(ptr, Convert.ToUInt32(count));
71     }
72
73     public static IntPtr StrDup(string str)
74     {
75         return NativeCustomExportFunctions.efl_mono_native_strdup(str);
76     }
77
78     public static IntPtr AddStringshare(string str)
79     {
80         IntPtr nativeStr = StringConversion.ManagedStringToNativeUtf8Alloc(str);
81         try
82         {
83             var strShare = NativeMethods.eina_stringshare_add(nativeStr);
84             return strShare;
85         }
86         finally
87         {
88             Eina.MemoryNative.Free(nativeStr);
89         }
90     }
91
92     public static void DelStringshare(IntPtr str)
93     {
94         NativeMethods.eina_stringshare_del(str);
95     }
96
97     public static void DelStringshareRef(IntPtr ptr)
98     {
99         NativeMethods.efl_mono_native_stringshare_del_ref(ptr);
100     }
101
102     // IntPtr's for some native functions
103     public static IntPtr PtrCompareFuncPtrGet()
104     {
105         return NativeCustomExportFunctions.efl_mono_native_ptr_compare_addr_get();
106     }
107
108     public static IntPtr StrCompareFuncPtrGet()
109     {
110         return NativeCustomExportFunctions.efl_mono_native_str_compare_addr_get();
111     }
112
113     public static IntPtr FreeFuncPtrGet()
114     {
115         return NativeCustomExportFunctions.efl_mono_native_free_addr_get();
116     }
117
118     public static IntPtr StringshareDelFuncPtrGet()
119     {
120         return NativeMethods.efl_mono_native_stringshare_del_addr_get();
121     }
122
123     public static IntPtr EflUnrefFuncPtrGet()
124     {
125         return NativeCustomExportFunctions.efl_mono_native_efl_unref_addr_get();
126     }
127 }
128
129 public static class PrimitiveConversion
130 {
131    public static T PointerToManaged<T>(IntPtr nat)
132    {
133        if (nat == IntPtr.Zero)
134        {
135            Eina.Log.Error("Null pointer for primitive type.");
136            return default(T);
137        }
138
139        var w = Marshal.PtrToStructure<T>(nat);
140        return w;
141    }
142
143    public static IntPtr ManagedToPointerAlloc<T>(T man)
144    {
145        IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf<T>());
146        Marshal.StructureToPtr(man, ptr, false);
147        return ptr;
148    }
149 }
150
151 public static class StringConversion
152 {
153     public static IntPtr ManagedStringToNativeUtf8Alloc(string managedString)
154     {
155         if (managedString == null)
156         {
157             return IntPtr.Zero;
158         }
159
160         byte[] strbuf = Encoding.UTF8.GetBytes(managedString);
161         IntPtr native = MemoryNative.Alloc(strbuf.Length + 1);
162         try
163         {
164             Marshal.Copy(strbuf, 0, native, strbuf.Length);
165             Marshal.WriteByte(native + strbuf.Length, 0); // write the terminating null
166             return native;
167         }
168         catch(Exception e)
169         {
170             MemoryNative.Free(native);
171             throw e;
172         }
173     }
174
175     public static string NativeUtf8ToManagedString(IntPtr pNativeData)
176     {
177         if (pNativeData == IntPtr.Zero)
178         {
179             return null;
180         }
181
182         int len = 0;
183         while (Marshal.ReadByte(pNativeData, len) != 0)
184         {
185             ++len;
186         }
187
188         byte[] strbuf = new byte[len];
189         Marshal.Copy(pNativeData, strbuf, 0, strbuf.Length);
190         return Encoding.UTF8.GetString(strbuf);
191     }
192 }
193
194 /// <summary>Enum to handle resource ownership between managed and unmanaged code.</summary>
195 public enum Ownership
196 {
197     /// <summary> The resource is owned by the managed code. It should free the handle on disposal.</summary>
198     Managed,
199     /// <summary> The resource is owned by the unmanaged code. It won't be freed on disposal.</summary>
200     Unmanaged
201 }
202
203 }