0f037018723dfb7fa159fd2d0a2fd295ae5655bb
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / BaseHandle.cs
1 /** Copyright (c) 2017 Samsung Electronics Co., Ltd.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 */
16
17 namespace Tizen.NUI
18 {
19
20     public class BaseHandle : global::System.IDisposable
21     {
22         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
23         protected bool swigCMemOwn;
24         private bool _registerMe;
25
26         internal BaseHandle(global::System.IntPtr cPtr, bool cMemoryOwn)
27         {
28             _registerMe = swigCMemOwn = cMemoryOwn;
29
30             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
31
32             // using copy constructor to create another native handle so Registry.Unregister works fine.
33             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, NDalicPINVOKE.new_BaseHandle__SWIG_2(swigCPtr));
34
35             if (_registerMe)
36             {
37                 // Register this instance of BaseHandle in the registry.
38                 Registry.Register(this);
39             }
40         }
41
42         internal BaseHandle(global::System.IntPtr cPtr)
43         {
44             _registerMe = swigCMemOwn = true;
45
46             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
47
48             // using copy constructor to create another native handle so Registry.Unregister works fine.
49             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, NDalicPINVOKE.new_BaseHandle__SWIG_2(swigCPtr));
50
51             if (_registerMe)
52             {
53                 // Register this instance of BaseHandle in the registry.
54                 Registry.Register(this);
55             }
56         }
57
58         internal BaseHandle(global::System.IntPtr cPtr)
59         {
60             _registerMe = swigCMemOwn = true;
61             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
62
63             // Register this instance of BaseHandle in the registry.
64             Registry.Register(this);
65         }
66
67         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(BaseHandle obj)
68         {
69             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
70         }
71
72         //A Flag to check who called Dispose(). (By User or DisposeQueue)
73         private bool isDisposeQueued = false;
74         //A Flat to check if it is already disposed.
75         protected bool disposed = false;
76
77         ~BaseHandle()
78         {
79             if (!isDisposeQueued)
80             {
81                 isDisposeQueued = true;
82                 DisposeQueue.Instance.Add(this);
83             }
84         }
85
86         public void Dispose()
87         {
88             //Throw excpetion if Dispose() is called in separate thread.
89             if (!Window.IsInstalled())
90             {
91                 throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
92             }
93
94             if (isDisposeQueued)
95             {
96                 Dispose(DisposeTypes.Implicit);
97             }
98             else
99             {
100                 Dispose(DisposeTypes.Explicit);
101                 System.GC.SuppressFinalize(this);
102             }
103         }
104
105         protected virtual void Dispose(DisposeTypes type)
106         {
107             if (disposed)
108             {
109                 return;
110             }
111
112             if (type == DisposeTypes.Explicit)
113             {
114                 //Called by User
115                 //Release your own managed resources here.
116                 //You should release all of your own disposable objects here.
117
118             }
119
120             //Release your own unmanaged resources here.
121             //You should not access any managed member here except static instance.
122             //because the execution order of Finalizes is non-deterministic.
123
124             //Unreference this instance from Registry.
125             if (_registerMe)
126             {
127                 Registry.Unregister(this);
128             }
129
130             if (swigCPtr.Handle != global::System.IntPtr.Zero)
131             {
132                 swigCMemOwn = false;
133                 NDalicPINVOKE.delete_BaseHandle(swigCPtr);
134                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
135             }
136
137             disposed = true;
138         }
139
140
141
142         // Returns the bool value true to indicate that an operand is true and returns false otherwise.
143         public static bool operator true(BaseHandle handle)
144         {
145             // if the C# object is null, return false
146             if (BaseHandle.ReferenceEquals(handle, null))
147             {
148                 return false;
149             }
150             // returns true if the handle has a body, false otherwise
151             return handle.HasBody();
152         }
153
154         // Returns the bool false  to indicate that an operand is false and returns true otherwise.
155         public static bool operator false(BaseHandle handle)
156         {
157             // if the C# object is null, return true
158             if (BaseHandle.ReferenceEquals(handle, null))
159             {
160                 return true;
161             }
162             return !handle.HasBody();
163         }
164
165         // Explicit conversion from Handle to bool.
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         // Equality operator
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         // Inequality operator. Returns Null if either operand is Null
206         public static bool operator !=(BaseHandle x, BaseHandle y)
207         {
208             return !(x == y);
209         }
210
211         // Logical AND operator for &&
212         // It's possible when doing a && this function (opBitwiseAnd) is never called due
213         // to short circuiting. E.g.
214         // If you perform x && y What actually is called is
215         // BaseHandle.op_False( x ) ? BaseHandle.op_True( x ) : BaseHandle.opTrue( BaseHandle.opBitwiseAnd(x,y) )
216         //
217         public static BaseHandle operator &(BaseHandle x, BaseHandle y)
218         {
219             if (x == y)
220             {
221                 return x;
222             }
223             return null;
224         }
225
226         // Logical OR operator for ||
227         // It's possible when doing a || this function (opBitwiseOr) is never called due
228         // to short circuiting. E.g.
229         // If you perform x || y What actually is called is
230         // BaseHandle.op_True( x ) ? BaseHandle.op_True( x ) : BaseHandle.opTrue( BaseHandle.opBitwiseOr(x,y) )
231         public static BaseHandle operator |(BaseHandle x, BaseHandle y)
232         {
233             if (!BaseHandle.ReferenceEquals(x, null) || !BaseHandle.ReferenceEquals(y, null))
234             {
235                 if (x.HasBody())
236                 {
237                     return x;
238                 }
239                 if (y.HasBody())
240                 {
241                     return y;
242                 }
243                 return null;
244             }
245             return null;
246         }
247
248         // Logical ! operator
249         public static bool operator !(BaseHandle x)
250         {
251             // if the C# object is null, return true
252             if (BaseHandle.ReferenceEquals(x, null))
253             {
254                 return true;
255             }
256             if (x.HasBody())
257             {
258                 return false;
259             }
260             return true;
261         }
262
263
264         public BaseHandle() : this(NDalicPINVOKE.new_BaseHandle__SWIG_1())
265         {
266             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
267         }
268
269         public BaseHandle(BaseHandle handle) : this(NDalicPINVOKE.new_BaseHandle__SWIG_2(BaseHandle.getCPtr(handle)))
270         {
271             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
272         }
273
274
275         public bool DoAction(string actionName, PropertyMap attributes)
276         {
277             bool ret = NDalicPINVOKE.BaseHandle_DoAction(swigCPtr, actionName, PropertyMap.getCPtr(attributes));
278             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
279             return ret;
280         }
281
282         public string GetTypeName()
283         {
284             string ret = NDalicPINVOKE.BaseHandle_GetTypeName(swigCPtr);
285             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
286             return ret;
287         }
288
289         public bool GetTypeInfo(TypeInfo info)
290         {
291             bool ret = NDalicPINVOKE.BaseHandle_GetTypeInfo(swigCPtr, TypeInfo.getCPtr(info));
292             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
293             return ret;
294         }
295
296
297         public void Reset()
298         {
299             NDalicPINVOKE.BaseHandle_Reset(swigCPtr);
300             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
301         }
302
303         public bool EqualTo(BaseHandle rhs)
304         {
305             bool ret = NDalicPINVOKE.BaseHandle_EqualTo(swigCPtr, BaseHandle.getCPtr(rhs));
306             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
307             return ret;
308         }
309
310         public bool NotEqualTo(BaseHandle rhs)
311         {
312             bool ret = NDalicPINVOKE.BaseHandle_NotEqualTo(swigCPtr, BaseHandle.getCPtr(rhs));
313             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
314             return ret;
315         }
316
317         internal RefObject GetObjectPtr()
318         {
319             global::System.IntPtr cPtr = NDalicPINVOKE.BaseHandle_GetObjectPtr(swigCPtr);
320             RefObject ret = (cPtr == global::System.IntPtr.Zero) ? null : new RefObject(cPtr, false);
321             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
322             return ret;
323         }
324
325         public bool HasBody()
326         {
327             if (disposed == true)
328             {
329                 return false;
330             }
331
332             bool ret = NDalicPINVOKE.BaseHandle_HasBody(swigCPtr);
333             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
334             return ret;
335         }
336
337         public bool IsEqual(BaseHandle rhs)
338         {
339             if (disposed == true)
340             {
341                 return false;
342             }
343
344             bool ret = NDalicPINVOKE.BaseHandle_IsEqual(swigCPtr, BaseHandle.getCPtr(rhs));
345             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
346             return ret;
347         }
348
349     }
350
351 }
352