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