Release 4.0.0-preview1-00243
[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         public override bool Equals(object o)
260         {
261             if(o == null)
262             {
263                 return false;
264             }
265             if(!(o is BaseHandle))
266             {
267                 return false;
268             }
269             BaseHandle b = (BaseHandle)o;
270             if (!BaseHandle.ReferenceEquals(this, null) && !BaseHandle.ReferenceEquals(b, null))
271             {
272                 // drop into native code to see if both handles point to the same body
273                 return this.IsEqual(b);
274             }
275
276             if (BaseHandle.ReferenceEquals(this, null) && !BaseHandle.ReferenceEquals(b, null))
277             {
278                 if (b.HasBody()) return false;
279                 else return true;
280             }
281             if (!BaseHandle.ReferenceEquals(this, null) && BaseHandle.ReferenceEquals(b, null))
282             {
283                 if (this.HasBody()) return false;
284                 else return true;
285             }
286
287             return false;
288         }
289
290         public override int GetHashCode()
291         {
292             return base.GetHashCode();
293         }
294
295         public BaseHandle() : this(NDalicPINVOKE.new_BaseHandle__SWIG_1())
296         {
297             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
298         }
299
300         public BaseHandle(BaseHandle handle) : this(NDalicPINVOKE.new_BaseHandle__SWIG_2(BaseHandle.getCPtr(handle)))
301         {
302             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
303         }
304
305
306         public bool DoAction(string actionName, PropertyMap attributes)
307         {
308             bool ret = NDalicPINVOKE.BaseHandle_DoAction(swigCPtr, actionName, PropertyMap.getCPtr(attributes));
309             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
310             return ret;
311         }
312
313         public string GetTypeName()
314         {
315             string ret = NDalicPINVOKE.BaseHandle_GetTypeName(swigCPtr);
316             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
317             return ret;
318         }
319
320         public bool GetTypeInfo(TypeInfo info)
321         {
322             bool ret = NDalicPINVOKE.BaseHandle_GetTypeInfo(swigCPtr, TypeInfo.getCPtr(info));
323             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
324             return ret;
325         }
326
327
328         public void Reset()
329         {
330             NDalicPINVOKE.BaseHandle_Reset(swigCPtr);
331             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
332         }
333
334         public bool EqualTo(BaseHandle rhs)
335         {
336             bool ret = NDalicPINVOKE.BaseHandle_EqualTo(swigCPtr, BaseHandle.getCPtr(rhs));
337             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
338             return ret;
339         }
340
341         public bool NotEqualTo(BaseHandle rhs)
342         {
343             bool ret = NDalicPINVOKE.BaseHandle_NotEqualTo(swigCPtr, BaseHandle.getCPtr(rhs));
344             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
345             return ret;
346         }
347
348         internal RefObject GetObjectPtr()
349         {
350             global::System.IntPtr cPtr = NDalicPINVOKE.BaseHandle_GetObjectPtr(swigCPtr);
351             RefObject ret = (cPtr == global::System.IntPtr.Zero) ? null : new RefObject(cPtr, false);
352             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
353             return ret;
354         }
355
356         public bool HasBody()
357         {
358             if (disposed == true)
359             {
360                 return false;
361             }
362
363             bool ret = NDalicPINVOKE.BaseHandle_HasBody(swigCPtr);
364             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
365             return ret;
366         }
367
368         public bool IsEqual(BaseHandle rhs)
369         {
370             if (disposed == true)
371             {
372                 return false;
373             }
374
375             bool ret = NDalicPINVOKE.BaseHandle_IsEqual(swigCPtr, BaseHandle.getCPtr(rhs));
376             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
377             return ret;
378         }
379
380         internal global::System.Runtime.InteropServices.HandleRef GetBaseHandleCPtrHandleRef
381         {
382             get
383             {
384                 return swigCPtr;
385             }
386         }
387
388     }
389
390 }
391