[EflSharp] Separate efl and Circle cs files and Update cs files (#786)
[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     // IntPtr's for some native functions
79     public static IntPtr PtrCompareFuncPtrGet()
80     {
81         return NativeCustomExportFunctions.efl_mono_native_ptr_compare_addr_get();
82     }
83
84     public static IntPtr StrCompareFuncPtrGet()
85     {
86         return NativeCustomExportFunctions.efl_mono_native_str_compare_addr_get();
87     }
88
89     public static IntPtr FreeFuncPtrGet()
90     {
91         return NativeCustomExportFunctions.efl_mono_native_free_addr_get();
92     }
93
94     public static IntPtr EflUnrefFuncPtrGet()
95     {
96         return NativeCustomExportFunctions.efl_mono_native_efl_unref_addr_get();
97     }
98 }
99
100 public static class PrimitiveConversion
101 {
102    public static T PointerToManaged<T>(IntPtr nat)
103    {
104        if (nat == IntPtr.Zero)
105        {
106            Eina.Log.Error("Null pointer for primitive type.");
107            return default(T);
108        }
109
110        var w = Marshal.PtrToStructure<T>(nat);
111        return w;
112    }
113
114    public static IntPtr ManagedToPointerAlloc<T>(T man)
115    {
116        IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf<T>());
117        Marshal.StructureToPtr(man, ptr, false);
118        return ptr;
119    }
120 }
121
122 public static class StringConversion
123 {
124     public static IntPtr ManagedStringToNativeUtf8Alloc(string managedString)
125     {
126         if (managedString == null)
127         {
128             return IntPtr.Zero;
129         }
130
131         byte[] strbuf = Encoding.UTF8.GetBytes(managedString);
132         IntPtr native = MemoryNative.Alloc(strbuf.Length + 1);
133         Marshal.Copy(strbuf, 0, native, strbuf.Length);
134         Marshal.WriteByte(native + strbuf.Length, 0); // write the terminating null
135         return native;
136     }
137
138     public static string NativeUtf8ToManagedString(IntPtr pNativeData)
139     {
140         if (pNativeData == IntPtr.Zero)
141         {
142             return null;
143         }
144
145         int len = 0;
146         while (Marshal.ReadByte(pNativeData, len) != 0)
147         {
148             ++len;
149         }
150
151         byte[] strbuf = new byte[len];
152         Marshal.Copy(pNativeData, strbuf, 0, strbuf.Length);
153         return Encoding.UTF8.GetString(strbuf);
154     }
155 }
156
157 /// <summary>Enum to handle resource ownership between managed and unmanaged code.</summary>
158 public enum Ownership
159 {
160     /// <summary> The resource is owned by the managed code. It should free the handle on disposal.</summary>
161     Managed,
162     /// <summary> The resource is owned by the unmanaged code. It won't be freed on disposal.</summary>
163     Unmanaged
164 }
165
166 }