2 * Copyright(c) 2018 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 using System.Collections.Concurrent;
20 using System.Threading;
25 /// This is used to store a mapping between C++ base handle objects and it's C# instances.
28 internal sealed class Registry
30 private static readonly Registry registry = new Registry();
33 /// static initialization singleton
35 internal static Registry Instance
37 get { return registry; }
41 /// Given a C++ object, the dictionary allows us to find which C# object it belongs to.
42 /// By keeping the weak reference only, it will allow the object to be garbage collected.
44 private ConcurrentDictionary<IntPtr, WeakReference> _controlMap;
48 _controlMap = new ConcurrentDictionary<IntPtr, WeakReference>();
53 /// Stores the mapping between this instance of BaseHandle (C# base class) and native part.
55 /// <param name="baseHandle">The instance of BaseHandle (C# base class).</param>
56 internal static void Register(BaseHandle baseHandle)
58 // We store a pointer to the RefObject for the control
59 RefObject refObj = baseHandle.GetObjectPtr();
60 IntPtr refCptr = (IntPtr)RefObject.getCPtr(refObj);
62 RegistryCurrentThreadCheck();
64 if(Instance._controlMap.TryAdd(refCptr, new WeakReference(baseHandle, false)) != true)
66 NUILog.Debug("refCptr is already exist! OR something wrong!");
73 /// Removes this instance of BaseHandle (C# base class) and native part from the mapping table.
75 /// <param name="baseHandle"> The instance of BaseHandle (C# base class)</param>
76 internal static void Unregister(BaseHandle baseHandle)
78 RefObject refObj = baseHandle.GetObjectPtr();
79 IntPtr refCptr = (IntPtr)RefObject.getCPtr(refObj);
81 RegistryCurrentThreadCheck();
83 WeakReference removeTarget;
84 if(Instance._controlMap.TryRemove(refCptr, out removeTarget) != true)
86 NUILog.Debug("something wrong when removing refCptr!");
92 internal static BaseHandle GetManagedBaseHandleFromNativePtr(BaseHandle baseHandle)
94 RefObject refObj = baseHandle.GetObjectPtr();
95 IntPtr refObjectPtr = (IntPtr)RefObject.getCPtr(refObj);
97 // we store a dictionary of ref-obects (C++ land) to managed obects (C# land)
98 return GetManagedBaseHandleFromRefObject(refObjectPtr);
101 internal static BaseHandle GetManagedBaseHandleFromNativePtr(IntPtr cPtr)
103 IntPtr refObjectPtr = NDalicPINVOKE.GetRefObjectPtr(cPtr);
105 // we store a dictionary of ref-obects (C++ land) to managed obects (C# land)
106 return GetManagedBaseHandleFromRefObject(refObjectPtr);
109 internal static BaseHandle GetManagedBaseHandleFromRefObject(IntPtr refObjectPtr)
111 if (refObjectPtr == global::System.IntPtr.Zero)
113 NUILog.Debug("Registry refObjectPtr is NULL! This means bind native object is NULL!");
118 NUILog.Debug($"refObjectPtr=0x{refObjectPtr.ToInt64():X}");
121 RegistryCurrentThreadCheck();
123 // we store a dictionary of ref-obects (C++ land) to managed obects (C# land)
124 WeakReference weakReference;
126 if (Instance._controlMap.TryGetValue(refObjectPtr, out weakReference))
128 if(weakReference == null)
130 throw new System.InvalidOperationException("Error! NUI Registry weakReference should not be NULL!");
133 BaseHandle ret = weakReference.Target as BaseHandle;
142 private static Thread savedApplicationThread;
143 internal Thread SavedApplicationThread
147 return savedApplicationThread;
151 savedApplicationThread = value;
155 private static void RegistryCurrentThreadCheck()
157 if(savedApplicationThread == null)
159 Tizen.Log.Fatal("NUI", $"Error! maybe main thread is created by other process ");
162 int currentId = Thread.CurrentThread.ManagedThreadId;
163 int mainThreadId = savedApplicationThread.ManagedThreadId;
165 if(currentId != mainThreadId)
167 Tizen.Log.Fatal("NUI", $"Error! current thread({currentId}) which is NOT main thread({mainThreadId}) utilizes NUI object!");