2 * Copyright(c) 2021 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.
18 using System.ComponentModel;
19 using System.Runtime.CompilerServices;
20 using Tizen.NUI.Binding;
25 /// BaseHandle is a handle to an internal Dali resource.
27 /// <since_tizen> 3 </since_tizen>
28 public class BaseHandle : Element, global::System.IDisposable
33 /// <since_tizen> 3 </since_tizen>
34 [Obsolete("Deprecated in API9, Will be removed in API11, Please use SwigCMemOwn")]
35 [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1051:Do not declare visible instance fields", Justification = "<Pending>")]
36 protected bool swigCMemOwn;
39 /// A flag to check if it is already disposed.
41 /// <since_tizen> 3 </since_tizen>
42 [Obsolete("Deprecated in API9, Will be removed in API11, Please use Disposed")]
43 [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1051:Do not declare visible instance fields", Justification = "<Pending>")]
44 protected bool disposed = false;
46 private global::System.Runtime.InteropServices.HandleRef swigCPtr;
47 private global::System.Runtime.InteropServices.HandleRef swigCPtrCopy;
48 private bool registerMe;
50 //A Flag to check who called Dispose(). (By User or DisposeQueue)
51 private bool isDisposeQueued = false;
54 private static int debuggingCount = 0;
58 /// Create an instance of BaseHandle.
60 /// <since_tizen> 3 </since_tizen>
61 public BaseHandle() : this(Interop.BaseHandle.NewBaseHandle())
63 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
67 /// Create an instance of BaseHandle.
69 /// <param name="handle">The BaseHandle instance.</param>
70 /// <since_tizen> 3 </since_tizen>
71 public BaseHandle(BaseHandle handle) : this(Interop.BaseHandle.NewBaseHandle(BaseHandle.getCPtr(handle)))
73 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
76 internal BaseHandle(global::System.IntPtr cPtr, bool cMemoryOwn)
78 //to catch derived classes dali native exceptions
79 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
81 NUILog.Debug($"[Dispose] BaseHandle.contructor with cMemeryOwn:{cMemoryOwn} START");
83 registerMe = swigCMemOwn = cMemoryOwn;
84 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
85 // using copy constructor to create another native handle so Registry.Unregister works fine.
86 swigCPtrCopy = new global::System.Runtime.InteropServices.HandleRef(this, Interop.BaseHandle.NewBaseHandle(swigCPtr));
87 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
91 // Register this instance of BaseHandle in the registry.
92 Registry.Register(this);
96 NUILog.Debug($"[Dispose] type:{GetType()} copyNativeHandle:{swigCPtrCopy.Handle.ToString("X8")}");
99 if (this is BaseComponents.View view)
101 NUILog.Debug($"[Dispose] ID:{view.ID} Name:{view.Name} debuggingCount:{debuggingCount}");
103 NUILog.Debug($"[Dispose] BaseHandle.contructor with cMemeryOwn END");
104 NUILog.Debug($"=============================");
108 internal BaseHandle(global::System.IntPtr cPtr)
110 NUILog.Debug($"[Dispose] BaseHandle.contructor START");
112 registerMe = swigCMemOwn = true;
114 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
116 // using copy constructor to create another native handle so Registry.Unregister works fine.
117 swigCPtrCopy = new global::System.Runtime.InteropServices.HandleRef(this, Interop.BaseHandle.NewBaseHandle(SwigCPtr));
118 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
122 // Register this instance of BaseHandle in the registry.
123 Registry.Register(this);
127 NUILog.Debug($"[Dispose] type:{GetType()} copyNativeHandle:{swigCPtrCopy.Handle.ToString("X8")}");
130 if (this is BaseComponents.View view)
132 NUILog.Debug($"[Dispose] ID:{view.ID} Name:{view.Name} debuggingCount:{debuggingCount}");
134 NUILog.Debug($"[Dispose] BaseHandle.contructor END");
135 NUILog.Debug($"=============================");
143 /// <since_tizen> 3 </since_tizen>
144 // following this guide: https://docs.microsoft.com/ko-kr/dotnet/fundamentals/code-analysis/quality-rules/ca1063?view=vs-2019 (CA1063)
145 ~BaseHandle() => Dispose(false);
148 /// Event when a property is set.
150 /// <since_tizen> 5 </since_tizen>
151 /// <seealso cref="BindableObject.PropertyChanged"/>
152 [Obsolete("Deprecated in API9, Will be removed in API11, Please use BindableObject.PropertyChanged instead!")]
153 public event PropertyChangedEventHandler PropertySet;
155 internal global::System.Runtime.InteropServices.HandleRef GetBaseHandleCPtrHandleRef
164 /// Returns the bool value true to indicate that an operand is true and returns false otherwise.
166 /// <since_tizen> 3 </since_tizen>
167 public static bool operator true(BaseHandle handle)
169 // if the C# object is null, return false
170 if (BaseHandle.ReferenceEquals(handle, null))
174 // returns true if the handle has a body, false otherwise
175 return handle.HasBody();
179 /// Returns the bool false to indicate that an operand is false and returns true otherwise.
181 /// <since_tizen> 3 </since_tizen>
182 public static bool operator false(BaseHandle handle)
184 // if the C# object is null, return true
185 if (BaseHandle.ReferenceEquals(handle, null))
189 return !handle.HasBody();
193 /// Explicit conversion from Handle to bool.
195 /// <since_tizen> 3 </since_tizen>
196 public static explicit operator bool(BaseHandle handle)
198 // if the C# object is null, return false
199 if (BaseHandle.ReferenceEquals(handle, null))
203 // returns true if the handle has a body, false otherwise
204 return handle.HasBody();
208 /// Equality operator
210 /// <since_tizen> 3 </since_tizen>
211 public static bool operator ==(BaseHandle x, BaseHandle y)
213 // if the C# objects are the same return true
214 if (BaseHandle.ReferenceEquals(x, y))
218 if (!BaseHandle.ReferenceEquals(x, null) && !BaseHandle.ReferenceEquals(y, null))
220 // drop into native code to see if both handles point to the same body
224 if (BaseHandle.ReferenceEquals(x, null) && !BaseHandle.ReferenceEquals(y, null))
226 if (y.HasBody()) return false;
229 if (!BaseHandle.ReferenceEquals(x, null) && BaseHandle.ReferenceEquals(y, null))
231 if (x.HasBody()) return false;
239 /// Inequality operator. Returns Null if either operand is Null
241 /// <since_tizen> 3 </since_tizen>
242 public static bool operator !=(BaseHandle x, BaseHandle y)
248 /// Logical AND operator.<br />
249 /// It's possible when doing a operator this function (opBitwiseAnd) is never called due to short circuiting.<br />
251 /// <since_tizen> 3 </since_tizen>
252 public static BaseHandle operator &(BaseHandle x, BaseHandle y)
262 /// Logical OR operator for ||.<br />
263 /// It's possible when doing a || this function (opBitwiseOr) is never called due to short circuiting.<br />
265 /// <since_tizen> 3 </since_tizen>
266 public static BaseHandle operator |(BaseHandle x, BaseHandle y)
268 if (!BaseHandle.ReferenceEquals(x, null) || !BaseHandle.ReferenceEquals(y, null))
270 if (x != null && x.HasBody())
274 if (y != null && y.HasBody())
284 /// Logical ! operator
286 /// <since_tizen> 3 </since_tizen>
287 public static bool operator !(BaseHandle x)
289 // if the C# object is null, return true
290 if (BaseHandle.ReferenceEquals(x, null))
304 /// <param name="o">The object should be compared.</param>
305 /// <returns>True if equal.</returns>
306 /// <since_tizen> 5 </since_tizen>
307 public override bool Equals(object o)
309 return base.Equals(o);
313 /// Gets the hash code of this baseHandle.
315 /// <returns>The hash code.</returns>
316 /// <since_tizen> 5 </since_tizen>
317 public override int GetHashCode()
319 return base.GetHashCode();
325 /// <since_tizen> 3 </since_tizen>
326 public void Dispose()
330 Dispose(DisposeTypes.Implicit);
336 GC.SuppressFinalize(this);
340 /// Hidden API (Inhouse API).
344 /// Following the guide of https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose.
345 /// This will replace "protected virtual void Dispose(DisposeTypes type)" which is exactly same in functionality.
347 /// <param name="disposing">true in order to free managed objects</param>
348 // Protected implementation of Dispose pattern.
349 [EditorBrowsable(EditorBrowsableState.Never)]
350 protected virtual void Dispose(bool disposing)
359 // TODO: dispose managed state (managed objects).
360 // Explicit call. user calls Dispose()
362 //Throw exception if Dispose() is called in separate thread.
363 if (!Window.IsInstalled())
365 var process = global::System.Diagnostics.Process.GetCurrentProcess().Id;
366 var thread = global::System.Threading.Thread.CurrentThread.ManagedThreadId;
367 var me = this.GetType().FullName;
369 throw new global::System.InvalidOperationException("[NUI][BaseHandle] This API called from separate thread. This API must be called from MainThread. \n" +
370 $" process:{process} thread:{thread}, disposing:{disposing}, isDisposed:{this.disposed}, isDisposeQueued:{this.isDisposeQueued}, me:{me}\n");
375 Tizen.Log.Fatal("NUI", $"should not be here! (dead code) this will be removed!");
376 throw new global::System.Exception($"[NUI] should not be here! (dead code) this will be removed!");
377 Dispose(DisposeTypes.Implicit);
381 Dispose(DisposeTypes.Explicit);
386 // Implicit call. user doesn't call Dispose(), so this object is added into DisposeQueue to be disposed automatically.
387 if (!isDisposeQueued)
389 isDisposeQueued = true;
390 DisposeQueue.Instance.Add(this);
394 // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
395 // TODO: set large fields to null.
400 /// Performs an action on this object with the given action name and attributes.
402 /// <param name="actionName">The command for the action.</param>
403 /// <param name="attributes">The list of attributes for the action.</param>
404 /// <returns>The action is performed by the object or not.</returns>
405 /// <since_tizen> 3 </since_tizen>
406 public bool DoAction(string actionName, PropertyMap attributes)
408 bool ret = Interop.BaseHandle.DoAction(swigCPtrCopy, actionName, PropertyMap.getCPtr(attributes));
409 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
414 /// Returns the type name for the Handle.<br />
415 /// Will return an empty string if the typename does not exist. This will happen for types that
416 /// have not registered with type-registry.
418 /// <returns>The type name. Empty string if the typename does not exist.</returns>
419 /// <since_tizen> 3 </since_tizen>
420 public string GetTypeName()
422 string ret = Interop.BaseHandle.GetTypeName(swigCPtrCopy);
423 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
428 /// Returns the type info for the Handle.<br />
430 /// <param name="info">The type information.</param>
431 /// <returns>True If get the type info.</returns>
432 /// <since_tizen> 3 </since_tizen>
433 public bool GetTypeInfo(Tizen.NUI.TypeInfo info)
435 bool ret = Interop.BaseHandle.GetTypeInfo(swigCPtrCopy, Tizen.NUI.TypeInfo.getCPtr(info));
436 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
441 /// Resets the handle.
443 /// <since_tizen> 3 </since_tizen>
446 Interop.BaseHandle.Reset(swigCPtrCopy);
447 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
451 /// To check the BaseHandle instance is equal or not.
453 /// <param name="rhs">The baseHandle instance.</param>
454 /// <returns>True If equal.</returns>
455 /// <since_tizen> 3 </since_tizen>
456 public bool EqualTo(BaseHandle rhs)
458 bool ret = Interop.BaseHandle.EqualTo(swigCPtrCopy, BaseHandle.getCPtr(rhs));
459 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
464 /// To check the BaseHandle instance is equal or not.
466 /// <param name="rhs">The baseHandle instance.</param>
467 /// <returns>True If not equal.</returns>
468 /// <since_tizen> 3 </since_tizen>
469 public bool NotEqualTo(BaseHandle rhs)
471 bool ret = Interop.BaseHandle.NotEqualTo(swigCPtrCopy, BaseHandle.getCPtr(rhs));
472 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
477 /// To check the BaseHandle instance has body or not.
479 /// <returns>True If the baseHandle instance has body.</returns>
480 /// <since_tizen> 3 </since_tizen>
481 public bool HasBody()
483 if (swigCPtrCopy.Handle == IntPtr.Zero)
488 if (disposed == true)
492 bool ret = Interop.BaseHandle.HasBody(swigCPtrCopy);
493 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
498 /// To check the BaseHandle instance is equal or not.
500 /// <param name="rhs">The baseHandle instance.</param>
501 /// <returns>True If equal.</returns>
502 /// <since_tizen> 3 </since_tizen>
503 public bool IsEqual(BaseHandle rhs)
505 if (disposed == true)
510 bool ret = Interop.BaseHandle.IsEqual(swigCPtrCopy, BaseHandle.getCPtr(rhs));
511 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
515 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(BaseHandle obj)
517 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtrCopy;
520 internal void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
522 PropertySet?.Invoke(this, new PropertyChangedEventArgs(propertyName));
528 /// <since_tizen> 3 </since_tizen>
529 protected virtual void Dispose(DisposeTypes type)
536 NUILog.Debug($"[Dispose] BaseHandle.Dispose({type}) START");
538 if (type == DisposeTypes.Explicit)
541 //Release your own managed resources here.
542 //You should release all of your own disposable objects here.
546 //Release your own unmanaged resources here.
547 //You should not access any managed member here except static instance.
548 //because the execution order of Finalizes is non-deterministic.
550 //Unreference this instance from Registry.
553 Registry.Unregister(this);
558 NUILog.Debug($"[Dispose] swigCMemOwn:{swigCMemOwn} debuggingCount:{debuggingCount} type:{GetType()} copyNativeHandle:{swigCPtrCopy.Handle.ToString("X8")}");
560 // this is temporary test code. will be removed laster
562 if (swigCPtr.Handle != IntPtr.Zero && swigCPtrCopy.Handle != IntPtr.Zero)
564 var process = global::System.Diagnostics.Process.GetCurrentProcess().Id;
565 var thread = global::System.Threading.Thread.CurrentThread.ManagedThreadId;
566 var me = this.GetType().FullName;
567 var daliId = "unknown";
568 var hash = this.GetType().GetHashCode();
569 var name = "unknown";
571 if (this is BaseComponents.View)
573 daliId = Interop.Actor.GetId(swigCPtrCopy).ToString();
574 name = Interop.Actor.GetName(swigCPtrCopy);
575 BaseObject tmp = new BaseObject(Interop.BaseHandle.GetBaseObject(swigCPtrCopy), false);
576 var refCnt = tmp.ReferenceCount();
580 Log.Error("NUI", $"[ERR] reference count is over than 2. Could be a memory leak. Need to check! \n" +
581 $" process:{process} thread:{thread}, isDisposed:{this.disposed}, isDisposeQueued:{this.isDisposeQueued}, me:{me} \n" +
582 $" disposeType:{type}, name:{name}, daliID:{daliId}, hash:{hash}, refCnt:{refCnt}");
588 if (SwigCPtr.Handle != IntPtr.Zero)
593 ReleaseSwigCPtr(SwigCPtr);
595 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
599 var me = this.GetType().FullName;
600 Log.Error("NUI", $"[ERR] SwigCPtr is NULL, need to check! me:{me}");
603 if (swigCPtrCopy.Handle != global::System.IntPtr.Zero)
606 Interop.BaseHandle.DeleteBaseHandle(swigCPtrCopy);
607 swigCPtrCopy = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
611 var me = this.GetType().FullName;
612 Log.Error("NUI", $"[ERR] swigCPtrCopy is NULL, need to check! me:{me}");
617 if (null != Application.Current)
619 Application.Current.XamlResourceChanged -= OnResourcesChanged;
622 NUILog.Debug($"[Dispose] BaseHandle.Dispose({type}) END");
623 NUILog.Debug($"=============================");
627 /// Release swigCPtr.
629 /// <since_tizen> 6 </since_tizen>
630 /// This will not be public opened.
631 [EditorBrowsable(EditorBrowsableState.Never)]
632 protected virtual void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
637 /// Contains event arguments for the FocusChangeRequested event.
639 [Obsolete("Deprecated in API9; Will be removed in API11.")]
640 public class FocusRequestArgs : EventArgs
644 /// Gets or sets a value that indicates the starting focus state of the element for which a focus change is requested.
646 public bool Focus { get; set; }
649 /// Gets or sets a value that indicates the ending focus state of the element for which a focus change is requested.
651 public bool Result { get; set; }
654 internal global::System.Runtime.InteropServices.HandleRef SwigCPtr
658 if (swigCPtr.Handle == IntPtr.Zero)
660 var process = global::System.Diagnostics.Process.GetCurrentProcess().Id;
661 var thread = global::System.Threading.Thread.CurrentThread.ManagedThreadId;
662 var me = this.GetType().FullName;
664 throw new ObjectDisposedException(nameof(SwigCPtr), $"Error! NUI's native dali object is already disposed. " +
665 $"OR the native dali object handle of NUI becomes null! \n" +
666 $" process:{process} thread:{thread}, isDisposed:{this.disposed}, isDisposeQueued:{this.isDisposeQueued}, me:{me}\n");
672 internal bool IsNativeHandleInvalid()
674 return swigCPtr.Handle == IntPtr.Zero;
680 [EditorBrowsable(EditorBrowsableState.Never)]
681 protected internal bool SwigCMemOwn => swigCMemOwn;
684 /// A flag to check if it is already disposed.
686 [EditorBrowsable(EditorBrowsableState.Never)]
687 protected internal bool Disposed => disposed;
690 /// A flag to check if it is disposed by DisposeQueue.
692 [EditorBrowsable(EditorBrowsableState.Never)]
693 protected internal bool IsDisposeQueued => isDisposeQueued;