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 protected bool swigCMemOwn;
39 /// A flag to check if it is already disposed.
41 /// <since_tizen> 3 </since_tizen>
42 protected bool disposed = false;
44 private global::System.Runtime.InteropServices.HandleRef swigCPtr;
45 private global::System.Runtime.InteropServices.HandleRef swigCPtrCopy;
46 private bool registerMe;
48 //A Flag to check who called Dispose(). (By User or DisposeQueue)
49 private bool isDisposeQueued = false;
52 /// Create an instance of BaseHandle.
54 /// <since_tizen> 3 </since_tizen>
55 public BaseHandle() : this(Interop.BaseHandle.NewBaseHandle())
57 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
61 /// Create an instance of BaseHandle.
63 /// <param name="handle">The BaseHandle instance.</param>
64 /// <since_tizen> 3 </since_tizen>
65 public BaseHandle(BaseHandle handle) : this(Interop.BaseHandle.NewBaseHandle(BaseHandle.getCPtr(handle)))
67 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
70 internal BaseHandle(global::System.IntPtr cPtr, bool cMemoryOwn)
72 //to catch derived classes dali native exceptions
73 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
75 registerMe = swigCMemOwn = cMemoryOwn;
76 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
77 // using copy constructor to create another native handle so Registry.Unregister works fine.
78 swigCPtrCopy = new global::System.Runtime.InteropServices.HandleRef(this, Interop.BaseHandle.NewBaseHandle(swigCPtr));
79 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
84 // Register this instance of BaseHandle in the registry.
85 Registry.Register(this);
89 internal BaseHandle(global::System.IntPtr cPtr)
91 registerMe = swigCMemOwn = true;
93 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
95 // using copy constructor to create another native handle so Registry.Unregister works fine.
96 swigCPtrCopy = new global::System.Runtime.InteropServices.HandleRef(this, Interop.BaseHandle.NewBaseHandle(SwigCPtr));
97 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
101 // Register this instance of BaseHandle in the registry.
102 Registry.Register(this);
109 /// <since_tizen> 3 </since_tizen>
110 ~BaseHandle() => Dispose(false);
113 /// Event when a property is set.
115 /// <since_tizen> 5 </since_tizen>
116 public event PropertyChangedEventHandler PropertySet;
118 internal global::System.Runtime.InteropServices.HandleRef GetBaseHandleCPtrHandleRef
127 /// Returns the bool value true to indicate that an operand is true and returns false otherwise.
129 /// <since_tizen> 3 </since_tizen>
130 public static bool operator true(BaseHandle handle)
132 // if the C# object is null, return false
133 if (BaseHandle.ReferenceEquals(handle, null))
137 // returns true if the handle has a body, false otherwise
138 return handle.HasBody();
142 /// Returns the bool false to indicate that an operand is false and returns true otherwise.
144 /// <since_tizen> 3 </since_tizen>
145 public static bool operator false(BaseHandle handle)
147 // if the C# object is null, return true
148 if (BaseHandle.ReferenceEquals(handle, null))
152 return !handle.HasBody();
156 /// Explicit conversion from Handle to bool.
158 /// <since_tizen> 3 </since_tizen>
159 public static explicit operator bool(BaseHandle handle)
161 // if the C# object is null, return false
162 if (BaseHandle.ReferenceEquals(handle, null))
166 // returns true if the handle has a body, false otherwise
167 return handle.HasBody();
171 /// Equality operator
173 /// <since_tizen> 3 </since_tizen>
174 public static bool operator ==(BaseHandle x, BaseHandle y)
176 // if the C# objects are the same return true
177 if (BaseHandle.ReferenceEquals(x, y))
181 if (!BaseHandle.ReferenceEquals(x, null) && !BaseHandle.ReferenceEquals(y, null))
183 // drop into native code to see if both handles point to the same body
187 if (BaseHandle.ReferenceEquals(x, null) && !BaseHandle.ReferenceEquals(y, null))
189 if (y.HasBody()) return false;
192 if (!BaseHandle.ReferenceEquals(x, null) && BaseHandle.ReferenceEquals(y, null))
194 if (x.HasBody()) return false;
202 /// Inequality operator. Returns Null if either operand is Null
204 /// <since_tizen> 3 </since_tizen>
205 public static bool operator !=(BaseHandle x, BaseHandle y)
211 /// Logical AND operator.<br />
212 /// It's possible when doing a operator this function (opBitwiseAnd) is never called due to short circuiting.<br />
214 /// <since_tizen> 3 </since_tizen>
215 public static BaseHandle operator &(BaseHandle x, BaseHandle y)
225 /// Logical OR operator for ||.<br />
226 /// It's possible when doing a || this function (opBitwiseOr) is never called due to short circuiting.<br />
228 /// <since_tizen> 3 </since_tizen>
229 public static BaseHandle operator |(BaseHandle x, BaseHandle y)
231 if (!BaseHandle.ReferenceEquals(x, null) || !BaseHandle.ReferenceEquals(y, null))
233 if (x != null && x.HasBody())
237 if (y != null && y.HasBody())
247 /// Logical ! operator
249 /// <since_tizen> 3 </since_tizen>
250 public static bool operator !(BaseHandle x)
252 // if the C# object is null, return true
253 if (BaseHandle.ReferenceEquals(x, null))
267 /// <param name="o">The object should be compared.</param>
268 /// <returns>True if equal.</returns>
269 /// <since_tizen> 5 </since_tizen>
270 public override bool Equals(object o)
272 return base.Equals(o);
276 /// Gets the the hash code of this baseHandle.
278 /// <returns>The hash code.</returns>
279 /// <since_tizen> 5 </since_tizen>
280 public override int GetHashCode()
282 return base.GetHashCode();
288 /// <since_tizen> 3 </since_tizen>
289 public void Dispose()
292 System.GC.SuppressFinalize(this);
296 /// Hidden API (Inhouse API).
300 /// Following the guide of https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose.
301 /// This will replace "protected virtual void Dispose(DisposeTypes type)" which is exactly same in functionality.
303 /// <param name="disposing">true in order to free managed objects</param>
304 // Protected implementation of Dispose pattern.
305 [EditorBrowsable(EditorBrowsableState.Never)]
306 protected virtual void Dispose(bool disposing)
315 // TODO: dispose managed state (managed objects).
316 // Explicit call. user calls Dispose()
318 //Throw excpetion if Dispose() is called in separate thread.
319 if (!Window.IsInstalled())
321 throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
326 Dispose(DisposeTypes.Implicit);
330 Dispose(DisposeTypes.Explicit);
335 // Implicit call. user doesn't call Dispose(), so this object is added into DisposeQueue to be disposed automatically.
336 if (!isDisposeQueued)
338 isDisposeQueued = true;
339 DisposeQueue.Instance.Add(this);
343 // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
344 // TODO: set large fields to null.
349 /// Performs an action on this object with the given action name and attributes.
351 /// <param name="actionName">The command for the action.</param>
352 /// <param name="attributes">The list of attributes for the action.</param>
353 /// <returns>The action is performed by the object or not.</returns>
354 /// <since_tizen> 3 </since_tizen>
355 public bool DoAction(string actionName, PropertyMap attributes)
357 bool ret = Interop.BaseHandle.DoAction(swigCPtrCopy, actionName, PropertyMap.getCPtr(attributes));
358 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
363 /// Returns the type name for the Handle.<br />
364 /// Will return an empty string if the typename does not exist. This will happen for types that
365 /// have not registered with type-registry.
367 /// <returns>The type name. Empty string if the typename does not exist.</returns>
368 /// <since_tizen> 3 </since_tizen>
369 public string GetTypeName()
371 string ret = Interop.BaseHandle.GetTypeName(swigCPtrCopy);
372 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
377 /// Returns the type info for the Handle.<br />
379 /// <param name="info">The type information.</param>
380 /// <returns>True If get the type info.</returns>
381 /// <since_tizen> 3 </since_tizen>
382 public bool GetTypeInfo(Tizen.NUI.TypeInfo info)
384 bool ret = Interop.BaseHandle.GetTypeInfo(swigCPtrCopy, Tizen.NUI.TypeInfo.getCPtr(info));
385 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
390 /// Resets the handle.
392 /// <since_tizen> 3 </since_tizen>
395 Interop.BaseHandle.Reset(swigCPtrCopy);
396 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
400 /// To check the BaseHandle instance is equal or not.
402 /// <param name="rhs">The baseHandle instance.</param>
403 /// <returns>True If equal.</returns>
404 /// <since_tizen> 3 </since_tizen>
405 public bool EqualTo(BaseHandle rhs)
407 bool ret = Interop.BaseHandle.EqualTo(swigCPtrCopy, BaseHandle.getCPtr(rhs));
408 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
413 /// To check the BaseHandle instance is equal or not.
415 /// <param name="rhs">The baseHandle instance.</param>
416 /// <returns>True If not equal.</returns>
417 /// <since_tizen> 3 </since_tizen>
418 public bool NotEqualTo(BaseHandle rhs)
420 bool ret = Interop.BaseHandle.NotEqualTo(swigCPtrCopy, BaseHandle.getCPtr(rhs));
421 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
426 /// To check the BaseHandle instance has body or not.
428 /// <returns>True If the baseHandle instance has body.</returns>
429 /// <since_tizen> 3 </since_tizen>
430 public bool HasBody()
432 if (swigCPtrCopy.Handle == IntPtr.Zero)
437 if (disposed == true)
441 bool ret = Interop.BaseHandle.HasBody(swigCPtrCopy);
442 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
447 /// To check the BaseHandle instance is equal or not.
449 /// <param name="rhs">The baseHandle instance.</param>
450 /// <returns>True If equal.</returns>
451 /// <since_tizen> 3 </since_tizen>
452 public bool IsEqual(BaseHandle rhs)
454 if (disposed == true)
459 bool ret = Interop.BaseHandle.IsEqual(swigCPtrCopy, BaseHandle.getCPtr(rhs));
460 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
464 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(BaseHandle obj)
466 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtrCopy;
469 internal void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
471 PropertySet?.Invoke(this, new PropertyChangedEventArgs(propertyName));
477 /// <since_tizen> 3 </since_tizen>
478 protected virtual void Dispose(DisposeTypes type)
485 if (type == DisposeTypes.Explicit)
488 //Release your own managed resources here.
489 //You should release all of your own disposable objects here.
493 //Release your own unmanaged resources here.
494 //You should not access any managed member here except static instance.
495 //because the execution order of Finalizes is non-deterministic.
497 //Unreference this instance from Registry.
500 Registry.Unregister(this);
503 if (SwigCPtr.Handle != IntPtr.Zero)
508 ReleaseSwigCPtr(SwigCPtr);
510 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
512 if (swigCPtrCopy.Handle != global::System.IntPtr.Zero)
515 Interop.BaseHandle.DeleteBaseHandle(swigCPtrCopy);
516 swigCPtrCopy = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
523 /// Release swigCPtr.
525 /// <since_tizen> 6 </since_tizen>
526 /// This will not be public opened.
527 [EditorBrowsable(EditorBrowsableState.Never)]
528 protected virtual void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
533 /// Contains event arguments for the FocusChangeRequested event.
535 [Obsolete("Deprecated in API9; Will be removed in API11.")]
536 public class FocusRequestArgs : EventArgs
540 /// Gets or sets a value that indicates the starting focus state of the element for which a focus change is requested.
542 public bool Focus { get; set; }
545 /// Gets or sets a value that indicates the ending focus state of the element for which a focus change is requested.
547 public bool Result { get; set; }
550 internal global::System.Runtime.InteropServices.HandleRef SwigCPtr
562 [EditorBrowsable(EditorBrowsableState.Never)]
563 protected internal bool SwigCMemOwn => swigCMemOwn;
566 /// A flag to check if it is already disposed.
568 [EditorBrowsable(EditorBrowsableState.Never)]
569 protected internal bool Disposed => disposed;