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
28 static internal void Preload()
30 // Do nothing. Just call for load static values.
34 /// The flag to check if it is already disposed of.
36 /// <since_tizen> 6 </since_tizen>
37 [Obsolete("Deprecated in API9, will be removed in API11, Use Disposed")]
38 [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1051:Do not declare visible instance fields", Justification = "<Pending>")]
39 protected bool disposed = false;
41 private global::System.Runtime.InteropServices.HandleRef swigCPtr;
42 private bool swigCMemOwn { get; set; }
43 private bool isDisposeQueued = false;
46 /// Create an instance of Disposable.
48 /// <since_tizen> 6 </since_tizen>
52 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
55 /// This will not be public.
56 [EditorBrowsable(EditorBrowsableState.Never)]
57 public Disposable(global::System.IntPtr cPtr, bool cMemoryOwn)
59 swigCMemOwn = cMemoryOwn;
60 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
66 /// <since_tizen> 6 </since_tizen>
67 ~Disposable() => Dispose(false);
72 /// <since_tizen> 6 </since_tizen>
77 Dispose(DisposeTypes.Implicit);
83 GC.SuppressFinalize(this);
86 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Disposable obj)
88 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.SwigCPtr;
92 /// Hidden API (Inhouse API).
96 /// Following the guide of https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose.
97 /// This will replace "protected virtual void Dispose(DisposeTypes type)" which is exactly same in functionality.
99 /// <param name="disposing">true in order to free managed objects</param>
100 // Protected implementation of Dispose pattern.
101 [EditorBrowsable(EditorBrowsableState.Never)]
102 protected virtual void Dispose(bool disposing)
111 // TODO: dispose managed state (managed objects).
112 // Explicit call. user calls Dispose()
116 Tizen.Log.Fatal("NUI", $"[Disposable]should not be here! (dead code) this will be removed!");
118 //to fix ArtApp black screen issue. this will be enabled after talking with ArtApp team and fixing it.
119 // throw new global::System.Exception($"[NUI] should not be here! (dead code) this will be removed!");
120 Dispose(DisposeTypes.Implicit);
124 Dispose(DisposeTypes.Explicit);
129 // Implicit call. user doesn't call Dispose(), so this object is added into DisposeQueue to be disposed automatically.
130 if (!isDisposeQueued)
132 isDisposeQueued = true;
133 DisposeQueue.Instance.Add(this);
137 // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
138 // TODO: set large fields to null.
144 /// <since_tizen> 6 </since_tizen>
145 protected virtual void Dispose(DisposeTypes type)
152 if (type == DisposeTypes.Explicit)
155 //Release your own managed resources here.
156 //You should release all of your own disposable objects here.
159 //Release your own unmanaged resources here.
160 //You should not access any managed member here except static instance.
161 //because the execution order of Finalizes is non-deterministic.
162 if (swigCPtr.Handle != global::System.IntPtr.Zero)
164 var nativeSwigCPtr = swigCPtr.Handle;
165 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
169 ReleaseSwigCPtr(new global::System.Runtime.InteropServices.HandleRef(this, nativeSwigCPtr));
177 /// Release swigCPtr.
179 /// <since_tizen> 6 </since_tizen>
180 /// This will not be public opened.
181 [EditorBrowsable(EditorBrowsableState.Never)]
182 protected virtual void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
186 internal global::System.Runtime.InteropServices.HandleRef SwigCPtr
195 internal bool SwigCMemOwn => swigCMemOwn;
198 /// The flag to check if it is already disposed of.
200 [EditorBrowsable(EditorBrowsableState.Never)]
201 protected internal bool Disposed => disposed;
204 /// The flag to check if it is disposed by DisposeQueue.
206 [EditorBrowsable(EditorBrowsableState.Never)]
207 protected internal bool IsDisposeQueued => isDisposeQueued;
210 internal static class DisposableExtension
212 internal static T Ensure<T>(this T caller) where T : Disposable
214 return (caller.SwigCPtr.Handle == System.IntPtr.Zero ? null : caller);