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.Collections.Generic;
21 using System.Runtime.InteropServices;
22 using System.Threading;
27 /// This is used to store a mapping between C++ base handle objects and it's C# instances.
30 internal sealed class Registry
32 private static readonly Registry registry = new Registry();
35 /// static initialization singleton
37 internal static Registry Instance
39 get { return registry; }
43 /// Given a C++ object, the dictionary allows us to find which C# object it belongs to.
44 /// By keeping the weak reference only, it will allow the object to be garbage collected.
46 private ConcurrentDictionary<IntPtr, WeakReference> _controlMap;
50 _controlMap = new ConcurrentDictionary<IntPtr, WeakReference>();
55 /// Stores the mapping between this instance of BaseHandle (C# base class) and native part.
57 /// <param name="baseHandle">The instance of BaseHandle (C# base class).</param>
58 internal static void Register(BaseHandle baseHandle)
60 // We store a pointer to the RefObject for the control
61 IntPtr refCptr = Interop.BaseHandle.BaseHandle_GetObjectPtr(baseHandle.GetBaseHandleCPtrHandleRef);
63 RegistryCurrentThreadCheck();
65 if (Instance._controlMap.TryAdd(refCptr, new WeakReference(baseHandle, false)) != true)
67 NUILog.Debug("refCptr is already exist! OR something wrong!");
74 /// Removes this instance of BaseHandle (C# base class) and native part from the mapping table.
76 /// <param name="baseHandle"> The instance of BaseHandle (C# base class)</param>
77 internal static void Unregister(BaseHandle baseHandle)
79 IntPtr refCptr = Interop.BaseHandle.BaseHandle_GetObjectPtr(baseHandle.GetBaseHandleCPtrHandleRef);
81 RegistryCurrentThreadCheck();
83 if (Instance._controlMap.TryRemove(refCptr, out refe) != true)
85 NUILog.Debug("something wrong when removing refCptr!");
91 internal static BaseHandle GetManagedBaseHandleFromNativePtr(BaseHandle baseHandle)
93 IntPtr refObjectPtr = Interop.BaseHandle.BaseHandle_GetObjectPtr(baseHandle.GetBaseHandleCPtrHandleRef);
95 // we store a dictionary of ref-obects (C++ land) to managed obects (C# land)
96 return GetManagedBaseHandleFromRefObject(refObjectPtr);
99 internal static BaseHandle GetManagedBaseHandleFromNativePtr(IntPtr cPtr)
101 IntPtr refObjectPtr = Interop.RefObject.GetRefObjectPtr(cPtr);
103 // we store a dictionary of ref-obects (C++ land) to managed obects (C# land)
104 return GetManagedBaseHandleFromRefObject(refObjectPtr);
107 internal static BaseHandle GetManagedBaseHandleFromRefObject(IntPtr refObjectPtr)
109 if (refObjectPtr == global::System.IntPtr.Zero)
111 NUILog.Debug("Registry refObjectPtr is NULL! This means bind native object is NULL!");
116 NUILog.Debug($"refObjectPtr=0x{refObjectPtr.ToInt64():X}");
119 RegistryCurrentThreadCheck();
121 // we store a dictionary of ref-obects (C++ land) to managed obects (C# land)
122 WeakReference weakReference;
124 if (Instance._controlMap.TryGetValue(refObjectPtr, out weakReference))
126 if (weakReference == null)
128 throw new System.InvalidOperationException("Error! NUI Registry weakReference should not be NULL!");
131 BaseHandle ret = weakReference.Target as BaseHandle;
140 private static Thread savedApplicationThread;
141 internal Thread SavedApplicationThread
145 return savedApplicationThread;
149 savedApplicationThread = value;
153 private static void RegistryCurrentThreadCheck()
156 if (savedApplicationThread == null)
158 Tizen.Log.Fatal("NUI", $"Error! maybe main thread is created by other process\n");
161 int currentId = Thread.CurrentThread.ManagedThreadId;
162 int mainThreadId = savedApplicationThread.ManagedThreadId;
164 if (currentId != mainThreadId)
166 Tizen.Log.Fatal("NUI", $"Error! current thread({currentId}) which is NOT main thread({mainThreadId}) utilizes NUI object!");