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