653043cdcbc949cef38b2e6ed80177ff6b66817a
[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 namespace Tizen.NUI
18 {
19
20     /// <summary>
21     /// BaseHandle is a handle to an internal Dali resource.
22     /// </summary>
23     /// <since_tizen> 3 </since_tizen>
24     public class BaseHandle : global::System.IDisposable
25     {
26         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
27         /// <summary>
28         /// swigCMemOwn
29         /// </summary>
30         /// <since_tizen> 3 </since_tizen>
31         protected bool swigCMemOwn;
32         private bool _registerMe;
33
34         internal BaseHandle(global::System.IntPtr cPtr, bool cMemoryOwn)
35         {
36             //to catch derived classes dali native exceptions
37             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
38
39             _registerMe = swigCMemOwn = cMemoryOwn;
40
41             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
42
43             // using copy constructor to create another native handle so Registry.Unregister works fine.
44             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, NDalicPINVOKE.new_BaseHandle__SWIG_2(swigCPtr));
45             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
46
47             if (_registerMe)
48             {
49                 // Register this instance of BaseHandle in the registry.
50                 Registry.Register(this);
51             }
52         }
53
54         internal BaseHandle(global::System.IntPtr cPtr)
55         {
56             _registerMe = swigCMemOwn = true;
57
58             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
59
60             // using copy constructor to create another native handle so Registry.Unregister works fine.
61             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, NDalicPINVOKE.new_BaseHandle__SWIG_2(swigCPtr));
62             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
63
64             if (_registerMe)
65             {
66                 // Register this instance of BaseHandle in the registry.
67                 Registry.Register(this);
68             }
69         }
70
71         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(BaseHandle obj)
72         {
73             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
74         }
75
76         //A Flag to check who called Dispose(). (By User or DisposeQueue)
77         private bool isDisposeQueued = false;
78
79         /// <summary>
80         /// A Flat to check if it is already disposed.
81         /// </summary>
82         /// <since_tizen> 3 </since_tizen>
83         protected bool disposed = false;
84
85         /// <summary>
86         /// Dispose.
87         /// </summary>
88         /// <since_tizen> 3 </since_tizen>
89         ~BaseHandle()
90         {
91             if (!isDisposeQueued)
92             {
93                 isDisposeQueued = true;
94                 DisposeQueue.Instance.Add(this);
95             }
96         }
97
98         /// <summary>
99         /// Dispose.
100         /// </summary>
101         /// <since_tizen> 3 </since_tizen>
102         public void Dispose()
103         {
104             //Throw excpetion if Dispose() is called in separate thread.
105             if (!Window.IsInstalled())
106             {
107                 throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
108             }
109
110             if (isDisposeQueued)
111             {
112                 Dispose(DisposeTypes.Implicit);
113             }
114             else
115             {
116                 Dispose(DisposeTypes.Explicit);
117                 System.GC.SuppressFinalize(this);
118             }
119         }
120
121         /// <summary>
122         /// Dispose.
123         /// </summary>
124         /// <since_tizen> 3 </since_tizen>
125         protected virtual void Dispose(DisposeTypes type)
126         {
127             if (disposed)
128             {
129                 return;
130             }
131
132             if (type == DisposeTypes.Explicit)
133             {
134                 //Called by User
135                 //Release your own managed resources here.
136                 //You should release all of your own disposable objects here.
137
138             }
139
140             //Release your own unmanaged resources here.
141             //You should not access any managed member here except static instance.
142             //because the execution order of Finalizes is non-deterministic.
143
144             //Unreference this instance from Registry.
145             if (_registerMe)
146             {
147                 Registry.Unregister(this);
148             }
149
150             if (swigCPtr.Handle != global::System.IntPtr.Zero)
151             {
152                 swigCMemOwn = false;
153                 NDalicPINVOKE.delete_BaseHandle(swigCPtr);
154                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
155             }
156
157             disposed = true;
158         }
159
160         /// <summary>
161         /// Returns the bool value true to indicate that an operand is true and returns false otherwise.
162         /// </summary>
163         /// <since_tizen> 3 </since_tizen>
164         public static bool operator true(BaseHandle handle)
165         {
166             // if the C# object is null, return false
167             if (BaseHandle.ReferenceEquals(handle, null))
168             {
169                 return false;
170             }
171             // returns true if the handle has a body, false otherwise
172             return handle.HasBody();
173         }
174
175         /// <summary>
176         /// Returns the bool false  to indicate that an operand is false and returns true otherwise.
177         /// </summary>
178         /// <since_tizen> 3 </since_tizen>
179         public static bool operator false(BaseHandle handle)
180         {
181             // if the C# object is null, return true
182             if (BaseHandle.ReferenceEquals(handle, null))
183             {
184                 return true;
185             }
186             return !handle.HasBody();
187         }
188
189         /// <summary>
190         /// Explicit conversion from Handle to bool.
191         /// </summary>
192         /// <since_tizen> 3 </since_tizen>
193         public static explicit operator bool(BaseHandle handle)
194         {
195             // if the C# object is null, return false
196             if (BaseHandle.ReferenceEquals(handle, null))
197             {
198                 return false;
199             }
200             // returns true if the handle has a body, false otherwise
201             return handle.HasBody();
202         }
203
204         /// <summary>
205         /// Equality operator
206         /// </summary>
207         /// <since_tizen> 3 </since_tizen>
208         public static bool operator ==(BaseHandle x, BaseHandle y)
209         {
210             // if the C# objects are the same return true
211             if (BaseHandle.ReferenceEquals(x, y))
212             {
213                 return true;
214             }
215             if (!BaseHandle.ReferenceEquals(x, null) && !BaseHandle.ReferenceEquals(y, null))
216             {
217                 // drop into native code to see if both handles point to the same body
218                 return x.IsEqual(y);
219             }
220
221             if (BaseHandle.ReferenceEquals(x, null) && !BaseHandle.ReferenceEquals(y, null))
222             {
223                 if (y.HasBody()) return false;
224                 else return true;
225             }
226             if (!BaseHandle.ReferenceEquals(x, null) && BaseHandle.ReferenceEquals(y, null))
227             {
228                 if (x.HasBody()) return false;
229                 else return true;
230             }
231
232             return false;
233         }
234
235         /// <summary>
236         /// Inequality operator. Returns Null if either operand is Null
237         /// </summary>
238         /// <since_tizen> 3 </since_tizen>
239         public static bool operator !=(BaseHandle x, BaseHandle y)
240         {
241             return !(x == y);
242         }
243
244         /// <summary>
245         /// Logical AND operator.<br />
246         /// It's possible when doing a  operator this function (opBitwiseAnd) is never called due to short circuiting.<br />
247         /// </summary>
248         /// <since_tizen> 3 </since_tizen>
249         public static BaseHandle operator &(BaseHandle x, BaseHandle y)
250         {
251             if (x == y)
252             {
253                 return x;
254             }
255             return null;
256         }
257
258         /// <summary>
259         /// Logical OR operator for ||.<br />
260         /// It's possible when doing a || this function (opBitwiseOr) is never called due to short circuiting.<br />
261         /// </summary>
262         /// <since_tizen> 3 </since_tizen>
263         public static BaseHandle operator |(BaseHandle x, BaseHandle y)
264         {
265             if (!BaseHandle.ReferenceEquals(x, null) || !BaseHandle.ReferenceEquals(y, null))
266             {
267                 if (x.HasBody())
268                 {
269                     return x;
270                 }
271                 if (y.HasBody())
272                 {
273                     return y;
274                 }
275                 return null;
276             }
277             return null;
278         }
279
280         /// <summary>
281         /// Logical ! operator
282         /// </summary>
283         /// <since_tizen> 3 </since_tizen>
284         public static bool operator !(BaseHandle x)
285         {
286             // if the C# object is null, return true
287             if (BaseHandle.ReferenceEquals(x, null))
288             {
289                 return true;
290             }
291             if (x.HasBody())
292             {
293                 return false;
294             }
295             return true;
296         }
297
298         /// <summary>
299         /// Equals
300         /// </summary>
301         /// <param name="o">The object should be compared.</param>
302         /// <returns>True if equal.</returns>
303         /// <since_tizen> 5 </since_tizen>
304         public override bool Equals(object o)
305         {
306             return base.Equals(o);
307         }
308
309         /// <summary>
310         /// Gets the the hash code of this baseHandle.
311         /// </summary>
312         /// <returns>The Hash Code.</returns>
313         /// <since_tizen> 5 </since_tizen>
314         public override int GetHashCode()
315         {
316             return base.GetHashCode();
317         }
318
319         /// <summary>
320         /// Create an instance of BaseHandle.
321         /// </summary>
322         /// <since_tizen> 3 </since_tizen>
323         public BaseHandle() : this(NDalicPINVOKE.new_BaseHandle__SWIG_1())
324         {
325             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
326         }
327
328         /// <summary>
329         /// Create an instance of BaseHandle.
330         /// </summary>
331         /// <param name="handle">The BaseHandle instance.</param>
332         /// <since_tizen> 3 </since_tizen>
333         public BaseHandle(BaseHandle handle) : this(NDalicPINVOKE.new_BaseHandle__SWIG_2(BaseHandle.getCPtr(handle)))
334         {
335             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
336         }
337
338
339         /// <summary>
340         /// Performs an action on this object with the given action name and attributes.
341         /// </summary>
342         /// <param name="actionName">The command for the action.</param>
343         /// <param name="attributes">The list of attributes for the action.</param>
344         /// <returns>The action is performed by the object or not.</returns>
345         /// <since_tizen> 3 </since_tizen>
346         public bool DoAction(string actionName, PropertyMap attributes)
347         {
348             bool ret = NDalicPINVOKE.BaseHandle_DoAction(swigCPtr, actionName, PropertyMap.getCPtr(attributes));
349             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
350             return ret;
351         }
352
353         /// <summary>
354         /// Returns the type name for the Handle.<br />
355         /// Will return an empty string if the typename does not exist. This will happen for types that
356         /// have not registered with type-registry.
357         /// </summary>
358         /// <returns>The type name. Empty string if the typename does not exist.</returns>
359         /// <since_tizen> 3 </since_tizen>
360         public string GetTypeName()
361         {
362             string ret = NDalicPINVOKE.BaseHandle_GetTypeName(swigCPtr);
363             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
364             return ret;
365         }
366
367         /// <summary>
368         /// Returns the type info for the Handle.<br />
369         /// </summary>
370         /// <param name="info">The type information.</param>
371         /// <returns>True If get the type info.</returns>
372         /// <since_tizen> 3 </since_tizen>
373         public bool GetTypeInfo(TypeInfo info)
374         {
375             bool ret = NDalicPINVOKE.BaseHandle_GetTypeInfo(swigCPtr, TypeInfo.getCPtr(info));
376             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
377             return ret;
378         }
379
380         /// <summary>
381         /// Resets the handle.
382         /// </summary>
383         /// <since_tizen> 3 </since_tizen>
384         public void Reset()
385         {
386             NDalicPINVOKE.BaseHandle_Reset(swigCPtr);
387             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
388         }
389
390         /// <summary>
391         /// To check the BaseHandle instance is equal or not.
392         /// </summary>
393         /// <param name="rhs">The baseHandle instance.</param>
394         /// <returns>True If equal.</returns>
395         /// <since_tizen> 3 </since_tizen>
396         public bool EqualTo(BaseHandle rhs)
397         {
398             bool ret = NDalicPINVOKE.BaseHandle_EqualTo(swigCPtr, BaseHandle.getCPtr(rhs));
399             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
400             return ret;
401         }
402
403         /// <summary>
404         /// To check the BaseHandle instance is equal or not.
405         /// </summary>
406         /// <param name="rhs">The baseHandle instance.</param>
407         /// <returns>True If not equal.</returns>
408         /// <since_tizen> 3 </since_tizen>
409         public bool NotEqualTo(BaseHandle rhs)
410         {
411             bool ret = NDalicPINVOKE.BaseHandle_NotEqualTo(swigCPtr, BaseHandle.getCPtr(rhs));
412             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
413             return ret;
414         }
415
416         internal RefObject GetObjectPtr()
417         {
418             global::System.IntPtr cPtr = NDalicPINVOKE.BaseHandle_GetObjectPtr(swigCPtr);
419             RefObject ret = (cPtr == global::System.IntPtr.Zero) ? null : new RefObject(cPtr, false);
420             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
421             return ret;
422         }
423
424         /// <summary>
425         /// To check the BaseHandle instance has body or not.
426         /// </summary>
427         /// <returns>True If the baseHandle instance has body.</returns>
428         /// <since_tizen> 3 </since_tizen>
429         public bool HasBody()
430         {
431             if (disposed == true)
432             {
433                 return false;
434             }
435
436             bool ret = NDalicPINVOKE.BaseHandle_HasBody(swigCPtr);
437             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
438             return ret;
439         }
440
441         /// <summary>
442         /// To check the BaseHandle instance is equal or not.
443         /// </summary>
444         /// <param name="rhs">The baseHandle instance.</param>
445         /// <returns>True If equal.</returns>
446         /// <since_tizen> 3 </since_tizen>
447         public bool IsEqual(BaseHandle rhs)
448         {
449             if (disposed == true)
450             {
451                 return false;
452             }
453
454             bool ret = NDalicPINVOKE.BaseHandle_IsEqual(swigCPtr, BaseHandle.getCPtr(rhs));
455             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
456             return ret;
457         }
458
459         internal global::System.Runtime.InteropServices.HandleRef GetBaseHandleCPtrHandleRef
460         {
461             get
462             {
463                 return swigCPtr;
464             }
465         }
466
467     }
468
469 }