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.
17 using System.ComponentModel;
24 /// <since_tizen> 6 </since_tizen>
25 public class Disposable : global::System.IDisposable
28 /// A Flag to check if it is already disposed.
30 /// <since_tizen> 6 </since_tizen>
31 protected bool disposed = false;
33 private global::System.Runtime.InteropServices.HandleRef swigCPtr;
34 private bool swigCMemOwn { get; set; }
35 private bool isDisposeQueued = false;
38 /// Create an instance of Disposable.
40 /// <since_tizen> 6 </since_tizen>
44 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
47 /// This will not be public.
48 [EditorBrowsable(EditorBrowsableState.Never)]
49 public Disposable(global::System.IntPtr cPtr, bool cMemoryOwn)
51 swigCMemOwn = cMemoryOwn;
52 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
58 /// <since_tizen> 6 </since_tizen>
59 ~Disposable() => Dispose(false);
64 /// <since_tizen> 6 </since_tizen>
68 System.GC.SuppressFinalize(this);
72 /// Hidden API (Inhouse API).
76 /// Following the guide of https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose.
77 /// This will replace "protected virtual void Dispose(DisposeTypes type)" which is exactly same in functionality.
79 /// <param name="disposing">true in order to free managed objects</param>
80 // Protected implementation of Dispose pattern.
81 [EditorBrowsable(EditorBrowsableState.Never)]
82 protected virtual void Dispose(bool disposing)
91 // TODO: dispose managed state (managed objects).
92 // Explicit call. user calls Dispose()
94 //Throw excpetion if Dispose() is called in separate thread.
95 if (!Window.IsInstalled())
97 throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
102 Dispose(DisposeTypes.Implicit);
106 Dispose(DisposeTypes.Explicit);
111 // Implicit call. user doesn't call Dispose(), so this object is added into DisposeQueue to be disposed automatically.
112 if (!isDisposeQueued)
114 isDisposeQueued = true;
115 DisposeQueue.Instance.Add(this);
119 // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
120 // TODO: set large fields to null.
126 /// <since_tizen> 6 </since_tizen>
127 protected virtual void Dispose(DisposeTypes type)
134 if (type == DisposeTypes.Explicit)
137 //Release your own managed resources here.
138 //You should release all of your own disposable objects here.
141 //Release your own unmanaged resources here.
142 //You should not access any managed member here except static instance.
143 //because the execution order of Finalizes is non-deterministic.
144 if (swigCPtr.Handle != global::System.IntPtr.Zero)
149 ReleaseSwigCPtr(swigCPtr);
151 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
158 /// Release swigCPtr.
160 /// <since_tizen> 6 </since_tizen>
161 /// This will not be public opened.
162 [EditorBrowsable(EditorBrowsableState.Never)]
163 protected virtual void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
167 internal global::System.Runtime.InteropServices.HandleRef SwigCPtr
176 internal bool SwigCMemOwn => swigCMemOwn;
179 /// A Flag to check if it is already disposed.
181 [EditorBrowsable(EditorBrowsableState.Never)]
182 protected internal bool Disposed => disposed;