Release 4.0.0-preview1-00301
[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
18 namespace Tizen.NUI
19 {
20
21     /// <summary>
22     /// BaseHandle is a handle to an internal Dali resource.
23     /// </summary>
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> 4 </since_tizen>
304         public override bool Equals(object o)
305         {
306             if(o == null)
307             {
308                 return false;
309             }
310             if(!(o is BaseHandle))
311             {
312                 return false;
313             }
314             BaseHandle b = (BaseHandle)o;
315             if (!BaseHandle.ReferenceEquals(this, null) && !BaseHandle.ReferenceEquals(b, null))
316             {
317                 // drop into native code to see if both handles point to the same body
318                 return this.IsEqual(b);
319             }
320
321             if (BaseHandle.ReferenceEquals(this, null) && !BaseHandle.ReferenceEquals(b, null))
322             {
323                 if (b.HasBody()) return false;
324                 else return true;
325             }
326             if (!BaseHandle.ReferenceEquals(this, null) && BaseHandle.ReferenceEquals(b, null))
327             {
328                 if (this.HasBody()) return false;
329                 else return true;
330             }
331
332             return false;
333         }
334
335         /// <summary>
336         /// Gets the the hash code of this baseHandle.
337         /// </summary>
338         /// <returns>The Hash Code.</returns>
339         /// <since_tizen> 4 </since_tizen>
340         public override int GetHashCode()
341         {
342             return base.GetHashCode();
343         }
344
345         /// <summary>
346         /// Create an instance of BaseHandle.
347         /// </summary>
348         /// <since_tizen> 3 </since_tizen>
349         public BaseHandle() : this(NDalicPINVOKE.new_BaseHandle__SWIG_1())
350         {
351             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
352         }
353
354         /// <summary>
355         /// Create an instance of BaseHandle.
356         /// </summary>
357         /// <param name="handle">The BaseHandle instance.</param>
358         /// <since_tizen> 3 </since_tizen>
359         public BaseHandle(BaseHandle handle) : this(NDalicPINVOKE.new_BaseHandle__SWIG_2(BaseHandle.getCPtr(handle)))
360         {
361             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
362         }
363
364
365         /// <summary>
366         /// Performs an action on this object with the given action name and attributes.
367         /// </summary>
368         /// <param name="actionName">The command for the action.</param>
369         /// <param name="attributes">The list of attributes for the action.</param>
370         /// <returns>The action is performed by the object or not.</returns>
371         /// <since_tizen> 3 </since_tizen>
372         public bool DoAction(string actionName, PropertyMap attributes)
373         {
374             bool ret = NDalicPINVOKE.BaseHandle_DoAction(swigCPtr, actionName, PropertyMap.getCPtr(attributes));
375             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
376             return ret;
377         }
378
379         /// <summary>
380         /// Returns the type name for the Handle.<br />
381         /// Will return an empty string if the typename does not exist. This will happen for types that
382         /// have not registered with type-registry.
383         /// </summary>
384         /// <returns>The type name. Empty string if the typename does not exist.</returns>
385         /// <since_tizen> 3 </since_tizen>
386         public string GetTypeName()
387         {
388             string ret = NDalicPINVOKE.BaseHandle_GetTypeName(swigCPtr);
389             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
390             return ret;
391         }
392
393         /// <summary>
394         /// Returns the type info for the Handle.<br />
395         /// </summary>
396         /// <param name="info">The type information.</param>
397         /// <returns>True If get the type info.</returns>
398         /// <since_tizen> 3 </since_tizen>
399         public bool GetTypeInfo(TypeInfo info)
400         {
401             bool ret = NDalicPINVOKE.BaseHandle_GetTypeInfo(swigCPtr, TypeInfo.getCPtr(info));
402             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
403             return ret;
404         }
405
406         /// <summary>
407         /// Resets the handle.
408         /// </summary>
409         /// <since_tizen> 3 </since_tizen>
410         public void Reset()
411         {
412             NDalicPINVOKE.BaseHandle_Reset(swigCPtr);
413             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
414         }
415
416         /// <summary>
417         /// To check the BaseHandle instance is equal or not.
418         /// </summary>
419         /// <param name="rhs">The baseHandle instance.</param>
420         /// <returns>True If equal.</returns>
421         /// <since_tizen> 3 </since_tizen>
422         public bool EqualTo(BaseHandle rhs)
423         {
424             bool ret = NDalicPINVOKE.BaseHandle_EqualTo(swigCPtr, BaseHandle.getCPtr(rhs));
425             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
426             return ret;
427         }
428
429         /// <summary>
430         /// To check the BaseHandle instance is equal or not.
431         /// </summary>
432         /// <param name="rhs">The baseHandle instance.</param>
433         /// <returns>True If not equal.</returns>
434         /// <since_tizen> 3 </since_tizen>
435         public bool NotEqualTo(BaseHandle rhs)
436         {
437             bool ret = NDalicPINVOKE.BaseHandle_NotEqualTo(swigCPtr, BaseHandle.getCPtr(rhs));
438             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
439             return ret;
440         }
441
442         internal RefObject GetObjectPtr()
443         {
444             global::System.IntPtr cPtr = NDalicPINVOKE.BaseHandle_GetObjectPtr(swigCPtr);
445             RefObject ret = (cPtr == global::System.IntPtr.Zero) ? null : new RefObject(cPtr, false);
446             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
447             return ret;
448         }
449
450         /// <summary>
451         /// To check the BaseHandle instance has body or not.
452         /// </summary>
453         /// <returns>True If the baseHandle instance has body.</returns>
454         /// <since_tizen> 3 </since_tizen>
455         public bool HasBody()
456         {
457             if (disposed == true)
458             {
459                 return false;
460             }
461
462             bool ret = NDalicPINVOKE.BaseHandle_HasBody(swigCPtr);
463             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
464             return ret;
465         }
466
467         /// <summary>
468         /// To check the BaseHandle instance is equal or not.
469         /// </summary>
470         /// <param name="rhs">The baseHandle instance.</param>
471         /// <returns>True If equal.</returns>
472         /// <since_tizen> 3 </since_tizen>
473         public bool IsEqual(BaseHandle rhs)
474         {
475             if (disposed == true)
476             {
477                 return false;
478             }
479
480             bool ret = NDalicPINVOKE.BaseHandle_IsEqual(swigCPtr, BaseHandle.getCPtr(rhs));
481             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
482             return ret;
483         }
484
485         internal global::System.Runtime.InteropServices.HandleRef GetBaseHandleCPtrHandleRef
486         {
487             get
488             {
489                 return swigCPtr;
490             }
491         }
492
493     }
494
495 }