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