[NUI] Add obsolete tag (#2601)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / Common / Disposable.cs
1 /*
2  * Copyright(c) 2019 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 using System;
18 using System.ComponentModel;
19
20 namespace Tizen.NUI
21 {
22     /// <summary>
23     /// Disposable class.
24     /// </summary>
25     /// <since_tizen> 6 </since_tizen>
26     public class Disposable : global::System.IDisposable
27     {
28         /// <summary>
29         /// A Flag to check if it is already disposed.
30         /// </summary>
31         /// <since_tizen> 6 </since_tizen>
32         [Obsolete("Deprecated in API9, Will be removed in API11, Please use Disposed")]
33         [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1051:Do not declare visible instance fields", Justification = "<Pending>")]
34         protected bool disposed = false;
35
36         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
37         private bool swigCMemOwn { get; set; }
38         private bool isDisposeQueued = false;
39
40         /// <summary>
41         /// Create an instance of Disposable.
42         /// </summary>
43         /// <since_tizen> 6 </since_tizen>
44         public Disposable()
45         {
46             swigCMemOwn = false;
47             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
48         }
49
50         /// This will not be public.
51         [EditorBrowsable(EditorBrowsableState.Never)]
52         public Disposable(global::System.IntPtr cPtr, bool cMemoryOwn)
53         {
54             swigCMemOwn = cMemoryOwn;
55             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
56         }
57
58         /// <summary>
59         /// Dispose.
60         /// </summary>
61         /// <since_tizen> 6 </since_tizen>
62         ~Disposable() => Dispose(false);
63
64         /// <summary>
65         /// Dispose.
66         /// </summary>
67         /// <since_tizen> 6 </since_tizen>
68         public void Dispose()
69         {
70             Dispose(true);
71             System.GC.SuppressFinalize(this);
72         }
73
74         /// <summary>
75         /// Hidden API (Inhouse API).
76         /// Dispose. 
77         /// </summary>
78         /// <remarks>
79         /// Following the guide of https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose.
80         /// This will replace "protected virtual void Dispose(DisposeTypes type)" which is exactly same in functionality.
81         /// </remarks>
82         /// <param name="disposing">true in order to free managed objects</param>
83         // Protected implementation of Dispose pattern.
84         [EditorBrowsable(EditorBrowsableState.Never)]
85         protected virtual void Dispose(bool disposing)
86         {
87             if (disposed)
88             {
89                 return;
90             }
91
92             if (disposing)
93             {
94                 // TODO: dispose managed state (managed objects).
95                 // Explicit call. user calls Dispose()
96
97                 //Throw excpetion if Dispose() is called in separate thread.
98                 if (!Window.IsInstalled())
99                 {
100                     throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
101                 }
102
103                 if (isDisposeQueued)
104                 {
105                     Dispose(DisposeTypes.Implicit);
106                 }
107                 else
108                 {
109                     Dispose(DisposeTypes.Explicit);
110                 }
111             }
112             else
113             {
114                 // Implicit call. user doesn't call Dispose(), so this object is added into DisposeQueue to be disposed automatically.
115                 if (!isDisposeQueued)
116                 {
117                     isDisposeQueued = true;
118                     DisposeQueue.Instance.Add(this);
119                 }
120             }
121
122             // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
123             // TODO: set large fields to null.
124         }
125
126         /// <summary>
127         /// Dispose.
128         /// </summary>
129         /// <since_tizen> 6 </since_tizen>
130         protected virtual void Dispose(DisposeTypes type)
131         {
132             if (disposed)
133             {
134                 return;
135             }
136
137             if (type == DisposeTypes.Explicit)
138             {
139                 //Called by User
140                 //Release your own managed resources here.
141                 //You should release all of your own disposable objects here.
142             }
143
144             //Release your own unmanaged resources here.
145             //You should not access any managed member here except static instance.
146             //because the execution order of Finalizes is non-deterministic.
147             if (swigCPtr.Handle != global::System.IntPtr.Zero)
148             {
149                 if (SwigCMemOwn)
150                 {
151                     swigCMemOwn = false;
152                     ReleaseSwigCPtr(swigCPtr);
153                 }
154                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
155             }
156
157             disposed = true;
158         }
159
160         /// <summary>
161         /// Release swigCPtr.
162         /// </summary>
163         /// <since_tizen> 6 </since_tizen>
164         /// This will not be public opened.
165         [EditorBrowsable(EditorBrowsableState.Never)]
166         protected virtual void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
167         {
168         }
169
170         internal global::System.Runtime.InteropServices.HandleRef SwigCPtr
171         {
172             get => swigCPtr;
173             set
174             {
175                 swigCPtr = value;
176             }
177         }
178
179         internal bool SwigCMemOwn => swigCMemOwn;
180
181         /// <summary>
182         /// A Flag to check if it is already disposed.
183         /// </summary>
184         [EditorBrowsable(EditorBrowsableState.Never)]
185         protected internal bool Disposed => disposed;
186     }
187 }