[Tizen] Create another native handle to make sure Registry.Unregister works fine.
[platform/core/csapi/nui.git] / 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             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
30
31             // using copy constructor to create another native handle so Registry.Unregister works fine.
32             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, NDalicPINVOKE.new_BaseHandle__SWIG_2(swigCPtr));
33
34             if (_registerMe)
35             {
36                 // Register this instance of BaseHandle in the registry.
37                 Registry.Register(this);
38             }
39         }
40
41         internal BaseHandle(global::System.IntPtr cPtr)
42         {
43             _registerMe = swigCMemOwn = true;
44             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
45
46             // using copy constructor to create another native handle so Registry.Unregister works fine.
47             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, NDalicPINVOKE.new_BaseHandle__SWIG_2(swigCPtr));
48
49             // Register this instance of BaseHandle in the registry.
50             Registry.Register(this);
51         }
52
53         internal BaseHandle(global::System.IntPtr cPtr)
54         {
55             swigCMemOwn = true;
56             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
57
58             // Register this instance of BaseHandle in the registry.
59             Registry.Register(this);
60         }
61
62         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(BaseHandle obj)
63         {
64             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
65         }
66
67         //A Flag to check who called Dispose(). (By User or DisposeQueue)
68         private bool isDisposeQueued = false;
69         //A Flat to check if it is already disposed.
70         protected bool disposed = false;
71
72         ~BaseHandle()
73         {
74             if (!isDisposeQueued)
75             {
76                 isDisposeQueued = true;
77                 DisposeQueue.Instance.Add(this);
78             }
79         }
80
81         public void Dispose()
82         {
83             //Throw excpetion if Dispose() is called in separate thread.
84             if (!Window.IsInstalled())
85             {
86                 throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
87             }
88
89             if (isDisposeQueued)
90             {
91                 Dispose(DisposeTypes.Implicit);
92             }
93             else
94             {
95                 Dispose(DisposeTypes.Explicit);
96                 System.GC.SuppressFinalize(this);
97             }
98         }
99
100         protected virtual void Dispose(DisposeTypes type)
101         {
102             if (disposed)
103             {
104                 return;
105             }
106
107             if (type == DisposeTypes.Explicit)
108             {
109                 //Called by User
110                 //Release your own managed resources here.
111                 //You should release all of your own disposable objects here.
112
113             }
114
115             //Release your own unmanaged resources here.
116             //You should not access any managed member here except static instance.
117             //because the execution order of Finalizes is non-deterministic.
118
119             //Unreference this instance from Registry.
120             if (_registerMe)
121             {
122                 Registry.Unregister(this);
123             }
124
125             if (swigCPtr.Handle != global::System.IntPtr.Zero)
126             {
127                 swigCMemOwn = false;
128                 NDalicPINVOKE.delete_BaseHandle(swigCPtr);
129                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
130             }
131
132             disposed = true;
133         }
134
135
136
137         // Returns the bool value true to indicate that an operand is true and returns false otherwise.
138         public static bool operator true(BaseHandle handle)
139         {
140             // if the C# object is null, return false
141             if (BaseHandle.ReferenceEquals(handle, null))
142             {
143                 return false;
144             }
145             // returns true if the handle has a body, false otherwise
146             return handle.HasBody();
147         }
148
149         // Returns the bool false  to indicate that an operand is false and returns true otherwise.
150         public static bool operator false(BaseHandle handle)
151         {
152             // if the C# object is null, return true
153             if (BaseHandle.ReferenceEquals(handle, null))
154             {
155                 return true;
156             }
157             return !handle.HasBody();
158         }
159
160         // Explicit conversion from Handle to bool.
161         public static explicit operator bool(BaseHandle handle)
162         {
163             // if the C# object is null, return false
164             if (BaseHandle.ReferenceEquals(handle, null))
165             {
166                 return false;
167             }
168             // returns true if the handle has a body, false otherwise
169             return handle.HasBody();
170         }
171
172         // Equality operator
173         public static bool operator ==(BaseHandle x, BaseHandle y)
174         {
175             // if the C# objects are the same return true
176             if (BaseHandle.ReferenceEquals(x, y))
177             {
178                 return true;
179             }
180             if (!BaseHandle.ReferenceEquals(x, null) && !BaseHandle.ReferenceEquals(y, null))
181             {
182                 // drop into native code to see if both handles point to the same body
183                 return x.IsEqual(y);
184             }
185
186             if (BaseHandle.ReferenceEquals(x, null) && !BaseHandle.ReferenceEquals(y, null))
187             {
188                 if (y.HasBody()) return false;
189                 else return true;
190             }
191             if (!BaseHandle.ReferenceEquals(x, null) && BaseHandle.ReferenceEquals(y, null))
192             {
193                 if (x.HasBody()) return false;
194                 else return true;
195             }
196
197             return false;
198         }
199
200         // Inequality operator. Returns Null if either operand is Null
201         public static bool operator !=(BaseHandle x, BaseHandle y)
202         {
203             return !(x == y);
204         }
205
206         // Logical AND operator for &&
207         // It's possible when doing a && this function (opBitwiseAnd) is never called due
208         // to short circuiting. E.g.
209         // If you perform x && y What actually is called is
210         // BaseHandle.op_False( x ) ? BaseHandle.op_True( x ) : BaseHandle.opTrue( BaseHandle.opBitwiseAnd(x,y) )
211         //
212         public static BaseHandle operator &(BaseHandle x, BaseHandle y)
213         {
214             if (x == y)
215             {
216                 return x;
217             }
218             return null;
219         }
220
221         // Logical OR operator for ||
222         // It's possible when doing a || this function (opBitwiseOr) is never called due
223         // to short circuiting. E.g.
224         // If you perform x || y What actually is called is
225         // BaseHandle.op_True( x ) ? BaseHandle.op_True( x ) : BaseHandle.opTrue( BaseHandle.opBitwiseOr(x,y) )
226         public static BaseHandle operator |(BaseHandle x, BaseHandle y)
227         {
228             if (!BaseHandle.ReferenceEquals(x, null) || !BaseHandle.ReferenceEquals(y, null))
229             {
230                 if (x.HasBody())
231                 {
232                     return x;
233                 }
234                 if (y.HasBody())
235                 {
236                     return y;
237                 }
238                 return null;
239             }
240             return null;
241         }
242
243         // Logical ! operator
244         public static bool operator !(BaseHandle x)
245         {
246             // if the C# object is null, return true
247             if (BaseHandle.ReferenceEquals(x, null))
248             {
249                 return true;
250             }
251             if (x.HasBody())
252             {
253                 return false;
254             }
255             return true;
256         }
257
258
259         public BaseHandle() : this(NDalicPINVOKE.new_BaseHandle__SWIG_1())
260         {
261             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
262         }
263
264         public BaseHandle(BaseHandle handle) : this(NDalicPINVOKE.new_BaseHandle__SWIG_2(BaseHandle.getCPtr(handle)))
265         {
266             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
267         }
268
269
270         public bool DoAction(string actionName, PropertyMap attributes)
271         {
272             bool ret = NDalicPINVOKE.BaseHandle_DoAction(swigCPtr, actionName, PropertyMap.getCPtr(attributes));
273             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
274             return ret;
275         }
276
277         public string GetTypeName()
278         {
279             string ret = NDalicPINVOKE.BaseHandle_GetTypeName(swigCPtr);
280             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
281             return ret;
282         }
283
284         public bool GetTypeInfo(TypeInfo info)
285         {
286             bool ret = NDalicPINVOKE.BaseHandle_GetTypeInfo(swigCPtr, TypeInfo.getCPtr(info));
287             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
288             return ret;
289         }
290
291
292         public void Reset()
293         {
294             NDalicPINVOKE.BaseHandle_Reset(swigCPtr);
295             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
296         }
297
298         public bool EqualTo(BaseHandle rhs)
299         {
300             bool ret = NDalicPINVOKE.BaseHandle_EqualTo(swigCPtr, BaseHandle.getCPtr(rhs));
301             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
302             return ret;
303         }
304
305         public bool NotEqualTo(BaseHandle rhs)
306         {
307             bool ret = NDalicPINVOKE.BaseHandle_NotEqualTo(swigCPtr, BaseHandle.getCPtr(rhs));
308             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
309             return ret;
310         }
311
312         internal RefObject GetObjectPtr()
313         {
314             global::System.IntPtr cPtr = NDalicPINVOKE.BaseHandle_GetObjectPtr(swigCPtr);
315             RefObject ret = (cPtr == global::System.IntPtr.Zero) ? null : new RefObject(cPtr, false);
316             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
317             return ret;
318         }
319
320         public bool HasBody()
321         {
322             bool ret = NDalicPINVOKE.BaseHandle_HasBody(swigCPtr);
323             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
324             return ret;
325         }
326
327         public bool IsEqual(BaseHandle rhs)
328         {
329             bool ret = NDalicPINVOKE.BaseHandle_IsEqual(swigCPtr, BaseHandle.getCPtr(rhs));
330             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
331             return ret;
332         }
333
334     }
335
336 }
337