2 * Copyright(c) 2017 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;
21 using Tizen.NUI.Binding.Internals;
27 /// BaseHandle is a handle to an internal Dali resource.
29 /// <since_tizen> 3 </since_tizen>
30 public class BaseHandle : Element, global::System.IDisposable
35 /// <since_tizen> 3 </since_tizen>
36 [Obsolete("Deprecated in API9, Will be removed in API11, Please use SwigCMemOwn")]
37 [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1051:Do not declare visible instance fields", Justification = "<Pending>")]
38 protected bool swigCMemOwn;
41 /// A flag to check if it is already disposed.
43 /// <since_tizen> 3 </since_tizen>
44 [Obsolete("Deprecated in API9, Will be removed in API11, Please use Disposed")]
45 [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1051:Do not declare visible instance fields", Justification = "<Pending>")]
46 protected bool disposed = false;
48 private global::System.Runtime.InteropServices.HandleRef swigCPtr;
49 private global::System.Runtime.InteropServices.HandleRef swigCPtrCopy;
50 private bool registerMe;
52 //A Flag to check who called Dispose(). (By User or DisposeQueue)
53 private bool isDisposeQueued = false;
56 /// Create an instance of BaseHandle.
58 /// <since_tizen> 3 </since_tizen>
59 public BaseHandle() : this(Interop.BaseHandle.NewBaseHandle())
61 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
65 /// Create an instance of BaseHandle.
67 /// <param name="handle">The BaseHandle instance.</param>
68 /// <since_tizen> 3 </since_tizen>
69 public BaseHandle(BaseHandle handle) : this(Interop.BaseHandle.NewBaseHandle(BaseHandle.getCPtr(handle)))
71 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
74 internal BaseHandle(global::System.IntPtr cPtr, bool cMemoryOwn)
76 //to catch derived classes dali native exceptions
77 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
79 registerMe = swigCMemOwn = cMemoryOwn;
80 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
81 // using copy constructor to create another native handle so Registry.Unregister works fine.
82 swigCPtrCopy = new global::System.Runtime.InteropServices.HandleRef(this, Interop.BaseHandle.NewBaseHandle(swigCPtr));
83 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
88 // Register this instance of BaseHandle in the registry.
89 Registry.Register(this);
93 internal BaseHandle(global::System.IntPtr cPtr)
95 registerMe = swigCMemOwn = true;
97 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
99 // using copy constructor to create another native handle so Registry.Unregister works fine.
100 swigCPtrCopy = new global::System.Runtime.InteropServices.HandleRef(this, Interop.BaseHandle.NewBaseHandle(SwigCPtr));
101 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
105 // Register this instance of BaseHandle in the registry.
106 Registry.Register(this);
113 /// <since_tizen> 3 </since_tizen>
114 ~BaseHandle() => Dispose(false);
117 /// Event when a property is set.
119 /// <since_tizen> 5 </since_tizen>
120 public event PropertyChangedEventHandler PropertySet;
122 internal global::System.Runtime.InteropServices.HandleRef GetBaseHandleCPtrHandleRef
131 /// Returns the bool value true to indicate that an operand is true and returns false otherwise.
133 /// <since_tizen> 3 </since_tizen>
134 public static bool operator true(BaseHandle handle)
136 // if the C# object is null, return false
137 if (BaseHandle.ReferenceEquals(handle, null))
141 // returns true if the handle has a body, false otherwise
142 return handle.HasBody();
146 /// Returns the bool false to indicate that an operand is false and returns true otherwise.
148 /// <since_tizen> 3 </since_tizen>
149 public static bool operator false(BaseHandle handle)
151 // if the C# object is null, return true
152 if (BaseHandle.ReferenceEquals(handle, null))
156 return !handle.HasBody();
160 /// Explicit conversion from Handle to bool.
162 /// <since_tizen> 3 </since_tizen>
163 public static explicit operator bool(BaseHandle handle)
165 // if the C# object is null, return false
166 if (BaseHandle.ReferenceEquals(handle, null))
170 // returns true if the handle has a body, false otherwise
171 return handle.HasBody();
175 /// Equality operator
177 /// <since_tizen> 3 </since_tizen>
178 public static bool operator ==(BaseHandle x, BaseHandle y)
180 // if the C# objects are the same return true
181 if (BaseHandle.ReferenceEquals(x, y))
185 if (!BaseHandle.ReferenceEquals(x, null) && !BaseHandle.ReferenceEquals(y, null))
187 // drop into native code to see if both handles point to the same body
191 if (BaseHandle.ReferenceEquals(x, null) && !BaseHandle.ReferenceEquals(y, null))
193 if (y.HasBody()) return false;
196 if (!BaseHandle.ReferenceEquals(x, null) && BaseHandle.ReferenceEquals(y, null))
198 if (x.HasBody()) return false;
206 /// Inequality operator. Returns Null if either operand is Null
208 /// <since_tizen> 3 </since_tizen>
209 public static bool operator !=(BaseHandle x, BaseHandle y)
215 /// Logical AND operator.<br />
216 /// It's possible when doing a operator this function (opBitwiseAnd) is never called due to short circuiting.<br />
218 /// <since_tizen> 3 </since_tizen>
219 public static BaseHandle operator &(BaseHandle x, BaseHandle y)
229 /// Logical OR operator for ||.<br />
230 /// It's possible when doing a || this function (opBitwiseOr) is never called due to short circuiting.<br />
232 /// <since_tizen> 3 </since_tizen>
233 public static BaseHandle operator |(BaseHandle x, BaseHandle y)
235 if (!BaseHandle.ReferenceEquals(x, null) || !BaseHandle.ReferenceEquals(y, null))
237 if (x != null && x.HasBody())
241 if (y != null && y.HasBody())
251 /// Logical ! operator
253 /// <since_tizen> 3 </since_tizen>
254 public static bool operator !(BaseHandle x)
256 // if the C# object is null, return true
257 if (BaseHandle.ReferenceEquals(x, null))
271 /// <param name="o">The object should be compared.</param>
272 /// <returns>True if equal.</returns>
273 /// <since_tizen> 5 </since_tizen>
274 public override bool Equals(object o)
276 return base.Equals(o);
280 /// Gets the the hash code of this baseHandle.
282 /// <returns>The hash code.</returns>
283 /// <since_tizen> 5 </since_tizen>
284 public override int GetHashCode()
286 return base.GetHashCode();
292 /// <since_tizen> 3 </since_tizen>
293 public void Dispose()
296 System.GC.SuppressFinalize(this);
300 /// Hidden API (Inhouse API).
304 /// Following the guide of https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose.
305 /// This will replace "protected virtual void Dispose(DisposeTypes type)" which is exactly same in functionality.
307 /// <param name="disposing">true in order to free managed objects</param>
308 // Protected implementation of Dispose pattern.
309 [EditorBrowsable(EditorBrowsableState.Never)]
310 protected virtual void Dispose(bool disposing)
319 // TODO: dispose managed state (managed objects).
320 // Explicit call. user calls Dispose()
322 //Throw excpetion if Dispose() is called in separate thread.
323 if (!Window.IsInstalled())
325 throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
330 Dispose(DisposeTypes.Implicit);
334 Dispose(DisposeTypes.Explicit);
339 // Implicit call. user doesn't call Dispose(), so this object is added into DisposeQueue to be disposed automatically.
340 if (!isDisposeQueued)
342 isDisposeQueued = true;
343 DisposeQueue.Instance.Add(this);
347 // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
348 // TODO: set large fields to null.
353 /// Performs an action on this object with the given action name and attributes.
355 /// <param name="actionName">The command for the action.</param>
356 /// <param name="attributes">The list of attributes for the action.</param>
357 /// <returns>The action is performed by the object or not.</returns>
358 /// <since_tizen> 3 </since_tizen>
359 public bool DoAction(string actionName, PropertyMap attributes)
361 bool ret = Interop.BaseHandle.DoAction(swigCPtrCopy, actionName, PropertyMap.getCPtr(attributes));
362 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
367 /// Returns the type name for the Handle.<br />
368 /// Will return an empty string if the typename does not exist. This will happen for types that
369 /// have not registered with type-registry.
371 /// <returns>The type name. Empty string if the typename does not exist.</returns>
372 /// <since_tizen> 3 </since_tizen>
373 public string GetTypeName()
375 string ret = Interop.BaseHandle.GetTypeName(swigCPtrCopy);
376 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
381 /// Returns the type info for the Handle.<br />
383 /// <param name="info">The type information.</param>
384 /// <returns>True If get the type info.</returns>
385 /// <since_tizen> 3 </since_tizen>
386 public bool GetTypeInfo(Tizen.NUI.TypeInfo info)
388 bool ret = Interop.BaseHandle.GetTypeInfo(swigCPtrCopy, Tizen.NUI.TypeInfo.getCPtr(info));
389 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
394 /// Resets the handle.
396 /// <since_tizen> 3 </since_tizen>
399 Interop.BaseHandle.Reset(swigCPtrCopy);
400 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
404 /// To check the BaseHandle instance is equal or not.
406 /// <param name="rhs">The baseHandle instance.</param>
407 /// <returns>True If equal.</returns>
408 /// <since_tizen> 3 </since_tizen>
409 public bool EqualTo(BaseHandle rhs)
411 bool ret = Interop.BaseHandle.EqualTo(swigCPtrCopy, BaseHandle.getCPtr(rhs));
412 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
417 /// To check the BaseHandle instance is equal or not.
419 /// <param name="rhs">The baseHandle instance.</param>
420 /// <returns>True If not equal.</returns>
421 /// <since_tizen> 3 </since_tizen>
422 public bool NotEqualTo(BaseHandle rhs)
424 bool ret = Interop.BaseHandle.NotEqualTo(swigCPtrCopy, BaseHandle.getCPtr(rhs));
425 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
430 /// To check the BaseHandle instance has body or not.
432 /// <returns>True If the baseHandle instance has body.</returns>
433 /// <since_tizen> 3 </since_tizen>
434 public bool HasBody()
436 if (swigCPtrCopy.Handle == IntPtr.Zero)
441 if (disposed == true)
445 bool ret = Interop.BaseHandle.HasBody(swigCPtrCopy);
446 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 IsEqual(BaseHandle rhs)
458 if (disposed == true)
463 bool ret = Interop.BaseHandle.IsEqual(swigCPtrCopy, BaseHandle.getCPtr(rhs));
464 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
468 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(BaseHandle obj)
470 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtrCopy;
473 internal void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
475 PropertySet?.Invoke(this, new PropertyChangedEventArgs(propertyName));
481 /// <since_tizen> 3 </since_tizen>
482 protected virtual void Dispose(DisposeTypes type)
489 if (type == DisposeTypes.Explicit)
492 //Release your own managed resources here.
493 //You should release all of your own disposable objects here.
497 //Release your own unmanaged resources here.
498 //You should not access any managed member here except static instance.
499 //because the execution order of Finalizes is non-deterministic.
501 //Unreference this instance from Registry.
504 Registry.Unregister(this);
507 if (SwigCPtr.Handle != IntPtr.Zero)
512 ReleaseSwigCPtr(SwigCPtr);
514 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
516 if (swigCPtrCopy.Handle != global::System.IntPtr.Zero)
519 Interop.BaseHandle.DeleteBaseHandle(swigCPtrCopy);
520 swigCPtrCopy = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
527 /// Release swigCPtr.
529 /// <since_tizen> 6 </since_tizen>
530 /// This will not be public opened.
531 [EditorBrowsable(EditorBrowsableState.Never)]
532 protected virtual void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
537 /// Contains event arguments for the FocusChangeRequested event.
539 [Obsolete("Deprecated in API9; Will be removed in API11.")]
540 public class FocusRequestArgs : EventArgs
544 /// Gets or sets a value that indicates the starting focus state of the element for which a focus change is requested.
546 public bool Focus { get; set; }
549 /// Gets or sets a value that indicates the ending focus state of the element for which a focus change is requested.
551 public bool Result { get; set; }
554 internal global::System.Runtime.InteropServices.HandleRef SwigCPtr
566 [EditorBrowsable(EditorBrowsableState.Never)]
567 protected internal bool SwigCMemOwn => swigCMemOwn;
570 /// A flag to check if it is already disposed.
572 [EditorBrowsable(EditorBrowsableState.Never)]
573 protected internal bool Disposed => disposed;