2 using System.Reflection;
4 using System.Runtime.InteropServices;
5 using System.Collections.Generic;
6 using Tizen.NUI.BaseComponents;
11 /// This is used to store a mapping between C++ base handle objects and it's C# instances.
14 internal sealed class Registry
17 /// The registry is a singleton.
19 private static Registry instance = null;
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.
25 private Dictionary<IntPtr, WeakReference> _controlMap;
29 _controlMap = new Dictionary<IntPtr, WeakReference>();
34 /// Stores the mapping between this instance of BaseHandle (C# base class) and native part.
36 /// <param name="baseHandle">The instance of BaseHandle (C# base class).</param>
37 internal static void Register(BaseHandle baseHandle)
39 // We store a pointer to the RefObject for the control
40 RefObject refObj = baseHandle.GetObjectPtr();
41 IntPtr refCptr = (IntPtr)RefObject.getCPtr(refObj);
43 NUILog.Debug("________Storing ref object cptr in control map Hex: {0:X}" + refCptr);
45 if (!Instance._controlMap.ContainsKey(refCptr))
47 Instance._controlMap.Add(refCptr, new WeakReference(baseHandle, false));
54 /// Removes this instance of BaseHandle (C# base class) and native part from the mapping table.
56 /// <param name="baseHandle"> The instance of BaseHandle (C# base class)</param>
57 internal static void Unregister(BaseHandle baseHandle)
59 RefObject refObj = baseHandle.GetObjectPtr();
60 IntPtr refCptr = (IntPtr)RefObject.getCPtr(refObj);
62 if (Instance._controlMap.ContainsKey(refCptr))
64 Instance._controlMap.Remove(refCptr);
70 private static Registry Instance
76 instance = new Registry();
82 internal static BaseHandle GetManagedBaseHandleFromNativePtr(BaseHandle baseHandle)
84 RefObject refObj = baseHandle.GetObjectPtr();
85 IntPtr refObjectPtr = (IntPtr)RefObject.getCPtr(refObj);
87 // we store a dictionary of ref-obects (C++ land) to managed obects (C# land)
88 return GetManagedBaseHandleFromRefObject(refObjectPtr);
91 internal static BaseHandle GetManagedBaseHandleFromNativePtr(IntPtr cPtr)
93 IntPtr refObjectPtr = NDalicPINVOKE.GetRefObjectPtr(cPtr);
95 // we store a dictionary of ref-obects (C++ land) to managed obects (C# land)
96 return GetManagedBaseHandleFromRefObject(refObjectPtr);
99 internal static BaseHandle GetManagedBaseHandleFromRefObject(IntPtr refObjectPtr)
101 // we store a dictionary of ref-obects (C++ land) to managed obects (C# land)
102 WeakReference weakReference;
104 if (Instance._controlMap.TryGetValue(refObjectPtr, out weakReference))
106 BaseHandle ret = weakReference.Target as BaseHandle;