2 * Copyright(c) 2022 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, Use SwigCMemOwn")]
35 [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1051:Do not declare visible instance fields", Justification = "<Pending>")]
36 protected bool swigCMemOwn;
39 /// The flag to check if it is already disposed of.
41 /// <since_tizen> 3 </since_tizen>
42 [Obsolete("Deprecated in API9, will be removed in API11, 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 //The flag to check who called Dispose(). (By User or DisposeQueue)
51 private bool isDisposeQueued = false;
54 /// Create an instance of BaseHandle.
56 /// <since_tizen> 3 </since_tizen>
57 public BaseHandle() : this(Interop.BaseHandle.NewBaseHandle())
59 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
63 /// Create an instance of BaseHandle.
65 /// <param name="handle">The BaseHandle instance.</param>
66 /// <since_tizen> 3 </since_tizen>
67 public BaseHandle(BaseHandle handle) : this(Interop.BaseHandle.NewBaseHandle(BaseHandle.getCPtr(handle)))
69 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
72 internal BaseHandle(global::System.IntPtr cPtr, bool cMemoryOwn)
74 //to catch derived classes dali native exceptions
75 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
77 registerMe = swigCMemOwn = cMemoryOwn;
78 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
79 // using copy constructor to create another native handle so Registry.Unregister works fine.
80 swigCPtrCopy = new global::System.Runtime.InteropServices.HandleRef(this, Interop.BaseHandle.NewBaseHandle(swigCPtr));
81 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
86 // Register this instance of BaseHandle in the registry.
87 Registry.Register(this);
91 internal BaseHandle(global::System.IntPtr cPtr)
93 registerMe = swigCMemOwn = true;
95 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
97 // using copy constructor to create another native handle so Registry.Unregister works fine.
98 swigCPtrCopy = new global::System.Runtime.InteropServices.HandleRef(this, Interop.BaseHandle.NewBaseHandle(SwigCPtr));
99 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
103 // Register this instance of BaseHandle in the registry.
104 Registry.Register(this);
111 /// <since_tizen> 3 </since_tizen>
112 // following this guide: https://docs.microsoft.com/ko-kr/dotnet/fundamentals/code-analysis/quality-rules/ca1063?view=vs-2019 (CA1063)
113 ~BaseHandle() => Dispose(false);
116 /// Event when a property is set.
118 /// <since_tizen> 5 </since_tizen>
119 /// <seealso cref="BindableObject.PropertyChanged"/>
120 [Obsolete("Deprecated in API9, will be removed in API11, Use BindableObject.PropertyChanged instead!")]
121 public event PropertyChangedEventHandler PropertySet;
123 internal global::System.Runtime.InteropServices.HandleRef GetBaseHandleCPtrHandleRef
132 /// Returns the bool value true to indicate that an operand is true and returns false otherwise.
134 /// <since_tizen> 3 </since_tizen>
135 public static bool operator true(BaseHandle handle)
137 // if the C# object is null, return false
138 if (BaseHandle.ReferenceEquals(handle, null))
142 // returns true if the handle has a body, false otherwise
143 return handle.HasBody();
147 /// Returns the bool false to indicate that an operand is false and returns true otherwise.
149 /// <since_tizen> 3 </since_tizen>
150 public static bool operator false(BaseHandle handle)
152 // if the C# object is null, return true
153 if (BaseHandle.ReferenceEquals(handle, null))
157 return !handle.HasBody();
161 /// Explicit conversion from Handle to bool.
163 /// <since_tizen> 3 </since_tizen>
164 public static explicit operator bool(BaseHandle handle)
166 // if the C# object is null, return false
167 if (BaseHandle.ReferenceEquals(handle, null))
171 // returns true if the handle has a body, false otherwise
172 return handle.HasBody();
176 /// Equality operator
178 /// <since_tizen> 3 </since_tizen>
179 public static bool operator ==(BaseHandle x, BaseHandle y)
181 // if the C# objects are the same return true
182 if (BaseHandle.ReferenceEquals(x, y))
186 if (!BaseHandle.ReferenceEquals(x, null) && !BaseHandle.ReferenceEquals(y, null))
188 // drop into native code to see if both handles point to the same body
192 if (BaseHandle.ReferenceEquals(x, null) && !BaseHandle.ReferenceEquals(y, null))
194 if (y.HasBody()) return false;
197 if (!BaseHandle.ReferenceEquals(x, null) && BaseHandle.ReferenceEquals(y, null))
199 if (x.HasBody()) return false;
207 /// Inequality operator. Returns Null if either operand is Null
209 /// <since_tizen> 3 </since_tizen>
210 public static bool operator !=(BaseHandle x, BaseHandle y)
216 /// Logical AND operator.<br />
217 /// It's possible when doing a operator this function (opBitwiseAnd) is never called due to short circuiting.<br />
219 /// <since_tizen> 3 </since_tizen>
220 public static BaseHandle operator &(BaseHandle x, BaseHandle y)
230 /// Logical OR operator for ||.<br />
231 /// It's possible when doing a || this function (opBitwiseOr) is never called due to short circuiting.<br />
233 /// <since_tizen> 3 </since_tizen>
234 public static BaseHandle operator |(BaseHandle x, BaseHandle y)
236 if (!BaseHandle.ReferenceEquals(x, null) || !BaseHandle.ReferenceEquals(y, null))
238 if (x != null && x.HasBody())
242 if (y != null && y.HasBody())
252 /// Logical ! operator
254 /// <since_tizen> 3 </since_tizen>
255 public static bool operator !(BaseHandle x)
257 // if the C# object is null, return true
258 if (BaseHandle.ReferenceEquals(x, null))
272 /// <param name="o">The object should be compared.</param>
273 /// <returns>True if equal.</returns>
274 /// <since_tizen> 5 </since_tizen>
275 public override bool Equals(object o)
277 return base.Equals(o);
281 /// Gets the hash code of this baseHandle.
283 /// <returns>The hash code.</returns>
284 /// <since_tizen> 5 </since_tizen>
285 public override int GetHashCode()
287 return base.GetHashCode();
293 /// <since_tizen> 3 </since_tizen>
294 public void Dispose()
298 Dispose(DisposeTypes.Implicit);
304 GC.SuppressFinalize(this);
308 /// Hidden API (Inhouse API).
312 /// Following the guide of https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose.
313 /// This will replace "protected virtual void Dispose(DisposeTypes type)" which is exactly same in functionality.
315 /// <param name="disposing">true in order to free managed objects</param>
316 // Protected implementation of Dispose pattern.
317 [EditorBrowsable(EditorBrowsableState.Never)]
318 protected virtual void Dispose(bool disposing)
327 // TODO: dispose managed state (managed objects).
328 // Explicit call. user calls Dispose()
330 //Throw exception if Dispose() is called in separate thread.
331 if (!Window.IsInstalled())
333 var process = global::System.Diagnostics.Process.GetCurrentProcess().Id;
334 var thread = global::System.Threading.Thread.CurrentThread.ManagedThreadId;
335 var me = this.GetType().FullName;
337 throw new global::System.InvalidOperationException("[NUI][BaseHandle] This API called from separate thread. This API must be called from MainThread. \n" +
338 $" process:{process} thread:{thread}, disposing:{disposing}, isDisposed:{this.disposed}, isDisposeQueued:{this.isDisposeQueued}, me:{me}\n");
341 Dispose(DisposeTypes.Explicit);
345 // Implicit call. user doesn't call Dispose(), so this object is added into DisposeQueue to be disposed automatically.
346 if (!isDisposeQueued)
348 isDisposeQueued = true;
349 DisposeQueue.Instance.Add(this);
353 // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
354 // TODO: set large fields to null.
359 /// Performs an action on this object with the given action name and attributes.
361 /// <param name="actionName">The command for the action.</param>
362 /// <param name="attributes">The list of attributes for the action.</param>
363 /// <returns>The action is performed by the object or not.</returns>
364 /// <since_tizen> 3 </since_tizen>
365 public bool DoAction(string actionName, PropertyMap attributes)
367 bool ret = Interop.BaseHandle.DoAction(swigCPtrCopy, actionName, PropertyMap.getCPtr(attributes));
368 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
373 /// Returns the type name for the Handle.<br />
374 /// Will return an empty string if the typename does not exist. This will happen for types that
375 /// have not registered with type-registry.
377 /// <returns>The type name. Empty string if the typename does not exist.</returns>
378 /// <since_tizen> 3 </since_tizen>
379 public string GetTypeName()
381 string ret = Interop.BaseHandle.GetTypeName(swigCPtrCopy);
382 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
387 /// Returns the type info for the Handle.<br />
389 /// <param name="info">The type information.</param>
390 /// <returns>True If get the type info.</returns>
391 /// <since_tizen> 3 </since_tizen>
392 public bool GetTypeInfo(Tizen.NUI.TypeInfo info)
394 bool ret = Interop.BaseHandle.GetTypeInfo(swigCPtrCopy, Tizen.NUI.TypeInfo.getCPtr(info));
395 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
400 /// Resets the handle.
402 /// <since_tizen> 3 </since_tizen>
405 Interop.BaseHandle.Reset(swigCPtrCopy);
406 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
410 /// To check the BaseHandle instance is equal or not.
412 /// <param name="rhs">The baseHandle instance.</param>
413 /// <returns>True If equal.</returns>
414 /// <since_tizen> 3 </since_tizen>
415 public bool EqualTo(BaseHandle rhs)
417 bool ret = Interop.BaseHandle.EqualTo(swigCPtrCopy, BaseHandle.getCPtr(rhs));
418 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
423 /// To check the BaseHandle instance is equal or not.
425 /// <param name="rhs">The baseHandle instance.</param>
426 /// <returns>True If not equal.</returns>
427 /// <since_tizen> 3 </since_tizen>
428 public bool NotEqualTo(BaseHandle rhs)
430 bool ret = Interop.BaseHandle.NotEqualTo(swigCPtrCopy, BaseHandle.getCPtr(rhs));
431 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
436 /// To check the BaseHandle instance has body or not.
438 /// <returns>True If the baseHandle instance has body.</returns>
439 /// <since_tizen> 3 </since_tizen>
440 public bool HasBody()
442 if (swigCPtrCopy.Handle == IntPtr.Zero)
447 if (disposed == true)
451 bool ret = Interop.BaseHandle.HasBody(swigCPtrCopy);
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 equal.</returns>
461 /// <since_tizen> 3 </since_tizen>
462 public bool IsEqual(BaseHandle rhs)
464 if (disposed == true)
469 bool ret = Interop.BaseHandle.IsEqual(swigCPtrCopy, BaseHandle.getCPtr(rhs));
470 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
474 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(BaseHandle obj)
476 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtrCopy;
479 internal void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
481 PropertySet?.Invoke(this, new PropertyChangedEventArgs(propertyName));
487 /// <since_tizen> 3 </since_tizen>
488 protected virtual void Dispose(DisposeTypes type)
495 if (type == DisposeTypes.Explicit)
498 //Release your own managed resources here.
499 //You should release all of your own disposable objects here.
503 //Release your own unmanaged resources here.
504 //You should not access any managed member here except static instance.
505 //because the execution order of Finalizes is non-deterministic.
507 //Unreference this instance from Registry.
510 Registry.Unregister(this);
513 if (SwigCPtr.Handle != IntPtr.Zero)
518 ReleaseSwigCPtr(SwigCPtr);
520 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
524 var me = this.GetType().FullName;
525 Log.Error("NUI", $"[ERR] SwigCPtr is NULL, need to check! me:{me}");
528 if (swigCPtrCopy.Handle != global::System.IntPtr.Zero)
531 Interop.BaseHandle.DeleteBaseHandle(swigCPtrCopy);
532 swigCPtrCopy = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
536 var me = this.GetType().FullName;
537 Log.Error("NUI", $"[ERR] swigCPtrCopy is NULL, need to check! me:{me}");
544 /// Release swigCPtr.
546 /// <since_tizen> 6 </since_tizen>
547 /// This will not be public opened.
548 [EditorBrowsable(EditorBrowsableState.Never)]
549 protected virtual void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
554 /// Contains event arguments for the FocusChangeRequested event.
556 [Obsolete("Deprecated in API9; Will be removed in API11.")]
557 public class FocusRequestArgs : EventArgs
561 /// Gets or sets a value that indicates the starting focus state of the element for which a focus change is requested.
563 public bool Focus { get; set; }
566 /// Gets or sets a value that indicates the ending focus state of the element for which a focus change is requested.
568 public bool Result { get; set; }
571 internal global::System.Runtime.InteropServices.HandleRef SwigCPtr
575 if (swigCPtr.Handle == IntPtr.Zero)
577 var me = this.GetType().FullName;
578 Log.Fatal("NUI", $"Error! swigCPtr.Handle == IntPtr.Zero ! please check! me:{me}\n");
584 internal bool IsNativeHandleInvalid()
586 return swigCPtr.Handle == IntPtr.Zero;
592 [EditorBrowsable(EditorBrowsableState.Never)]
593 protected internal bool SwigCMemOwn => swigCMemOwn;
596 /// The flag to check if it is already disposed of.
598 [EditorBrowsable(EditorBrowsableState.Never)]
599 protected internal bool Disposed => disposed;
602 /// The flag to check if it is disposed by DisposeQueue.
604 [EditorBrowsable(EditorBrowsableState.Never)]
605 protected internal bool IsDisposeQueued => isDisposeQueued;