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 throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
370 Dispose(DisposeTypes.Implicit);
374 Dispose(DisposeTypes.Explicit);
379 // Implicit call. user doesn't call Dispose(), so this object is added into DisposeQueue to be disposed automatically.
380 if (!isDisposeQueued)
382 isDisposeQueued = true;
383 DisposeQueue.Instance.Add(this);
387 // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
388 // TODO: set large fields to null.
393 /// Performs an action on this object with the given action name and attributes.
395 /// <param name="actionName">The command for the action.</param>
396 /// <param name="attributes">The list of attributes for the action.</param>
397 /// <returns>The action is performed by the object or not.</returns>
398 /// <since_tizen> 3 </since_tizen>
399 public bool DoAction(string actionName, PropertyMap attributes)
401 bool ret = Interop.BaseHandle.DoAction(swigCPtrCopy, actionName, PropertyMap.getCPtr(attributes));
402 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
407 /// Returns the type name for the Handle.<br />
408 /// Will return an empty string if the typename does not exist. This will happen for types that
409 /// have not registered with type-registry.
411 /// <returns>The type name. Empty string if the typename does not exist.</returns>
412 /// <since_tizen> 3 </since_tizen>
413 public string GetTypeName()
415 string ret = Interop.BaseHandle.GetTypeName(swigCPtrCopy);
416 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
421 /// Returns the type info for the Handle.<br />
423 /// <param name="info">The type information.</param>
424 /// <returns>True If get the type info.</returns>
425 /// <since_tizen> 3 </since_tizen>
426 public bool GetTypeInfo(Tizen.NUI.TypeInfo info)
428 bool ret = Interop.BaseHandle.GetTypeInfo(swigCPtrCopy, Tizen.NUI.TypeInfo.getCPtr(info));
429 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
434 /// Resets the handle.
436 /// <since_tizen> 3 </since_tizen>
439 Interop.BaseHandle.Reset(swigCPtrCopy);
440 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
444 /// To check the BaseHandle instance is equal or not.
446 /// <param name="rhs">The baseHandle instance.</param>
447 /// <returns>True If equal.</returns>
448 /// <since_tizen> 3 </since_tizen>
449 public bool EqualTo(BaseHandle rhs)
451 bool ret = Interop.BaseHandle.EqualTo(swigCPtrCopy, BaseHandle.getCPtr(rhs));
452 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
457 /// To check the BaseHandle instance is equal or not.
459 /// <param name="rhs">The baseHandle instance.</param>
460 /// <returns>True If not equal.</returns>
461 /// <since_tizen> 3 </since_tizen>
462 public bool NotEqualTo(BaseHandle rhs)
464 bool ret = Interop.BaseHandle.NotEqualTo(swigCPtrCopy, BaseHandle.getCPtr(rhs));
465 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
470 /// To check the BaseHandle instance has body or not.
472 /// <returns>True If the baseHandle instance has body.</returns>
473 /// <since_tizen> 3 </since_tizen>
474 public bool HasBody()
476 if (swigCPtrCopy.Handle == IntPtr.Zero)
481 if (disposed == true)
485 bool ret = Interop.BaseHandle.HasBody(swigCPtrCopy);
486 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
491 /// To check the BaseHandle instance is equal or not.
493 /// <param name="rhs">The baseHandle instance.</param>
494 /// <returns>True If equal.</returns>
495 /// <since_tizen> 3 </since_tizen>
496 public bool IsEqual(BaseHandle rhs)
498 if (disposed == true)
503 bool ret = Interop.BaseHandle.IsEqual(swigCPtrCopy, BaseHandle.getCPtr(rhs));
504 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
508 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(BaseHandle obj)
510 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtrCopy;
513 internal void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
515 PropertySet?.Invoke(this, new PropertyChangedEventArgs(propertyName));
521 /// <since_tizen> 3 </since_tizen>
522 protected virtual void Dispose(DisposeTypes type)
529 NUILog.Debug($"[Dispose] BaseHandle.Dispose({type}) START");
531 if (type == DisposeTypes.Explicit)
534 //Release your own managed resources here.
535 //You should release all of your own disposable objects here.
539 //Release your own unmanaged resources here.
540 //You should not access any managed member here except static instance.
541 //because the execution order of Finalizes is non-deterministic.
543 //Unreference this instance from Registry.
546 Registry.Unregister(this);
551 NUILog.Debug($"[Dispose] swigCMemOwn:{swigCMemOwn} debuggingCount:{debuggingCount} type:{GetType()} copyNativeHandle:{swigCPtrCopy.Handle.ToString("X8")}");
554 if (SwigCPtr.Handle != IntPtr.Zero)
559 ReleaseSwigCPtr(SwigCPtr);
561 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
563 if (swigCPtrCopy.Handle != global::System.IntPtr.Zero)
566 Interop.BaseHandle.DeleteBaseHandle(swigCPtrCopy);
567 swigCPtrCopy = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
572 if (null != Application.Current)
574 Application.Current.XamlResourceChanged -= OnResourcesChanged;
577 NUILog.Debug($"[Dispose] BaseHandle.Dispose({type}) END");
578 NUILog.Debug($"=============================");
582 /// Release swigCPtr.
584 /// <since_tizen> 6 </since_tizen>
585 /// This will not be public opened.
586 [EditorBrowsable(EditorBrowsableState.Never)]
587 protected virtual void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
592 /// Contains event arguments for the FocusChangeRequested event.
594 [Obsolete("Deprecated in API9; Will be removed in API11.")]
595 public class FocusRequestArgs : EventArgs
599 /// Gets or sets a value that indicates the starting focus state of the element for which a focus change is requested.
601 public bool Focus { get; set; }
604 /// Gets or sets a value that indicates the ending focus state of the element for which a focus change is requested.
606 public bool Result { get; set; }
609 internal global::System.Runtime.InteropServices.HandleRef SwigCPtr
613 if (swigCPtr.Handle == IntPtr.Zero)
615 throw new ObjectDisposedException(nameof(SwigCPtr), "Error! NUI's native dali object is already disposed. OR the native dali object handle of NUI becomes null!");
621 internal bool IsNativeHandleInvalid()
623 return swigCPtr.Handle == IntPtr.Zero;
629 [EditorBrowsable(EditorBrowsableState.Never)]
630 protected internal bool SwigCMemOwn => swigCMemOwn;
633 /// A flag to check if it is already disposed.
635 [EditorBrowsable(EditorBrowsableState.Never)]
636 protected internal bool Disposed => disposed;