[NUI] Fix Svace issue (#949)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / ItemFactory.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 using System.Reflection;
19
20 namespace Tizen.NUI
21 {
22     using Tizen.NUI.BaseComponents;
23     internal class ItemFactory : global::System.IDisposable
24     {
25         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
26         protected bool swigCMemOwn;
27
28         internal ItemFactory(global::System.IntPtr cPtr, bool cMemoryOwn)
29         {
30             swigCMemOwn = cMemoryOwn;
31             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
32         }
33
34         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(ItemFactory 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
45         ~ItemFactory()
46         {
47             if (!isDisposeQueued)
48             {
49                 isDisposeQueued = true;
50                 DisposeQueue.Instance.Add(this);
51             }
52         }
53
54         public void Dispose()
55         {
56             //Throw excpetion if Dispose() is called in separate thread.
57             if (!Window.IsInstalled())
58             {
59                 throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
60             }
61
62             if (isDisposeQueued)
63             {
64                 Dispose(DisposeTypes.Implicit);
65             }
66             else
67             {
68                 Dispose(DisposeTypes.Explicit);
69                 System.GC.SuppressFinalize(this);
70             }
71         }
72
73         protected virtual void Dispose(DisposeTypes type)
74         {
75             if (disposed)
76             {
77                 return;
78             }
79
80             if (type == DisposeTypes.Explicit)
81             {
82                 //Called by User
83                 //Release your own managed resources here.
84                 //You should release all of your own disposable objects here.
85
86             }
87
88             //Release your own unmanaged resources here.
89             //You should not access any managed member here except static instance.
90             //because the execution order of Finalizes is non-deterministic.
91
92             if (swigCPtr.Handle != global::System.IntPtr.Zero)
93             {
94                 if (swigCMemOwn)
95                 {
96                     swigCMemOwn = false;
97                     Interop.ItemFactory.delete_ItemFactory(swigCPtr);
98                 }
99                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
100             }
101
102             disposed = true;
103         }
104
105         public virtual uint GetNumberOfItems()
106         {
107             uint ret = Interop.ItemFactory.ItemFactory_GetNumberOfItems(swigCPtr);
108             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
109             return ret;
110         }
111
112         public virtual View NewItem(uint itemId)
113         {
114             View ret = new View(Interop.ItemFactory.ItemFactory_NewItem(swigCPtr, itemId), true);
115             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
116             return ret;
117         }
118
119         public virtual void ItemReleased(uint itemId, View view)
120         {
121             if (SwigDerivedClassHasMethod("ItemReleased", swigMethodTypes2)) Interop.ItemFactory.ItemFactory_ItemReleasedSwigExplicitItemFactory(swigCPtr, itemId, View.getCPtr(view)); else Interop.ItemFactory.ItemFactory_ItemReleased(swigCPtr, itemId, View.getCPtr(view));
122             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
123         }
124
125         public ItemFactory() : this(Interop.ItemFactory.new_ItemFactory(), true)
126         {
127             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
128             SwigDirectorConnect();
129         }
130
131         private void SwigDirectorConnect()
132         {
133             if (SwigDerivedClassHasMethod("GetNumberOfItems", swigMethodTypes0))
134                 swigDelegate0 = new SwigDelegateItemFactory_0(SwigDirectorGetNumberOfItems);
135             if (SwigDerivedClassHasMethod("NewItem", swigMethodTypes1))
136                 swigDelegate1 = new SwigDelegateItemFactory_1(SwigDirectorNewItem);
137             if (SwigDerivedClassHasMethod("ItemReleased", swigMethodTypes2))
138                 swigDelegate2 = new SwigDelegateItemFactory_2(SwigDirectorItemReleased);
139             Interop.ItemFactory.ItemFactory_director_connect(swigCPtr, swigDelegate0, swigDelegate1, swigDelegate2);
140         }
141
142         private bool SwigDerivedClassHasMethod(string methodName, global::System.Type[] methodTypes)
143         {
144             global::System.Reflection.MethodInfo methodInfo = this.GetType().GetMethod(methodName, methodTypes);
145             bool hasDerivedMethod = this.GetType().GetTypeInfo().IsSubclassOf(typeof(ItemFactory));
146             NUILog.Debug("hasDerivedMethod=" + hasDerivedMethod);
147             return hasDerivedMethod && (methodInfo != null);
148         }
149
150         private uint SwigDirectorGetNumberOfItems()
151         {
152             return GetNumberOfItems();
153         }
154
155         private global::System.IntPtr SwigDirectorNewItem(uint itemId)
156         {
157             return View.getCPtr(NewItem(itemId)).Handle;
158         }
159
160         private void SwigDirectorItemReleased(uint itemId, global::System.IntPtr actor)
161         {
162             ItemReleased(itemId, new View(actor, true));
163         }
164
165         /// <since_tizen> 3 </since_tizen>
166         public delegate uint SwigDelegateItemFactory_0();
167         /// <since_tizen> 3 </since_tizen>
168         public delegate global::System.IntPtr SwigDelegateItemFactory_1(uint itemId);
169         /// <since_tizen> 3 </since_tizen>
170         public delegate void SwigDelegateItemFactory_2(uint itemId, global::System.IntPtr actor);
171
172         private SwigDelegateItemFactory_0 swigDelegate0;
173         private SwigDelegateItemFactory_1 swigDelegate1;
174         private SwigDelegateItemFactory_2 swigDelegate2;
175
176         private static global::System.Type[] swigMethodTypes0 = new global::System.Type[] { };
177         private static global::System.Type[] swigMethodTypes1 = new global::System.Type[] { typeof(uint) };
178         private static global::System.Type[] swigMethodTypes2 = new global::System.Type[] { typeof(uint), typeof(View) };
179     }
180
181 }