sync with tizen branch to finalize API
[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             //Unreference this instance from Registry.
92             Registry.Unregister(this);
93
94             if (swigCPtr.Handle != global::System.IntPtr.Zero)
95             {
96                 if (swigCMemOwn)
97                 {
98                     swigCMemOwn = false;
99                     NDalicPINVOKE.delete_BaseHandle(swigCPtr);
100                 }
101                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
102             }
103
104             disposed = true;
105         }
106
107
108
109         // Returns the bool value true to indicate that an operand is true and returns false otherwise.
110         public static bool operator true(BaseHandle handle)
111         {
112             // if the C# object is null, return false
113             if (BaseHandle.ReferenceEquals(handle, null))
114             {
115                 return false;
116             }
117             // returns true if the handle has a body, false otherwise
118             return handle.HasBody();
119         }
120
121         // Returns the bool false  to indicate that an operand is false and returns true otherwise.
122         public static bool operator false(BaseHandle handle)
123         {
124             // if the C# object is null, return true
125             if (BaseHandle.ReferenceEquals(handle, null))
126             {
127                 return true;
128             }
129             return !handle.HasBody();
130         }
131
132         // Explicit conversion from Handle to bool.
133         public static explicit operator bool(BaseHandle handle)
134         {
135             // if the C# object is null, return false
136             if (BaseHandle.ReferenceEquals(handle, null))
137             {
138                 return false;
139             }
140             // returns true if the handle has a body, false otherwise
141             return handle.HasBody();
142         }
143
144         // Equality operator
145         public static bool operator ==(BaseHandle x, BaseHandle y)
146         {
147             // if the C# objects are the same return true
148             if (BaseHandle.ReferenceEquals(x, y))
149             {
150                 return true;
151             }
152             if (!BaseHandle.ReferenceEquals(x, null) && !BaseHandle.ReferenceEquals(y, null))
153             {
154                 // drop into native code to see if both handles point to the same body
155                 return x.IsEqual(y);
156             }
157
158             if (BaseHandle.ReferenceEquals(x, null) && !BaseHandle.ReferenceEquals(y, null))
159             {
160                 if (y.HasBody()) return false;
161                 else return true;
162             }
163             if (!BaseHandle.ReferenceEquals(x, null) && BaseHandle.ReferenceEquals(y, null))
164             {
165                 if (x.HasBody()) return false;
166                 else return true;
167             }
168
169             return false;
170         }
171
172         // Inequality operator. Returns Null if either operand is Null
173         public static bool operator !=(BaseHandle x, BaseHandle y)
174         {
175             return !(x == y);
176         }
177
178         // Logical AND operator for &&
179         // It's possible when doing a && this function (opBitwiseAnd) is never called due
180         // to short circuiting. E.g.
181         // If you perform x && y What actually is called is
182         // BaseHandle.op_False( x ) ? BaseHandle.op_True( x ) : BaseHandle.opTrue( BaseHandle.opBitwiseAnd(x,y) )
183         //
184         public static BaseHandle operator &(BaseHandle x, BaseHandle y)
185         {
186             if (x == y)
187             {
188                 return x;
189             }
190             return null;
191         }
192
193         // Logical OR operator for ||
194         // It's possible when doing a || this function (opBitwiseOr) is never called due
195         // to short circuiting. E.g.
196         // If you perform x || y What actually is called is
197         // BaseHandle.op_True( x ) ? BaseHandle.op_True( x ) : BaseHandle.opTrue( BaseHandle.opBitwiseOr(x,y) )
198         public static BaseHandle operator |(BaseHandle x, BaseHandle y)
199         {
200             if (!BaseHandle.ReferenceEquals(x, null) || !BaseHandle.ReferenceEquals(y, null))
201             {
202                 if (x.HasBody())
203                 {
204                     return x;
205                 }
206                 if (y.HasBody())
207                 {
208                     return y;
209                 }
210                 return null;
211             }
212             return null;
213         }
214
215         // Logical ! operator
216         public static bool operator !(BaseHandle x)
217         {
218             // if the C# object is null, return true
219             if (BaseHandle.ReferenceEquals(x, null))
220             {
221                 return true;
222             }
223             if (x.HasBody())
224             {
225                 return false;
226             }
227             return true;
228         }
229
230
231         public BaseHandle() : this(NDalicPINVOKE.new_BaseHandle__SWIG_1(), true)
232         {
233             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
234         }
235
236         public BaseHandle(BaseHandle handle) : this(NDalicPINVOKE.new_BaseHandle__SWIG_2(BaseHandle.getCPtr(handle)), true)
237         {
238             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
239         }
240
241
242         public bool DoAction(string actionName, PropertyMap attributes)
243         {
244             bool ret = NDalicPINVOKE.BaseHandle_DoAction(swigCPtr, actionName, PropertyMap.getCPtr(attributes));
245             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
246             return ret;
247         }
248
249         public string GetTypeName()
250         {
251             string ret = NDalicPINVOKE.BaseHandle_GetTypeName(swigCPtr);
252             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
253             return ret;
254         }
255
256         public bool GetTypeInfo(TypeInfo info)
257         {
258             bool ret = NDalicPINVOKE.BaseHandle_GetTypeInfo(swigCPtr, TypeInfo.getCPtr(info));
259             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
260             return ret;
261         }
262
263
264         public void Reset()
265         {
266             NDalicPINVOKE.BaseHandle_Reset(swigCPtr);
267             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
268         }
269
270         public bool EqualTo(BaseHandle rhs)
271         {
272             bool ret = NDalicPINVOKE.BaseHandle_EqualTo(swigCPtr, BaseHandle.getCPtr(rhs));
273             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
274             return ret;
275         }
276
277         public bool NotEqualTo(BaseHandle rhs)
278         {
279             bool ret = NDalicPINVOKE.BaseHandle_NotEqualTo(swigCPtr, BaseHandle.getCPtr(rhs));
280             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
281             return ret;
282         }
283
284         internal RefObject GetObjectPtr()
285         {
286             global::System.IntPtr cPtr = NDalicPINVOKE.BaseHandle_GetObjectPtr(swigCPtr);
287             RefObject ret = (cPtr == global::System.IntPtr.Zero) ? null : new RefObject(cPtr, false);
288             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
289             return ret;
290         }
291
292         public bool HasBody()
293         {
294             bool ret = NDalicPINVOKE.BaseHandle_HasBody(swigCPtr);
295             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
296             return ret;
297         }
298
299         public bool IsEqual(BaseHandle rhs)
300         {
301             bool ret = NDalicPINVOKE.BaseHandle_IsEqual(swigCPtr, BaseHandle.getCPtr(rhs));
302             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
303             return ret;
304         }
305
306     }
307
308 }
309