|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 using System; |
|
18 |
|
19 namespace SymbianUtils |
|
20 { |
|
21 public class DisposableObject : IDisposable |
|
22 { |
|
23 #region Constructors |
|
24 public DisposableObject() |
|
25 { |
|
26 } |
|
27 |
|
28 ~DisposableObject() |
|
29 { |
|
30 // Not allowed to access managed resources from |
|
31 // within a C# destructor (in this context we are being |
|
32 // called by the GC and it will take care of disposing of |
|
33 // other managed resources for us). |
|
34 Cleanup( false, true ); |
|
35 } |
|
36 #endregion |
|
37 |
|
38 #region Properties |
|
39 public bool HaveBeenDisposedOf |
|
40 { |
|
41 get { return iHaveBeenDisposedOf; } |
|
42 } |
|
43 #endregion |
|
44 |
|
45 #region API - Cleanup Framework |
|
46 protected virtual void CleanupManagedResources() |
|
47 { |
|
48 } |
|
49 |
|
50 protected virtual void CleanupUnmanagedResources() |
|
51 { |
|
52 } |
|
53 #endregion |
|
54 |
|
55 #region From IDisposable |
|
56 public void Dispose() |
|
57 { |
|
58 // In this situation, we are programatically being asked to |
|
59 // release all resources, including managed ones. |
|
60 Cleanup( true, true ); |
|
61 |
|
62 // Take yourself off the Finalization queue |
|
63 // to prevent finalization code for this object |
|
64 // from executing a second time. |
|
65 GC.SuppressFinalize( this ); |
|
66 } |
|
67 #endregion |
|
68 |
|
69 #region Internal methods |
|
70 private void Cleanup( bool aReleaseManagedResources, bool aReleaseUnmanagedResources ) |
|
71 { |
|
72 lock( this ) |
|
73 { |
|
74 if ( iHaveBeenDisposedOf == false ) |
|
75 { |
|
76 try |
|
77 { |
|
78 if ( aReleaseManagedResources ) |
|
79 { |
|
80 CleanupManagedResources(); |
|
81 } |
|
82 if ( aReleaseUnmanagedResources ) |
|
83 { |
|
84 CleanupUnmanagedResources(); |
|
85 } |
|
86 |
|
87 iHaveBeenDisposedOf = true; |
|
88 } |
|
89 catch |
|
90 { |
|
91 } |
|
92 } |
|
93 } |
|
94 } |
|
95 #endregion |
|
96 |
|
97 #region Data members |
|
98 private bool iHaveBeenDisposedOf = false; |
|
99 #endregion |
|
100 } |
|
101 } |