using System.Reflection;
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using Tizen.NUI.BaseComponents;
namespace Tizen.NUI
{
///
/// This is used to store a mapping between C++ base handle objects and it's C# instances.
///
///
internal sealed class Registry
{
///
/// The registry is a singleton.
///
private static Registry instance = null;
///
/// Given a C++ object, the dictionary allows us to find which C# object it belongs to.
/// By keeping the weak reference only, it will allow the object to be garbage collected.
///
private Dictionary _controlMap;
private Registry()
{
_controlMap = new Dictionary();
}
///
/// Stores the mapping between this instance of BaseHandle (C# base class) and native part.
///
/// The instance of BaseHandle (C# base class).
internal static void Register(BaseHandle baseHandle)
{
// We store a pointer to the RefObject for the control
RefObject refObj = baseHandle.GetObjectPtr();
IntPtr refCptr = (IntPtr)RefObject.getCPtr(refObj);
NUILog.Debug("Storing ref object cptr in control map Hex: {0:X}" + refCptr);
if (!Instance._controlMap.ContainsKey(refCptr))
{
Instance._controlMap.Add(refCptr, new WeakReference(baseHandle, false));
}
return;
}
///
/// Removes this instance of BaseHandle (C# base class) and native part from the mapping table.
///
/// The instance of BaseHandle (C# base class)
internal static void Unregister(BaseHandle baseHandle)
{
RefObject refObj = baseHandle.GetObjectPtr();
IntPtr refCptr = (IntPtr)RefObject.getCPtr(refObj);
if (Instance._controlMap.ContainsKey(refCptr))
{
Instance._controlMap.Remove(refCptr);
}
return;
}
private static Registry Instance
{
get
{
if (instance == null)
{
instance = new Registry();
}
return instance;
}
}
internal static BaseHandle GetManagedBaseHandleFromNativePtr(BaseHandle baseHandle)
{
RefObject refObj = baseHandle.GetObjectPtr();
IntPtr refObjectPtr = (IntPtr)RefObject.getCPtr(refObj);
// we store a dictionary of ref-obects (C++ land) to managed obects (C# land)
return GetManagedBaseHandleFromRefObject(refObjectPtr);
}
internal static BaseHandle GetManagedBaseHandleFromNativePtr(IntPtr cPtr)
{
IntPtr refObjectPtr = NDalicPINVOKE.GetRefObjectPtr(cPtr);
// we store a dictionary of ref-obects (C++ land) to managed obects (C# land)
return GetManagedBaseHandleFromRefObject(refObjectPtr);
}
internal static BaseHandle GetManagedBaseHandleFromRefObject(IntPtr refObjectPtr)
{
// we store a dictionary of ref-obects (C++ land) to managed obects (C# land)
WeakReference weakReference;
if (Instance._controlMap.TryGetValue(refObjectPtr, out weakReference))
{
BaseHandle ret = weakReference?.Target as BaseHandle;
return ret;
}
else
{
return null;
}
}
}
}