bbc687ed7a75eea7a1c6807664edd858176dee89
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / EventThreadCallback.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     internal class EventThreadCallback : global::System.IDisposable
22     {
23         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
24         protected bool swigCMemOwn;
25         /// <since_tizen> 3 </since_tizen>
26         public delegate void CallbackDelegate();
27
28         internal EventThreadCallback(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(EventThreadCallback 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         ~EventThreadCallback()
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                     //NDalicManualPINVOKE.delete_EventThreadCallback(swigCPtr);
98                 }
99                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
100             }
101
102             disposed = true;
103         }
104
105         public EventThreadCallback(CallbackDelegate func) : this(Interop.EventThreadCallback.new_EventThreadCallback(func), true)
106         {
107             if (NDalicPINVOKE.SWIGPendingException.Pending)
108                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
109         }
110
111         public void Trigger()
112         {
113             if ((System.IntPtr)swigCPtr != global::System.IntPtr.Zero)
114             {
115                 Interop.EventThreadCallback.EventThreadCallback_Trigger(swigCPtr);
116             }
117
118             if (NDalicPINVOKE.SWIGPendingException.Pending)
119                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
120         }
121     }
122
123 }