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