ef4789681171c8fb46cfa5c95a2f6fe2d597009f
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / SignalObserver.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 using System;
17 using System.Runtime.InteropServices;
18 using System.ComponentModel;
19
20
21 namespace Tizen.NUI
22 {
23     //Please do not use! this will be internal
24     [EditorBrowsable(EditorBrowsableState.Never)]
25     public class SignalObserver : global::System.IDisposable
26     {
27         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
28         protected bool swigCMemOwn;
29
30         internal SignalObserver(global::System.IntPtr cPtr, bool cMemoryOwn)
31         {
32             swigCMemOwn = cMemoryOwn;
33             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
34         }
35
36         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(SignalObserver obj)
37         {
38             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
39         }
40
41         //A Flag to check who called Dispose(). (By User or DisposeQueue)
42         private bool isDisposeQueued = false;
43         //A Flat to check if it is already disposed.
44         protected bool disposed = false;
45
46
47         ~SignalObserver()
48         {
49             if (!isDisposeQueued)
50             {
51                 isDisposeQueued = true;
52                 DisposeQueue.Instance.Add(this);
53             }
54         }
55
56         public void Dispose()
57         {
58             //Throw excpetion if Dispose() is called in separate thread.
59             if (!Window.IsInstalled())
60             {
61                 throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
62             }
63
64             if (isDisposeQueued)
65             {
66                 Dispose(DisposeTypes.Implicit);
67             }
68             else
69             {
70                 Dispose(DisposeTypes.Explicit);
71                 System.GC.SuppressFinalize(this);
72             }
73         }
74
75         protected virtual void Dispose(DisposeTypes type)
76         {
77             if (disposed)
78             {
79                 return;
80             }
81
82             if (type == DisposeTypes.Explicit)
83             {
84                 //Called by User
85                 //Release your own managed resources here.
86                 //You should release all of your own disposable objects here.
87
88             }
89
90             //Release your own unmanaged resources here.
91             //You should not access any managed member here except static instance.
92             //because the execution order of Finalizes is non-deterministic.
93
94             if (swigCPtr.Handle != global::System.IntPtr.Zero)
95             {
96                 if (swigCMemOwn)
97                 {
98                     swigCMemOwn = false;
99                     NDalicPINVOKE.delete_SignalObserver(swigCPtr);
100                 }
101                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
102             }
103
104             disposed = true;
105         }
106
107         public virtual void SignalDisconnected(SlotObserver slotObserver, SWIGTYPE_p_Dali__CallbackBase callback)
108         {
109             NDalicPINVOKE.SignalObserver_SignalDisconnected(swigCPtr, SlotObserver.getCPtr(slotObserver), SWIGTYPE_p_Dali__CallbackBase.getCPtr(callback));
110             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
111         }
112
113     }
114
115 }