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