annotate Implab/Disposable.cs @ 68:9dd6a896a385

ServiceLocator: small refactoring, GetService method is made virtual
author cin
date Thu, 28 Aug 2014 02:28:00 +0400
parents d9d794b61bb9
children 673947ce458a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
48
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
1 using Implab.Diagnostics;
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
2 using System;
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
3 using System.Collections.Generic;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
4 using System.Diagnostics;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
5 using System.Linq;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
6 using System.Web;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
7
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
8 namespace Implab {
48
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
9 /// <summary>
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
10 /// Объект, поддерживающий освобождение ресурсов.
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
11 /// </summary>
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
12 public class Disposable : IDisposable {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
13
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
14 bool m_disposed;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
15
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
16 public event EventHandler Disposed;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
17
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
18 public bool IsDisposed {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
19 get { return m_disposed; }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
20 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
21
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
22 protected void AssertNotDisposed() {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
23 if (m_disposed)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
24 throw new ObjectDisposedException(this.ToString());
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
25 }
48
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
26 /// <summary>
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
27 /// Переводит объект в состояние <c>Disposed</c> и вызывает событие <see cref="Disposed"/>
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
28 /// </summary>
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
29 /// <param name="disposing">Признак того, что нужно освободить ресурсы, иначе данный метод
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
30 /// вызван сборщиком мусора и нужно освобождать ТОЛЬКО неуправляемые ресурсы ТОЛЬКО этого
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
31 /// объекта.</param>
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
32 /// <remarks>
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
33 /// Данный метод осуществляет проверку того, что объект уже был освобожден, чтобы не вызывать
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
34 /// событие <see cref="Disposed"/>. Не поддерживает многопоточность.
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
35 /// </remarks>
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
36 protected virtual void Dispose(bool disposing) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
37 if (disposing && !m_disposed) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
38 m_disposed = true;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
39
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
40 EventHandler temp = Disposed;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
41 if (temp != null)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
42 temp(this,EventArgs.Empty);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
43 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
44 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
45 public void Dispose() {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
46 Dispose(true);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
47 GC.SuppressFinalize(this);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
48 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
49
48
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
50 /// <summary>
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
51 /// Записывает сообщение об утечке объекта.
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
52 /// </summary>
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
53 protected virtual void ReportObjectLeaks() {
48
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
54 TraceLog.TraceWarning("The object is marked as disposable but isn't disposed properly: {0}", this);
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
55 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
56
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
57 ~Disposable() {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
58 Dispose(false);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
59 ReportObjectLeaks();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
60 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
61 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
62 }