Apply Dispose pattern to NUI.
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Size2D.cs
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 namespace Tizen.NUI
19 {
20
21     /// <summary>
22     /// A two dimensional size
23     /// </summary>
24     public class Size2D : global::System.IDisposable
25     {
26         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
27         protected bool swigCMemOwn;
28
29         internal Size2D(global::System.IntPtr cPtr, bool cMemoryOwn)
30         {
31             swigCMemOwn = cMemoryOwn;
32             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
33         }
34
35         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Size2D obj)
36         {
37             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
38         }
39
40         //NUI Dispose Pattern written by Jinwoo Nam(jjw.nam) 
41
42         //A Flag to check who called Dispose(). (By User or DisposeQueue)
43         private bool isDisposeQueued = false;
44         //A Flat to check if it is already disposed.
45         protected bool disposed = false;
46
47         ~Size2D()
48         {
49             if(!isDisposeQueued)
50             {
51                 isDisposeQueued = true;
52                 DisposeQueue.Instance.Add(this);
53             }
54         }
55
56         /// <summary>
57         /// Dispose
58         /// </summary>
59         public void Dispose()
60         {
61             //Throw excpetion if Dispose() is called in separate thread.
62             if (!Window.IsInstalled())
63             {
64                 throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
65             }
66
67             if (isDisposeQueued)
68             {
69                 Dispose(DisposeTypes.Implicit);
70             }
71             else
72             {
73                 Dispose(DisposeTypes.Explicit);
74                 System.GC.SuppressFinalize(this);
75             }
76         }
77
78         protected virtual void Dispose(DisposeTypes type)
79         {
80             if (disposed)
81             {
82                 return;
83             }
84
85             if(type == DisposeTypes.Explicit)
86             {
87                 //Called by User
88                 //Release your own managed resources here.
89                 //You should release all of your own disposable objects here.
90             }
91
92             //Release your own unmanaged resources here.
93             //You should not access any managed member here except static instance.
94             //because the execution order of Finalizes is non-deterministic.
95
96             if (swigCPtr.Handle != global::System.IntPtr.Zero)
97             {
98                 if (swigCMemOwn)
99                 {
100                     swigCMemOwn = false;
101                     NDalicPINVOKE.delete_Vector2(swigCPtr);
102                 }
103                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
104             }
105             disposed = true;
106         }
107
108         /// <summary>
109         /// Addition operator for A+B
110         /// </summary>
111         /// <param name="arg1">Size, A</param>
112         /// <param name="arg2">Size to assign, B</param>
113         /// <returns>A Size containing the result of the addition</returns>
114         public static Size2D operator +(Size2D arg1, Size2D arg2)
115         {
116             return arg1.Add(arg2);
117         }
118
119         /// <summary>
120         /// Subtraction operator for A-B
121         /// </summary>
122         /// <param name="arg1">Size, A</param>
123         /// <param name="arg2">Size to subtract, B</param>
124         /// <returns>A Size containing the result of the subtraction</returns>
125         public static Size2D operator -(Size2D arg1, Size2D arg2)
126         {
127             return arg1.Subtract(arg2);
128         }
129
130         /// <summary>
131         /// Unary negation operator.
132         /// </summary>
133         /// <param name="arg1">Size for unary negation</param>
134         /// <returns>A Size containg the negation</returns>
135         public static Size2D operator -(Size2D arg1)
136         {
137             return arg1.Subtract();
138         }
139
140         /// <summary>
141         /// Multiplication operator.
142         /// </summary>
143         /// <param name="arg1">Size for multiplication</param>
144         /// <param name="arg2">The Size to multipl</param>
145         /// <returns>A Size containing the result of the multiplication</returns>
146         public static Size2D operator *(Size2D arg1, Size2D arg2)
147         {
148             return arg1.Multiply(arg2);
149         }
150
151         /// <summary>
152         /// Multiplication operator.
153         /// </summary>
154         /// <param name="arg1">Size for multiplication</param>
155         /// <param name="arg2">The int value to scale the Size</param>
156         /// <returns>A Size containing the result of the scaling</returns>
157
158         public static Size2D operator *(Size2D arg1, int arg2)
159         {
160             return arg1.Multiply(arg2);
161         }
162
163         /// <summary>
164         /// Division operator.
165         /// </summary>
166         /// <param name="arg1">Size for division</param>
167         /// <param name="arg2">The Size to divide</param>
168         /// <returns>A Size containing the result of the division></returns>
169         public static Size2D operator /(Size2D arg1, Size2D arg2)
170         {
171             return arg1.Divide(arg2);
172         }
173
174         /// <summary>
175         /// Division operator.
176         /// </summary>
177         /// <param name="arg1">Size for division</param>
178         /// <param name="arg2">The int value to scale the Size by</param>
179         /// <returns>A Size containing the result of the scaling</returns>
180         public static Size2D operator /(Size2D arg1, int arg2)
181         {
182             return arg1.Divide(arg2);
183         }
184
185         /// <summary>
186         /// Array subscript operator.
187         /// </summary>
188         /// <param name="index">Subscript index</param>
189         /// <returns>The float at the given index</returns>
190         public float this[uint index]
191         {
192             get
193             {
194                 return ValueOfIndex(index);
195             }
196         }
197
198         /// <summary>
199         /// Get Size from pointer.
200         /// </summary>
201         /// <param name="cPtr">Pointer of the Size</param>
202         /// <returns>Size</returns>
203         internal static Size2D GetSize2DFromPtr(global::System.IntPtr cPtr)
204         {
205             Size2D ret = new Size2D(cPtr, false);
206             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
207             return ret;
208         }
209
210         /// <summary>
211         /// Constructor.
212         /// </summary>
213         public Size2D() : this(NDalicPINVOKE.new_Vector2__SWIG_0(), true)
214         {
215             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
216         }
217
218         /// <summary>
219         /// Constructor.
220         /// </summary>
221         /// <param name="x">x (or width) component</param>
222         /// <param name="y">y (or height) component</param>
223         public Size2D(int x, int y) : this(NDalicPINVOKE.new_Vector2__SWIG_1((float)x, (float)y), true)
224         {
225             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
226         }
227
228         /// <summary>
229         /// Constructor.
230         /// </summary>
231         /// <param name="size">Size with x (width), y (height), and z (depth)</param>
232         public Size2D(Size size) : this(NDalicPINVOKE.new_Vector2__SWIG_3(Size.getCPtr(size)), true)
233         {
234             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
235         }
236
237         private Size2D Add(Size2D rhs)
238         {
239             Size2D ret = new Size2D(NDalicPINVOKE.Vector2_Add(swigCPtr, Size2D.getCPtr(rhs)), true);
240             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
241             return ret;
242         }
243
244         private Size2D Subtract(Size2D rhs)
245         {
246             Size2D ret = new Size2D(NDalicPINVOKE.Vector2_Subtract__SWIG_0(swigCPtr, Size2D.getCPtr(rhs)), true);
247             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
248             return ret;
249         }
250
251
252         private Size2D Multiply(Size2D rhs)
253         {
254             Size2D ret = new Size2D(NDalicPINVOKE.Vector2_Multiply__SWIG_0(swigCPtr, Size2D.getCPtr(rhs)), true);
255             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
256             return ret;
257         }
258
259         private Size2D Multiply(int rhs)
260         {
261             Size2D ret = new Size2D(NDalicPINVOKE.Vector2_Multiply__SWIG_1(swigCPtr, (float)rhs), true);
262             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
263             return ret;
264         }
265
266
267         private Size2D Divide(Size2D rhs)
268         {
269             Size2D ret = new Size2D(NDalicPINVOKE.Vector2_Divide__SWIG_0(swigCPtr, Size2D.getCPtr(rhs)), true);
270             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
271             return ret;
272         }
273
274         private Size2D Divide(int rhs)
275         {
276             Size2D ret = new Size2D(NDalicPINVOKE.Vector2_Divide__SWIG_1(swigCPtr, (float)rhs), true);
277             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
278             return ret;
279         }
280
281         private Size2D Subtract()
282         {
283             Size2D ret = new Size2D(NDalicPINVOKE.Vector2_Subtract__SWIG_1(swigCPtr), true);
284             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
285             return ret;
286         }
287
288         /// <summary>
289         /// Check equality.<br>
290         /// Utilizes appropriate machine epsilon values.<br>
291         /// </summary>
292         /// <param name="rhs">The Size to test against</param>
293         /// <returns>True if the Sizes are equal</returns>
294         public bool EqualTo(Size2D rhs)
295         {
296             bool ret = NDalicPINVOKE.Vector2_EqualTo(swigCPtr, Size2D.getCPtr(rhs));
297             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
298             return ret;
299         }
300
301         /// <summary>
302         /// Check inequality.<br>
303         /// Utilizes appropriate machine epsilon values.<br>
304         /// </summary>
305         /// <param name="rhs">The Size to test against</param>
306         /// <returns>True if the Sizes are not equal</returns>
307         public bool NotEqualTo(Size2D rhs)
308         {
309             bool ret = NDalicPINVOKE.Vector2_NotEqualTo(swigCPtr, Size2D.getCPtr(rhs));
310             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
311             return ret;
312         }
313
314         private int ValueOfIndex(uint index)
315         {
316             int ret = (int)NDalicPINVOKE.Vector2_ValueOfIndex__SWIG_0(swigCPtr, index);
317             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
318             return ret;
319         }
320
321         /// <summary>
322         /// Property for width component of Size
323         /// </summary>
324         public int Width
325         {
326             set
327             {
328                 NDalicPINVOKE.Vector2_Width_set(swigCPtr, (float)value);
329                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
330             }
331             get
332             {
333                 float ret = NDalicPINVOKE.Vector2_Width_get(swigCPtr);
334                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
335                 return (int)ret;
336             }
337         }
338
339         /// <summary>
340         /// Property for height component of Size
341         /// </summary>
342         public int Height
343         {
344             set
345             {
346                 NDalicPINVOKE.Vector2_Height_set(swigCPtr, (float)value);
347                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
348             }
349             get
350             {
351                 float ret = NDalicPINVOKE.Vector2_Height_get(swigCPtr);
352                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
353                 return (int)ret;
354             }
355         }
356
357         /// <summary>
358         /// Type cast operator, Size2D to Vector2.
359         /// </summary>
360         /// <param name="size">Object of Size2D type</param>
361         public static implicit operator Vector2(Size2D size)
362         {
363             return new Vector2((float)size.Width, (float)size.Height);
364         }
365
366         /// <summary>
367         /// Type cast operator, Vector2 to Size2D type.
368         /// </summary>
369         /// <param name="vec">Object of Vector2 type</param>
370         public static implicit operator Size2D(Vector2 vec)
371         {
372             return new Size2D((int)vec.X, (int)vec.Y);
373         }
374
375     }
376
377 }
378