Fix Dispose(true) issue
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Common / BaseHandle.cs
1 /*
2  * Copyright(c) 2021 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17 using System;
18 using System.ComponentModel;
19 using System.Runtime.CompilerServices;
20 using Tizen.NUI.Binding;
21
22 namespace Tizen.NUI
23 {
24     /// <summary>
25     /// BaseHandle is a handle to an internal Dali resource.
26     /// </summary>
27     /// <since_tizen> 3 </since_tizen>
28     public class BaseHandle : Element, global::System.IDisposable
29     {
30         /// <summary>
31         /// swigCMemOwn
32         /// </summary>
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;
37
38         /// <summary>
39         /// A flag to check if it is already disposed.
40         /// </summary>
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;
45
46         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
47         private global::System.Runtime.InteropServices.HandleRef swigCPtrCopy;
48         private bool registerMe;
49
50         //A Flag to check who called Dispose(). (By User or DisposeQueue)
51         private bool isDisposeQueued = false;
52
53         /// <summary>
54         /// Create an instance of BaseHandle.
55         /// </summary>
56         /// <since_tizen> 3 </since_tizen>
57         public BaseHandle() : this(Interop.BaseHandle.NewBaseHandle())
58         {
59             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
60         }
61
62         /// <summary>
63         /// Create an instance of BaseHandle.
64         /// </summary>
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)))
68         {
69             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
70         }
71
72         internal BaseHandle(global::System.IntPtr cPtr, bool cMemoryOwn)
73         {
74             //to catch derived classes dali native exceptions
75             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
76
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();
82
83             if (registerMe)
84             {
85
86                 // Register this instance of BaseHandle in the registry.
87                 Registry.Register(this);
88             }
89         }
90
91         internal BaseHandle(global::System.IntPtr cPtr)
92         {
93             registerMe = swigCMemOwn = true;
94
95             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
96
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();
100
101             if (registerMe)
102             {
103                 // Register this instance of BaseHandle in the registry.
104                 Registry.Register(this);
105             }
106         }
107
108         /// <summary>
109         /// Dispose.
110         /// </summary>
111         /// <since_tizen> 3 </since_tizen>
112         ~BaseHandle() => Dispose(false);
113
114         /// <summary>
115         /// Event when a property is set.
116         /// </summary>
117         /// <since_tizen> 5 </since_tizen>
118         /// <seealso cref="BindableObject.PropertyChanged"/>
119         [Obsolete("Deprecated in API9, Will be removed in API11, Please use BindableObject.PropertyChanged instead!")]
120         public event PropertyChangedEventHandler PropertySet;
121
122         internal global::System.Runtime.InteropServices.HandleRef GetBaseHandleCPtrHandleRef
123         {
124             get
125             {
126                 return swigCPtrCopy;
127             }
128         }
129
130         /// <summary>
131         /// Returns the bool value true to indicate that an operand is true and returns false otherwise.
132         /// </summary>
133         /// <since_tizen> 3 </since_tizen>
134         public static bool operator true(BaseHandle handle)
135         {
136             // if the C# object is null, return false
137             if (BaseHandle.ReferenceEquals(handle, null))
138             {
139                 return false;
140             }
141             // returns true if the handle has a body, false otherwise
142             return handle.HasBody();
143         }
144
145         /// <summary>
146         /// Returns the bool false  to indicate that an operand is false and returns true otherwise.
147         /// </summary>
148         /// <since_tizen> 3 </since_tizen>
149         public static bool operator false(BaseHandle handle)
150         {
151             // if the C# object is null, return true
152             if (BaseHandle.ReferenceEquals(handle, null))
153             {
154                 return true;
155             }
156             return !handle.HasBody();
157         }
158
159         /// <summary>
160         /// Explicit conversion from Handle to bool.
161         /// </summary>
162         /// <since_tizen> 3 </since_tizen>
163         public static explicit operator bool(BaseHandle handle)
164         {
165             // if the C# object is null, return false
166             if (BaseHandle.ReferenceEquals(handle, null))
167             {
168                 return false;
169             }
170             // returns true if the handle has a body, false otherwise
171             return handle.HasBody();
172         }
173
174         /// <summary>
175         /// Equality operator
176         /// </summary>
177         /// <since_tizen> 3 </since_tizen>
178         public static bool operator ==(BaseHandle x, BaseHandle y)
179         {
180             // if the C# objects are the same return true
181             if (BaseHandle.ReferenceEquals(x, y))
182             {
183                 return true;
184             }
185             if (!BaseHandle.ReferenceEquals(x, null) && !BaseHandle.ReferenceEquals(y, null))
186             {
187                 // drop into native code to see if both handles point to the same body
188                 return x.IsEqual(y);
189             }
190
191             if (BaseHandle.ReferenceEquals(x, null) && !BaseHandle.ReferenceEquals(y, null))
192             {
193                 if (y.HasBody()) return false;
194                 else return true;
195             }
196             if (!BaseHandle.ReferenceEquals(x, null) && BaseHandle.ReferenceEquals(y, null))
197             {
198                 if (x.HasBody()) return false;
199                 else return true;
200             }
201
202             return false;
203         }
204
205         /// <summary>
206         /// Inequality operator. Returns Null if either operand is Null
207         /// </summary>
208         /// <since_tizen> 3 </since_tizen>
209         public static bool operator !=(BaseHandle x, BaseHandle y)
210         {
211             return !(x == y);
212         }
213
214         /// <summary>
215         /// Logical AND operator.<br />
216         /// It's possible when doing a  operator this function (opBitwiseAnd) is never called due to short circuiting.<br />
217         /// </summary>
218         /// <since_tizen> 3 </since_tizen>
219         public static BaseHandle operator &(BaseHandle x, BaseHandle y)
220         {
221             if (x == y)
222             {
223                 return x;
224             }
225             return null;
226         }
227
228         /// <summary>
229         /// Logical OR operator for ||.<br />
230         /// It's possible when doing a || this function (opBitwiseOr) is never called due to short circuiting.<br />
231         /// </summary>
232         /// <since_tizen> 3 </since_tizen>
233         public static BaseHandle operator |(BaseHandle x, BaseHandle y)
234         {
235             if (!BaseHandle.ReferenceEquals(x, null) || !BaseHandle.ReferenceEquals(y, null))
236             {
237                 if (x != null && x.HasBody())
238                 {
239                     return x;
240                 }
241                 if (y != null && y.HasBody())
242                 {
243                     return y;
244                 }
245                 return null;
246             }
247             return null;
248         }
249
250         /// <summary>
251         /// Logical ! operator
252         /// </summary>
253         /// <since_tizen> 3 </since_tizen>
254         public static bool operator !(BaseHandle x)
255         {
256             // if the C# object is null, return true
257             if (BaseHandle.ReferenceEquals(x, null))
258             {
259                 return true;
260             }
261             if (x.HasBody())
262             {
263                 return false;
264             }
265             return true;
266         }
267
268         /// <summary>
269         /// Equals
270         /// </summary>
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)
275         {
276             return base.Equals(o);
277         }
278
279         /// <summary>
280         /// Gets the hash code of this baseHandle.
281         /// </summary>
282         /// <returns>The hash code.</returns>
283         /// <since_tizen> 5 </since_tizen>
284         public override int GetHashCode()
285         {
286             return base.GetHashCode();
287         }
288
289         /// <summary>
290         /// Dispose.
291         /// </summary>
292         /// <since_tizen> 3 </since_tizen>
293         public void Dispose()
294         {
295             if (isDisposeQueued)
296             {
297                 Dispose(DisposeTypes.Implicit);
298             }
299             else
300             {
301                 Dispose(true);
302             }
303             GC.SuppressFinalize(this);
304         }
305
306         /// <summary>
307         /// Hidden API (Inhouse API).
308         /// Dispose. 
309         /// </summary>
310         /// <remarks>
311         /// Following the guide of https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose.
312         /// This will replace "protected virtual void Dispose(DisposeTypes type)" which is exactly same in functionality.
313         /// </remarks>
314         /// <param name="disposing">true in order to free managed objects</param>
315         // Protected implementation of Dispose pattern.
316         [EditorBrowsable(EditorBrowsableState.Never)]
317         protected virtual void Dispose(bool disposing)
318         {
319             if (disposed)
320             {
321                 return;
322             }
323
324             if (disposing)
325             {
326                 // TODO: dispose managed state (managed objects).
327                 // Explicit call. user calls Dispose()
328
329                 //Throw exception if Dispose() is called in separate thread.
330                 if (!Window.IsInstalled())
331                 {
332                     throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
333                 }
334
335                 if (isDisposeQueued)
336                 {
337                     Dispose(DisposeTypes.Implicit);
338                 }
339                 else
340                 {
341                     Dispose(DisposeTypes.Explicit);
342                 }
343             }
344             else
345             {
346                 // Implicit call. user doesn't call Dispose(), so this object is added into DisposeQueue to be disposed automatically.
347                 if (!isDisposeQueued)
348                 {
349                     isDisposeQueued = true;
350                     DisposeQueue.Instance.Add(this);
351                 }
352             }
353
354             // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
355             // TODO: set large fields to null.
356         }
357
358
359         /// <summary>
360         /// Performs an action on this object with the given action name and attributes.
361         /// </summary>
362         /// <param name="actionName">The command for the action.</param>
363         /// <param name="attributes">The list of attributes for the action.</param>
364         /// <returns>The action is performed by the object or not.</returns>
365         /// <since_tizen> 3 </since_tizen>
366         public bool DoAction(string actionName, PropertyMap attributes)
367         {
368             bool ret = Interop.BaseHandle.DoAction(swigCPtrCopy, actionName, PropertyMap.getCPtr(attributes));
369             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
370             return ret;
371         }
372
373         /// <summary>
374         /// Returns the type name for the Handle.<br />
375         /// Will return an empty string if the typename does not exist. This will happen for types that
376         /// have not registered with type-registry.
377         /// </summary>
378         /// <returns>The type name. Empty string if the typename does not exist.</returns>
379         /// <since_tizen> 3 </since_tizen>
380         public string GetTypeName()
381         {
382             string ret = Interop.BaseHandle.GetTypeName(swigCPtrCopy);
383             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
384             return ret;
385         }
386
387         /// <summary>
388         /// Returns the type info for the Handle.<br />
389         /// </summary>
390         /// <param name="info">The type information.</param>
391         /// <returns>True If get the type info.</returns>
392         /// <since_tizen> 3 </since_tizen>
393         public bool GetTypeInfo(Tizen.NUI.TypeInfo info)
394         {
395             bool ret = Interop.BaseHandle.GetTypeInfo(swigCPtrCopy, Tizen.NUI.TypeInfo.getCPtr(info));
396             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
397             return ret;
398         }
399
400         /// <summary>
401         /// Resets the handle.
402         /// </summary>
403         /// <since_tizen> 3 </since_tizen>
404         public void Reset()
405         {
406             Interop.BaseHandle.Reset(swigCPtrCopy);
407             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
408         }
409
410         /// <summary>
411         /// To check the BaseHandle instance is equal or not.
412         /// </summary>
413         /// <param name="rhs">The baseHandle instance.</param>
414         /// <returns>True If equal.</returns>
415         /// <since_tizen> 3 </since_tizen>
416         public bool EqualTo(BaseHandle rhs)
417         {
418             bool ret = Interop.BaseHandle.EqualTo(swigCPtrCopy, BaseHandle.getCPtr(rhs));
419             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
420             return ret;
421         }
422
423         /// <summary>
424         /// To check the BaseHandle instance is equal or not.
425         /// </summary>
426         /// <param name="rhs">The baseHandle instance.</param>
427         /// <returns>True If not equal.</returns>
428         /// <since_tizen> 3 </since_tizen>
429         public bool NotEqualTo(BaseHandle rhs)
430         {
431             bool ret = Interop.BaseHandle.NotEqualTo(swigCPtrCopy, BaseHandle.getCPtr(rhs));
432             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
433             return ret;
434         }
435
436         /// <summary>
437         /// To check the BaseHandle instance has body or not.
438         /// </summary>
439         /// <returns>True If the baseHandle instance has body.</returns>
440         /// <since_tizen> 3 </since_tizen>
441         public bool HasBody()
442         {
443             if (swigCPtrCopy.Handle == IntPtr.Zero)
444             {
445                 return false;
446             }
447
448             if (disposed == true)
449             {
450                 return false;
451             }
452             bool ret = Interop.BaseHandle.HasBody(swigCPtrCopy);
453             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
454             return ret;
455         }
456
457         /// <summary>
458         /// To check the BaseHandle instance is equal or not.
459         /// </summary>
460         /// <param name="rhs">The baseHandle instance.</param>
461         /// <returns>True If equal.</returns>
462         /// <since_tizen> 3 </since_tizen>
463         public bool IsEqual(BaseHandle rhs)
464         {
465             if (disposed == true)
466             {
467                 return false;
468             }
469
470             bool ret = Interop.BaseHandle.IsEqual(swigCPtrCopy, BaseHandle.getCPtr(rhs));
471             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
472             return ret;
473         }
474
475         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(BaseHandle obj)
476         {
477             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtrCopy;
478         }
479
480         internal void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
481         {
482             PropertySet?.Invoke(this, new PropertyChangedEventArgs(propertyName));
483         }
484
485         /// <summary>
486         /// Dispose.
487         /// </summary>
488         /// <since_tizen> 3 </since_tizen>
489         protected virtual void Dispose(DisposeTypes type)
490         {
491             if (disposed)
492             {
493                 return;
494             }
495
496             if (type == DisposeTypes.Explicit)
497             {
498                 //Called by User
499                 //Release your own managed resources here.
500                 //You should release all of your own disposable objects here.
501
502             }
503
504             //Release your own unmanaged resources here.
505             //You should not access any managed member here except static instance.
506             //because the execution order of Finalizes is non-deterministic.
507
508             //Unreference this instance from Registry.
509             if (registerMe)
510             {
511                 Registry.Unregister(this);
512             }
513
514             if (SwigCPtr.Handle != IntPtr.Zero)
515             {
516                 if (swigCMemOwn)
517                 {
518                     swigCMemOwn = false;
519                     ReleaseSwigCPtr(SwigCPtr);
520                 }
521                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
522             }
523             if (swigCPtrCopy.Handle != global::System.IntPtr.Zero)
524             {
525                 swigCMemOwn = false;
526                 Interop.BaseHandle.DeleteBaseHandle(swigCPtrCopy);
527                 swigCPtrCopy = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
528             }
529
530             disposed = true;
531
532             if (null != Application.Current)
533             {
534                 Application.Current.XamlResourceChanged -= OnResourcesChanged;
535             }
536         }
537
538         /// <summary>
539         /// Release swigCPtr.
540         /// </summary>
541         /// <since_tizen> 6 </since_tizen>
542         /// This will not be public opened.
543         [EditorBrowsable(EditorBrowsableState.Never)]
544         protected virtual void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
545         {
546         }
547
548         /// <summary>
549         /// Contains event arguments for the FocusChangeRequested event.
550         /// </summary>
551         [Obsolete("Deprecated in API9; Will be removed in API11.")]
552         public class FocusRequestArgs : EventArgs
553         {
554
555             /// <summary>
556             /// Gets or sets a value that indicates the starting focus state of the element for which a focus change is requested.
557             /// </summary>
558             public bool Focus { get; set; }
559
560             /// <summary>
561             /// Gets or sets a value that indicates the ending focus state of the element for which a focus change is requested.
562             /// </summary>
563             public bool Result { get; set; }
564         }
565
566         internal global::System.Runtime.InteropServices.HandleRef SwigCPtr
567         {
568             get => swigCPtr;
569             set
570             {
571                 swigCPtr = value;
572             }
573         }
574
575         /// <summary>
576         /// swigCMemOwn
577         /// </summary>
578         [EditorBrowsable(EditorBrowsableState.Never)]
579         protected internal bool SwigCMemOwn => swigCMemOwn;
580
581         /// <summary>
582         /// A flag to check if it is already disposed.
583         /// </summary>
584         [EditorBrowsable(EditorBrowsableState.Never)]
585         protected internal bool Disposed => disposed;
586     }
587 }