2 * Copyright(c) 2019 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.ComponentModel;
25 /// <since_tizen> 6 </since_tizen>
26 public class Disposable : global::System.IDisposable
29 /// A Flag to check if it is already disposed.
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;
36 private global::System.Runtime.InteropServices.HandleRef swigCPtr;
37 private bool swigCMemOwn { get; set; }
38 private bool isDisposeQueued = false;
41 /// Create an instance of Disposable.
43 /// <since_tizen> 6 </since_tizen>
47 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
50 /// This will not be public.
51 [EditorBrowsable(EditorBrowsableState.Never)]
52 public Disposable(global::System.IntPtr cPtr, bool cMemoryOwn)
54 swigCMemOwn = cMemoryOwn;
55 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
61 /// <since_tizen> 6 </since_tizen>
62 ~Disposable() => Dispose(false);
67 /// <since_tizen> 6 </since_tizen>
71 System.GC.SuppressFinalize(this);
75 /// Hidden API (Inhouse API).
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.
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)
94 // TODO: dispose managed state (managed objects).
95 // Explicit call. user calls Dispose()
97 //Throw excpetion if Dispose() is called in separate thread.
98 if (!Window.IsInstalled())
100 throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
105 Dispose(DisposeTypes.Implicit);
109 Dispose(DisposeTypes.Explicit);
114 // Implicit call. user doesn't call Dispose(), so this object is added into DisposeQueue to be disposed automatically.
115 if (!isDisposeQueued)
117 isDisposeQueued = true;
118 DisposeQueue.Instance.Add(this);
122 // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
123 // TODO: set large fields to null.
129 /// <since_tizen> 6 </since_tizen>
130 protected virtual void Dispose(DisposeTypes type)
137 if (type == DisposeTypes.Explicit)
140 //Release your own managed resources here.
141 //You should release all of your own disposable objects here.
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)
152 ReleaseSwigCPtr(swigCPtr);
154 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
161 /// Release swigCPtr.
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)
170 internal global::System.Runtime.InteropServices.HandleRef SwigCPtr
179 internal bool SwigCMemOwn => swigCMemOwn;
182 /// A Flag to check if it is already disposed.
184 [EditorBrowsable(EditorBrowsableState.Never)]
185 protected internal bool Disposed => disposed;