Release 4.0.0-preview1-00235
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / Registry.cs
1
2 using System.Reflection;
3 using System;
4 using System.Runtime.InteropServices;
5 using System.Collections.Generic;
6 using Tizen.NUI.BaseComponents;
7
8 namespace Tizen.NUI
9 {
10     /// <summary>
11     /// This is used to store a mapping between C++ base handle objects and it's C# instances.
12     ///
13     /// </summary>
14     internal sealed class Registry
15     {
16         /// <summary>
17         /// The registry is a singleton.
18         /// </summary>
19         private static Registry instance = null;
20
21         /// <summary>
22         /// Given a C++ object, the dictionary allows us to find which C# object it belongs to.
23         /// By keeping the weak reference only, it will allow the object to be garbage collected.
24         /// </summary>
25         private Dictionary<IntPtr, WeakReference> _controlMap;
26
27         private Registry()
28         {
29             _controlMap = new Dictionary<IntPtr, WeakReference>();
30         }
31
32
33         /// <summary>
34         /// Stores the mapping between this instance of BaseHandle (C# base class) and native part.
35         /// </summary>
36         /// <param name="baseHandle">The instance of BaseHandle (C# base class).</param>
37         internal static void Register(BaseHandle baseHandle)
38         {
39             // We store a pointer to the RefObject for the control
40             RefObject refObj = baseHandle.GetObjectPtr();
41             IntPtr refCptr = (IntPtr)RefObject.getCPtr(refObj);
42
43             NUILog.Debug("________Storing ref object cptr in control map Hex: {0:X}" + refCptr);
44
45             if (!Instance._controlMap.ContainsKey(refCptr))
46             {
47                 Instance._controlMap.Add(refCptr, new WeakReference(baseHandle, false));
48             }
49
50             return;
51         }
52
53         /// <summary>
54         /// Removes this instance of BaseHandle (C# base class) and native part from the mapping table.
55         /// </summary>
56         /// <param name="baseHandle"> The instance of BaseHandle (C# base class)</param>
57         internal static void Unregister(BaseHandle baseHandle)
58         {
59             RefObject refObj = baseHandle.GetObjectPtr();
60             IntPtr refCptr = (IntPtr)RefObject.getCPtr(refObj);
61
62             if (Instance._controlMap.ContainsKey(refCptr))
63             {
64                 Instance._controlMap.Remove(refCptr);
65             }
66
67             return;
68         }
69
70         private static Registry Instance
71         {
72             get
73             {
74                 if (instance == null)
75                 {
76                     instance = new Registry();
77                 }
78                 return instance;
79             }
80         }
81
82         internal static BaseHandle GetManagedBaseHandleFromNativePtr(BaseHandle baseHandle)
83         {
84             RefObject refObj = baseHandle.GetObjectPtr();
85             IntPtr refObjectPtr = (IntPtr)RefObject.getCPtr(refObj);
86
87             // we store a dictionary of ref-obects (C++ land) to managed obects (C# land)
88             return GetManagedBaseHandleFromRefObject(refObjectPtr);
89         }
90
91         internal static BaseHandle GetManagedBaseHandleFromNativePtr(IntPtr cPtr)
92         {
93             IntPtr refObjectPtr = NDalicPINVOKE.GetRefObjectPtr(cPtr);
94
95             // we store a dictionary of ref-obects (C++ land) to managed obects (C# land)
96             return GetManagedBaseHandleFromRefObject(refObjectPtr);
97         }
98
99         internal static BaseHandle GetManagedBaseHandleFromRefObject(IntPtr refObjectPtr)
100         {
101             // we store a dictionary of ref-obects (C++ land) to managed obects (C# land)
102             WeakReference weakReference;
103
104             if (Instance._controlMap.TryGetValue(refObjectPtr, out weakReference))
105             {
106                 BaseHandle ret = weakReference.Target as BaseHandle;
107                 return ret;
108             }
109             else
110             {
111                 return null;
112             }
113         }
114
115     }
116 }