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