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