@@ -0,0 +1,20 | |||||
|
1 | using System; | |||
|
2 | using System.Collections.Generic; | |||
|
3 | ||||
|
4 | namespace Implab.Components { | |||
|
5 | /// <summary> | |||
|
6 | /// Global application components and services. | |||
|
7 | /// </summary> | |||
|
8 | public static class App { | |||
|
9 | readonly static ComponentContainer<object> _root = new ComponentContainer<object>(); | |||
|
10 | ||||
|
11 | public static ICollection<object> RootContainer { | |||
|
12 | get { return _root; } | |||
|
13 | } | |||
|
14 | ||||
|
15 | static App() { | |||
|
16 | AppDomain.CurrentDomain.ProcessExit += (sender, e) => _root.Dispose(); | |||
|
17 | } | |||
|
18 | } | |||
|
19 | } | |||
|
20 |
@@ -0,0 +1,84 | |||||
|
1 | using System; | |||
|
2 | using System.Collections.Generic; | |||
|
3 | using System.Linq; | |||
|
4 | ||||
|
5 | namespace Implab.Components { | |||
|
6 | /// <summary> | |||
|
7 | /// Component container. | |||
|
8 | /// </summary> | |||
|
9 | /// <remarks>Instanses of this class are thread safe.</remarks> | |||
|
10 | public class ComponentContainer<T> : Disposable, ICollection<T> { | |||
|
11 | readonly HashSet<T> m_components = new HashSet<T>(); | |||
|
12 | ||||
|
13 | public void Clear() { | |||
|
14 | T[] removed; | |||
|
15 | ||||
|
16 | lock (m_components) { | |||
|
17 | removed = new T[m_components.Count]; | |||
|
18 | m_components.CopyTo(removed); | |||
|
19 | m_components.Clear(); | |||
|
20 | } | |||
|
21 | ||||
|
22 | foreach (var item in removed.OfType<IDisposable>()) | |||
|
23 | item.Dispose(); | |||
|
24 | } | |||
|
25 | ||||
|
26 | public bool Contains(T item) { | |||
|
27 | lock (m_components) | |||
|
28 | return m_components.Contains(item); | |||
|
29 | } | |||
|
30 | ||||
|
31 | public void CopyTo(T[] array, int arrayIndex) { | |||
|
32 | lock (m_components) | |||
|
33 | m_components.CopyTo(array, arrayIndex); | |||
|
34 | } | |||
|
35 | ||||
|
36 | public bool Remove(T item) { | |||
|
37 | lock (m_components) | |||
|
38 | return m_components.Remove(item); | |||
|
39 | } | |||
|
40 | ||||
|
41 | public int Count { | |||
|
42 | get { | |||
|
43 | lock (m_components) | |||
|
44 | return m_components.Count; | |||
|
45 | } | |||
|
46 | } | |||
|
47 | ||||
|
48 | public bool IsReadOnly { | |||
|
49 | get { | |||
|
50 | return false; | |||
|
51 | } | |||
|
52 | } | |||
|
53 | ||||
|
54 | public IEnumerator<T> GetEnumerator() { | |||
|
55 | T[] items; | |||
|
56 | lock (m_components) { | |||
|
57 | items = new T[m_components.Count]; | |||
|
58 | m_components.CopyTo(items); | |||
|
59 | return (IEnumerator<T>)items.GetEnumerator(); | |||
|
60 | } | |||
|
61 | } | |||
|
62 | ||||
|
63 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { | |||
|
64 | return GetEnumerator(); | |||
|
65 | } | |||
|
66 | ||||
|
67 | public void Add(T item) { | |||
|
68 | Safe.ArgumentNotNull(item, "item"); | |||
|
69 | ||||
|
70 | lock (m_components) { | |||
|
71 | if (IsDisposed) | |||
|
72 | Safe.Dispose(item); | |||
|
73 | else | |||
|
74 | m_components.Add(item); | |||
|
75 | } | |||
|
76 | } | |||
|
77 | ||||
|
78 | protected override void Dispose(bool disposing) { | |||
|
79 | base.Dispose(disposing); | |||
|
80 | Clear(); | |||
|
81 | } | |||
|
82 | } | |||
|
83 | } | |||
|
84 |
@@ -0,0 +1,103 | |||||
|
1 | using Implab.Diagnostics; | |||
|
2 | using System; | |||
|
3 | using System.Threading; | |||
|
4 | ||||
|
5 | namespace Implab.Components { | |||
|
6 | /// <summary> | |||
|
7 | /// Base class the objects which support disposing. | |||
|
8 | /// </summary> | |||
|
9 | public class Disposable : IDisposable { | |||
|
10 | ||||
|
11 | int m_disposed; | |||
|
12 | ||||
|
13 | public event EventHandler Disposed; | |||
|
14 | ||||
|
15 | public bool IsDisposed { | |||
|
16 | get { | |||
|
17 | Thread.MemoryBarrier(); | |||
|
18 | return m_disposed != 0; | |||
|
19 | } | |||
|
20 | } | |||
|
21 | ||||
|
22 | /// <summary> | |||
|
23 | /// Asserts the object is not disposed. | |||
|
24 | /// </summary> | |||
|
25 | /// <exception cref="ObjectDisposedException">The object is disposed</exception> | |||
|
26 | /// <remarks> | |||
|
27 | /// Успешная проверка того, что объект не освобожден еще не гарантирует, что он не | |||
|
28 | /// будет освобожден сразу после нее, поэтому методы использующие проверку должны | |||
|
29 | /// учитывать, что объект может быть освобожден из параллельного потока. | |||
|
30 | /// Данны метод служит для упрощения отладки ошибок при использовании объекта после его | |||
|
31 | /// освобождения. | |||
|
32 | /// </remarks> | |||
|
33 | /// <example> | |||
|
34 | /// // пример синхронизированного освобождения ресурсов | |||
|
35 | /// class FileStore : Disposable { | |||
|
36 | /// readonly TextWriter m_file; | |||
|
37 | /// readonly obejct m_sync = new object(); | |||
|
38 | /// | |||
|
39 | /// public FileStore(string file) { | |||
|
40 | /// m_file = new TextWriter(File.OpenWrite(file)); | |||
|
41 | /// } | |||
|
42 | /// | |||
|
43 | /// public void Write(string text) { | |||
|
44 | /// lock(m_sync) { | |||
|
45 | /// AssertNotDisposed(); | |||
|
46 | /// m_file.Write(text); | |||
|
47 | /// } | |||
|
48 | /// } | |||
|
49 | /// | |||
|
50 | /// protected override void Dispose(bool disposing) { | |||
|
51 | /// if (disposing) | |||
|
52 | /// lock(m_sync) { | |||
|
53 | /// m_file.Dipose(); | |||
|
54 | /// base.Dispose(true); | |||
|
55 | /// } | |||
|
56 | /// else | |||
|
57 | /// base.Dispose(false); | |||
|
58 | /// } | |||
|
59 | /// } | |||
|
60 | /// <example> | |||
|
61 | protected void AssertNotDisposed() { | |||
|
62 | Thread.MemoryBarrier(); | |||
|
63 | if (m_disposed != 0) | |||
|
64 | throw new ObjectDisposedException(ToString()); | |||
|
65 | } | |||
|
66 | /// <summary> | |||
|
67 | /// Вызывает событие <see cref="Disposed"/> | |||
|
68 | /// </summary> | |||
|
69 | /// <param name="disposing">Признак того, что нужно освободить ресурсы, иначе данный метод | |||
|
70 | /// вызван сборщиком мусора и нужно освобождать ТОЛЬКО неуправляемые ресурсы ТОЛЬКО этого | |||
|
71 | /// объекта.</param> | |||
|
72 | /// <remarks> | |||
|
73 | /// Данный метод вызывается гарантированно один раз даже при одновременном вызове <see cref="Dispose()"/> | |||
|
74 | /// из нескольких потоков. | |||
|
75 | /// </remarks> | |||
|
76 | protected virtual void Dispose(bool disposing) { | |||
|
77 | if (disposing) { | |||
|
78 | EventHandler temp = Disposed; | |||
|
79 | if (temp != null) | |||
|
80 | temp(this, EventArgs.Empty); | |||
|
81 | } | |||
|
82 | } | |||
|
83 | ||||
|
84 | public void Dispose() { | |||
|
85 | if (Interlocked.Increment(ref m_disposed) == 1) { | |||
|
86 | Dispose(true); | |||
|
87 | GC.SuppressFinalize(this); | |||
|
88 | } | |||
|
89 | } | |||
|
90 | ||||
|
91 | /// <summary> | |||
|
92 | /// Записывает сообщение об утечке объекта. | |||
|
93 | /// </summary> | |||
|
94 | protected virtual void ReportObjectLeaks() { | |||
|
95 | TraceLog.TraceWarning("The object is marked as disposable but isn't disposed properly: {0}", this); | |||
|
96 | } | |||
|
97 | ||||
|
98 | ~Disposable() { | |||
|
99 | Dispose(false); | |||
|
100 | ReportObjectLeaks(); | |||
|
101 | } | |||
|
102 | } | |||
|
103 | } No newline at end of file |
@@ -0,0 +1,90 | |||||
|
1 | using System; | |||
|
2 | using Implab.Parallels; | |||
|
3 | using System.Threading; | |||
|
4 | using System.Diagnostics; | |||
|
5 | using System.Diagnostics.CodeAnalysis; | |||
|
6 | ||||
|
7 | namespace Implab { | |||
|
8 | public abstract class DisposablePool<T> : IDisposable { | |||
|
9 | readonly int m_size; | |||
|
10 | readonly AsyncQueue<T> m_queue = new AsyncQueue<T>(); | |||
|
11 | ||||
|
12 | [SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")] | |||
|
13 | static readonly bool _isValueType = typeof(T).IsValueType; | |||
|
14 | ||||
|
15 | bool m_disposed; | |||
|
16 | ||||
|
17 | int m_count; | |||
|
18 | ||||
|
19 | protected DisposablePool(int size) { | |||
|
20 | m_size = size; | |||
|
21 | } | |||
|
22 | ||||
|
23 | protected DisposablePool() : this(Environment.ProcessorCount+1) { | |||
|
24 | } | |||
|
25 | ||||
|
26 | public T Allocate() { | |||
|
27 | if (m_disposed) | |||
|
28 | throw new ObjectDisposedException(ToString()); | |||
|
29 | ||||
|
30 | T instance; | |||
|
31 | if (m_queue.TryDequeue(out instance)) { | |||
|
32 | Interlocked.Decrement(ref m_count); | |||
|
33 | } else { | |||
|
34 | instance = CreateInstance(); | |||
|
35 | Debug.Assert(!Object.Equals(instance, default(T)) || _isValueType); | |||
|
36 | } | |||
|
37 | return instance; | |||
|
38 | } | |||
|
39 | ||||
|
40 | protected abstract T CreateInstance(); | |||
|
41 | ||||
|
42 | protected virtual void CleanupInstance(T instance) { | |||
|
43 | } | |||
|
44 | ||||
|
45 | public void Release(T instance) { | |||
|
46 | if ( Object.Equals(instance,default(T)) && !_isValueType) | |||
|
47 | return; | |||
|
48 | ||||
|
49 | Thread.MemoryBarrier(); | |||
|
50 | if (m_count < m_size && !m_disposed) { | |||
|
51 | Interlocked.Increment(ref m_count); | |||
|
52 | ||||
|
53 | CleanupInstance(instance); | |||
|
54 | ||||
|
55 | m_queue.Enqueue(instance); | |||
|
56 | ||||
|
57 | // пока элемент возвращался в кеш, была начата операция освобождения всего кеша | |||
|
58 | // и возможно уже законцена, в таком случае следует извлечь элемент обратно и | |||
|
59 | // освободить его. Если операция освобождения кеша еще не заврешилась, то будет | |||
|
60 | // изъят и освобожден произвольный элемен, что не повлияет на ход всего процесса. | |||
|
61 | if (m_disposed && m_queue.TryDequeue(out instance) && instance is IDisposable) | |||
|
62 | ((IDisposable)instance).Dispose() ; | |||
|
63 | ||||
|
64 | } else { | |||
|
65 | if (instance is IDisposable) | |||
|
66 | ((IDisposable)instance).Dispose(); | |||
|
67 | } | |||
|
68 | } | |||
|
69 | ||||
|
70 | protected virtual void Dispose(bool disposing) { | |||
|
71 | if (disposing) { | |||
|
72 | m_disposed = true; | |||
|
73 | T instance; | |||
|
74 | while (m_queue.TryDequeue(out instance)) | |||
|
75 | if (instance is IDisposable) | |||
|
76 | ((IDisposable)instance).Dispose(); | |||
|
77 | } | |||
|
78 | } | |||
|
79 | ||||
|
80 | #region IDisposable implementation | |||
|
81 | ||||
|
82 | public void Dispose() { | |||
|
83 | Dispose(true); | |||
|
84 | GC.SuppressFinalize(this); | |||
|
85 | } | |||
|
86 | ||||
|
87 | #endregion | |||
|
88 | } | |||
|
89 | } | |||
|
90 |
@@ -0,0 +1,12 | |||||
|
1 | namespace Implab.Components { | |||
|
2 | public enum ExecutionState { | |||
|
3 | Uninitialized, | |||
|
4 | Initial, | |||
|
5 | Starting, | |||
|
6 | Running, | |||
|
7 | Stopping, | |||
|
8 | Stopped, | |||
|
9 | Disposed, | |||
|
10 | Failed | |||
|
11 | } | |||
|
12 | } No newline at end of file |
@@ -0,0 +1,21 | |||||
|
1 | using System; | |||
|
2 | ||||
|
3 | namespace Implab.Components { | |||
|
4 | /// <summary> | |||
|
5 | /// Initializable components are created and initialized in two steps, first we have create the component, | |||
|
6 | /// then we have to complete it's creation by calling an <see cref="Init()"/> method. All parameters needed | |||
|
7 | /// to complete the initialization must be passed before the calling <see cref="Init()"/> | |||
|
8 | /// </summary> | |||
|
9 | public interface IInitializable { | |||
|
10 | /// <summary> | |||
|
11 | /// Completes initialization. | |||
|
12 | /// </summary> | |||
|
13 | /// <remarks> | |||
|
14 | /// Normally virtual shouldn't be called from the constructor, due to the incomplete object state, but | |||
|
15 | /// they can be called from this method. This method is also usefull when we constructing a complex grpah | |||
|
16 | /// of components where cyclic references may take place. | |||
|
17 | /// </remarks> | |||
|
18 | void Init(); | |||
|
19 | } | |||
|
20 | } | |||
|
21 |
@@ -0,0 +1,14 | |||||
|
1 | using System; | |||
|
2 | ||||
|
3 | namespace Implab.Components { | |||
|
4 | public interface IRunnable { | |||
|
5 | IPromise Start(); | |||
|
6 | ||||
|
7 | IPromise Stop(); | |||
|
8 | ||||
|
9 | ExecutionState State { get; } | |||
|
10 | ||||
|
11 | Exception LastError { get; } | |||
|
12 | } | |||
|
13 | } | |||
|
14 |
@@ -0,0 +1,60 | |||||
|
1 | using Implab.Parallels; | |||
|
2 | using System; | |||
|
3 | using System.Threading; | |||
|
4 | ||||
|
5 | namespace Implab.Components { | |||
|
6 | /// <summary> | |||
|
7 | /// Базовый класс для создания пулов объектов. | |||
|
8 | /// </summary> | |||
|
9 | /// <remarks> | |||
|
10 | /// <para>Пул объектов позволяет многократно использовать один и тотже объект, | |||
|
11 | /// что актуально для объектов, создание которых требует существенных ресурсов. | |||
|
12 | /// Пул объектов использует слабые ссылки, чтобы не препятствовать освобождению | |||
|
13 | /// ресурсов и создает новые объекты при необходимости.</para> | |||
|
14 | /// <para> | |||
|
15 | /// Наследники должны реализовывать метод <see cref="CreateInstance()"/> для создания | |||
|
16 | /// новых экземпляров. | |||
|
17 | /// </para> | |||
|
18 | /// <para>Пул поддерживает обращения сразу из нескольких потоков.</para> | |||
|
19 | /// </remarks> | |||
|
20 | public abstract class ObjectPool<T> where T : class { | |||
|
21 | readonly AsyncQueue<WeakReference> m_queue = new AsyncQueue<WeakReference>(); | |||
|
22 | readonly int m_size; | |||
|
23 | int m_count = 0; | |||
|
24 | ||||
|
25 | protected ObjectPool() : this(Environment.ProcessorCount+1) { | |||
|
26 | ||||
|
27 | } | |||
|
28 | ||||
|
29 | protected ObjectPool(int size) { | |||
|
30 | Safe.ArgumentInRange(size,1,size,"size"); | |||
|
31 | ||||
|
32 | m_size = size; | |||
|
33 | } | |||
|
34 | ||||
|
35 | protected abstract T CreateInstance(); | |||
|
36 | ||||
|
37 | protected virtual void CleanupInstance(T instance) { | |||
|
38 | } | |||
|
39 | ||||
|
40 | public T Allocate() { | |||
|
41 | WeakReference reference; | |||
|
42 | while (m_queue.TryDequeue(out reference)) { | |||
|
43 | Interlocked.Decrement(ref m_count); | |||
|
44 | object instance = reference.Target; | |||
|
45 | if (instance == null) | |||
|
46 | continue; | |||
|
47 | return (T)instance; | |||
|
48 | } | |||
|
49 | return CreateInstance(); | |||
|
50 | } | |||
|
51 | ||||
|
52 | public void Release(T instance) { | |||
|
53 | if (m_count < m_size && instance != null) { | |||
|
54 | Interlocked.Increment(ref m_count); | |||
|
55 | CleanupInstance(instance); | |||
|
56 | m_queue.Enqueue(new WeakReference(instance)); | |||
|
57 | } | |||
|
58 | } | |||
|
59 | } | |||
|
60 | } |
@@ -0,0 +1,248 | |||||
|
1 | using System; | |||
|
2 | using System.Collections.Generic; | |||
|
3 | ||||
|
4 | namespace Implab.Components { | |||
|
5 | /// <summary> | |||
|
6 | /// Коллекция сервисов, позволяет регистрировать и получать сервисы. | |||
|
7 | /// </summary> | |||
|
8 | public class ServiceLocator: Disposable, IServiceLocator, IServiceProvider { | |||
|
9 | // запись о сервисе | |||
|
10 | struct ServiceEntry : IDisposable { | |||
|
11 | public object service; // сервис | |||
|
12 | public bool shared; // признак того, что сервис НЕ нужно освобождать | |||
|
13 | public Func<object> activator; // активатор сервиса при первом обращении | |||
|
14 | public Action<object> cleanup; // функция для очистки сервиса | |||
|
15 | public List<Type> associated; // ссылки на текущую запись | |||
|
16 | public Type origin; // ссылка на оригинальную запись о сервисе | |||
|
17 | ||||
|
18 | #region IDisposable implementation | |||
|
19 | ||||
|
20 | public void Dispose() { | |||
|
21 | if (shared) | |||
|
22 | return; | |||
|
23 | if (cleanup != null) { | |||
|
24 | if (service != null) | |||
|
25 | cleanup(service); | |||
|
26 | } else | |||
|
27 | Safe.Dispose(service); | |||
|
28 | } | |||
|
29 | ||||
|
30 | #endregion | |||
|
31 | } | |||
|
32 | ||||
|
33 | // словарь существующих сервисов | |||
|
34 | readonly Dictionary<Type, ServiceEntry> m_services = new Dictionary<Type,ServiceEntry>(); | |||
|
35 | ||||
|
36 | /// <summary> | |||
|
37 | /// Получает объект предоставляющий сервис <typeparamref name="T"/>. | |||
|
38 | /// </summary> | |||
|
39 | /// <typeparam name="T">Тип запрашиваемого сервиса</typeparam> | |||
|
40 | /// <returns>Объект, реализующий сервис</returns> | |||
|
41 | /// <exception cref="KeyNotFoundException">Сервис не зарегистрирован</exception> | |||
|
42 | public T GetService<T>() { | |||
|
43 | object result; | |||
|
44 | if (TryGetService(typeof(T), out result)) | |||
|
45 | return (T)result; | |||
|
46 | throw new ApplicationException (String.Format ("{0} doesn't provide {1} service", this, typeof(T))); | |||
|
47 | } | |||
|
48 | ||||
|
49 | ||||
|
50 | /// <summary> | |||
|
51 | /// Пытается получить указанный сервис, в случае, если компонента не предоставляет требуемый сервис | |||
|
52 | /// не возникает исключений. | |||
|
53 | /// </summary> | |||
|
54 | /// <typeparam name="T">Тип требуемого сервиса.</typeparam> | |||
|
55 | /// <param name="service">Объект реализующий сервис, или <c>default(T)</c> если такового нет.</param> | |||
|
56 | /// <returns><c>true</c> - сервис найден, <c>false</c> - сервис не зарегистрирован.</returns> | |||
|
57 | public bool TryGetService<T>(out T service) { | |||
|
58 | object result; | |||
|
59 | if (TryGetService(typeof(T), out result)) { | |||
|
60 | service = (T)result; | |||
|
61 | return true; | |||
|
62 | } | |||
|
63 | service = default(T); | |||
|
64 | return false; | |||
|
65 | } | |||
|
66 | ||||
|
67 | /// <summary> | |||
|
68 | /// Получает объект предоставляющий сервис <paramref name="serviceType"/> | |||
|
69 | /// </summary> | |||
|
70 | /// <param name="serviceType">Тип запрашиваемого сервиса</param> | |||
|
71 | /// <returns>Объект, реализующий сервис</returns> | |||
|
72 | /// <exception cref="KeyNotFoundException">Сервис не зарегистрирован</exception> | |||
|
73 | public object GetService(Type serviceType) { | |||
|
74 | object result; | |||
|
75 | if (TryGetService(serviceType, out result)) | |||
|
76 | return result; | |||
|
77 | throw new ApplicationException (String.Format ("{0} doesn't provide {1} service", this, serviceType)); | |||
|
78 | } | |||
|
79 | ||||
|
80 | /// <summary> | |||
|
81 | /// Пытается получить требуемый сервис или совместимый с ним. | |||
|
82 | /// </summary> | |||
|
83 | /// <returns><c>true</c>, если сервис был найден, <c>false</c> в противном случае..</returns> | |||
|
84 | /// <param name="serviceType">Тип запрашиваемого сервиса.</param> | |||
|
85 | /// <param name="service">Искомый сервис.</param> | |||
|
86 | public virtual bool TryGetService(Type serviceType, out object service) { | |||
|
87 | Safe.ArgumentNotNull(serviceType, "serviceType"); | |||
|
88 | AssertNotDisposed(); | |||
|
89 | ||||
|
90 | ServiceEntry se; | |||
|
91 | if (!m_services.TryGetValue(serviceType, out se)) { | |||
|
92 | // ищем ближайщий объект, реализующий нужный сервис | |||
|
93 | Type pt = null; | |||
|
94 | foreach (var t in m_services.Keys) | |||
|
95 | if (serviceType.IsAssignableFrom(t) && (pt == null || t.IsAssignableFrom(pt))) | |||
|
96 | pt = t; | |||
|
97 | ||||
|
98 | if (pt == null) { | |||
|
99 | // нет нужного сервиса | |||
|
100 | service = null; | |||
|
101 | return false; | |||
|
102 | } | |||
|
103 | ||||
|
104 | var pe = m_services[pt]; | |||
|
105 | ||||
|
106 | // найденная запись может ссылаться на оригинальную запись с сервисом | |||
|
107 | if(pe.origin != null) { | |||
|
108 | pt = pe.origin; | |||
|
109 | pe = m_services[pt]; | |||
|
110 | } | |||
|
111 | ||||
|
112 | // добавляем список с обратными ссылками | |||
|
113 | if (pe.associated == null) | |||
|
114 | pe.associated = new List<Type>(); | |||
|
115 | ||||
|
116 | pe.associated.Add(serviceType); | |||
|
117 | ||||
|
118 | // обновляем родительскую запись | |||
|
119 | m_services[pt] = pe; | |||
|
120 | ||||
|
121 | // создаем запись со ссылкой | |||
|
122 | se = new ServiceEntry { | |||
|
123 | service = pe.service, | |||
|
124 | origin = pt, | |||
|
125 | shared = true // предотвращаем множественные попытки освобождения | |||
|
126 | }; | |||
|
127 | ||||
|
128 | m_services[serviceType] = se; | |||
|
129 | } | |||
|
130 | ||||
|
131 | // запись содержит в себе информацию о сервисе | |||
|
132 | if (se.service != null) { | |||
|
133 | service = se.service; | |||
|
134 | return true; | |||
|
135 | } | |||
|
136 | ||||
|
137 | // текущая запись является ссылкой | |||
|
138 | if (se.origin != null) { | |||
|
139 | se.service = GetService(se.origin); | |||
|
140 | m_services[serviceType] = se; | |||
|
141 | service = se.service; | |||
|
142 | return true; | |||
|
143 | } | |||
|
144 | ||||
|
145 | // текущая запись не является ссылкой и не имеет информации о сервисе | |||
|
146 | // она должна сожержать информацию об активации | |||
|
147 | if (se.activator != null) { | |||
|
148 | se.service = se.activator(); | |||
|
149 | ||||
|
150 | m_services[serviceType] = se; | |||
|
151 | ||||
|
152 | service = se.service; | |||
|
153 | return true; | |||
|
154 | } | |||
|
155 | ||||
|
156 | service = null; | |||
|
157 | return false; | |||
|
158 | } | |||
|
159 | ||||
|
160 | /// <summary> | |||
|
161 | /// Регистрирует фабрику для активации сервиса по первому требованию. | |||
|
162 | /// </summary> | |||
|
163 | /// <typeparam name="T">Тип регистрируемого сервиса.</typeparam> | |||
|
164 | /// <param name="activator">Фабрика для создания/получения объекта, предоставляющего сервис.</param> | |||
|
165 | /// <param name = "cleanup">Метод для освобождения экземпляра сервиса, будет вызыван при освобождении сервис-локатора.</param> | |||
|
166 | /// <remarks>При освобождении сервис-локатора, сервисы полученные в результате активации также будут освобождены.</remarks> | |||
|
167 | public void Register<T>(Func<T> activator, Action<T> cleanup) { | |||
|
168 | Safe.ArgumentNotNull(activator, "activator"); | |||
|
169 | ||||
|
170 | AssertNotDisposed(); | |||
|
171 | ||||
|
172 | Unregister(typeof(T)); | |||
|
173 | ||||
|
174 | var serviceEntry = new ServiceEntry(); | |||
|
175 | serviceEntry.activator = () => activator(); | |||
|
176 | if (cleanup != null) | |||
|
177 | serviceEntry.cleanup = instance => cleanup((T)instance); | |||
|
178 | m_services[typeof(T)] = serviceEntry; | |||
|
179 | } | |||
|
180 | ||||
|
181 | public void Register<T>(Func<T> activator) { | |||
|
182 | Register(activator, null); | |||
|
183 | } | |||
|
184 | ||||
|
185 | /// <summary> | |||
|
186 | /// Регистрирует объект, предоставляющий сервис. | |||
|
187 | /// </summary> | |||
|
188 | /// <typeparam name="T">Тип регистрируемого сервиса.</typeparam> | |||
|
189 | /// <param name="service">Объект, предоставляющий сервис.</param> | |||
|
190 | /// <exception cref="InvalidOperationException">Указанный сервис уже зарегистрирован.</exception> | |||
|
191 | /// <remarks>Сервис-локатором не управляет временем жизни объекта для зарегистрированного сервиса.</remarks> | |||
|
192 | public void Register<T>(T service) { | |||
|
193 | Register(service, true); | |||
|
194 | } | |||
|
195 | ||||
|
196 | /// <summary> | |||
|
197 | /// Регистрирует объект, предоставляющий сервис. Повторная регистрация отменяет уже существующую. | |||
|
198 | /// </summary> | |||
|
199 | /// <typeparam name="T">Тип регистрируемого сервиса.</typeparam> | |||
|
200 | /// <param name="service">Объект, предоставляющий сервис.</param> | |||
|
201 | /// <param name="shared">Признак того, что объект является разделяемым и сервис-локатор не должен его освобождать.</param> | |||
|
202 | public void Register<T>(T service, bool shared) { | |||
|
203 | Safe.ArgumentNotNull(service, "service"); | |||
|
204 | ||||
|
205 | AssertNotDisposed(); | |||
|
206 | ||||
|
207 | Unregister(typeof(T)); | |||
|
208 | ||||
|
209 | m_services[typeof(T)] = new ServiceEntry { service = service, shared = shared }; | |||
|
210 | } | |||
|
211 | ||||
|
212 | public void Unregister(Type serviceType) { | |||
|
213 | Safe.ArgumentNotNull(serviceType, "serviceType"); | |||
|
214 | ||||
|
215 | AssertNotDisposed(); | |||
|
216 | ||||
|
217 | ServiceEntry se; | |||
|
218 | if (m_services.TryGetValue(serviceType, out se)) { | |||
|
219 | if (se.origin != null) { | |||
|
220 | var pe = m_services[se.origin]; | |||
|
221 | pe.associated.Remove(serviceType); | |||
|
222 | } | |||
|
223 | // освобождаем ресурсы | |||
|
224 | se.Dispose(); | |||
|
225 | m_services.Remove(serviceType); | |||
|
226 | ||||
|
227 | // убираем связанные записи | |||
|
228 | if (se.associated != null) | |||
|
229 | foreach (var item in se.associated) | |||
|
230 | m_services.Remove(item); | |||
|
231 | } | |||
|
232 | } | |||
|
233 | ||||
|
234 | /// <summary> | |||
|
235 | /// Освобождает зарегистрированные сервисы (которые требуется освобоить). | |||
|
236 | /// </summary> | |||
|
237 | /// <param name="disposing">Призанак того, что нужно освободить ресурсы.</param> | |||
|
238 | protected override void Dispose(bool disposing) { | |||
|
239 | if (disposing) { | |||
|
240 | ||||
|
241 | foreach (var entry in m_services.Values) | |||
|
242 | entry.Dispose(); | |||
|
243 | ||||
|
244 | } | |||
|
245 | base.Dispose(disposing); | |||
|
246 | } | |||
|
247 | } | |||
|
248 | } No newline at end of file |
@@ -1,7 +1,6 | |||||
1 | using System; |
|
1 | using System; | |
2 | using System.Collections.Generic; |
|
2 | using System.Collections.Generic; | |
3 | using System.Linq; |
|
3 | using Implab.Components; | |
4 | using System.Text; |
|
|||
5 |
|
4 | |||
6 | namespace Implab.Diagnostics { |
|
5 | namespace Implab.Diagnostics { | |
7 | public abstract class ListenerBase : ServiceLocator, ILogWriter<object>, ILogWriter<TraceEvent> { |
|
6 | public abstract class ListenerBase : ServiceLocator, ILogWriter<object>, ILogWriter<TraceEvent> { |
@@ -75,7 +75,6 | |||||
75 | <Reference Include="mscorlib" /> |
|
75 | <Reference Include="mscorlib" /> | |
76 | </ItemGroup> |
|
76 | </ItemGroup> | |
77 | <ItemGroup> |
|
77 | <ItemGroup> | |
78 | <Compile Include="Component.cs" /> |
|
|||
79 | <Compile Include="CustomEqualityComparer.cs" /> |
|
78 | <Compile Include="CustomEqualityComparer.cs" /> | |
80 | <Compile Include="Diagnostics\ConsoleTraceListener.cs" /> |
|
79 | <Compile Include="Diagnostics\ConsoleTraceListener.cs" /> | |
81 | <Compile Include="Diagnostics\EventText.cs" /> |
|
80 | <Compile Include="Diagnostics\EventText.cs" /> | |
@@ -85,7 +84,6 | |||||
85 | <Compile Include="Diagnostics\TraceLog.cs" /> |
|
84 | <Compile Include="Diagnostics\TraceLog.cs" /> | |
86 | <Compile Include="Diagnostics\TraceEvent.cs" /> |
|
85 | <Compile Include="Diagnostics\TraceEvent.cs" /> | |
87 | <Compile Include="Diagnostics\TraceEventType.cs" /> |
|
86 | <Compile Include="Diagnostics\TraceEventType.cs" /> | |
88 | <Compile Include="Disposable.cs" /> |
|
|||
89 | <Compile Include="ICancellable.cs" /> |
|
87 | <Compile Include="ICancellable.cs" /> | |
90 | <Compile Include="IProgressHandler.cs" /> |
|
88 | <Compile Include="IProgressHandler.cs" /> | |
91 | <Compile Include="IProgressNotifier.cs" /> |
|
89 | <Compile Include="IProgressNotifier.cs" /> | |
@@ -130,8 +128,6 | |||||
130 | <Compile Include="Parsing\StarToken.cs" /> |
|
128 | <Compile Include="Parsing\StarToken.cs" /> | |
131 | <Compile Include="Parsing\SymbolToken.cs" /> |
|
129 | <Compile Include="Parsing\SymbolToken.cs" /> | |
132 | <Compile Include="Parsing\Token.cs" /> |
|
130 | <Compile Include="Parsing\Token.cs" /> | |
133 | <Compile Include="ServiceLocator.cs" /> |
|
|||
134 | <Compile Include="TaskController.cs" /> |
|
|||
135 | <Compile Include="ProgressInitEventArgs.cs" /> |
|
131 | <Compile Include="ProgressInitEventArgs.cs" /> | |
136 | <Compile Include="Properties\AssemblyInfo.cs" /> |
|
132 | <Compile Include="Properties\AssemblyInfo.cs" /> | |
137 | <Compile Include="Parallels\AsyncPool.cs" /> |
|
133 | <Compile Include="Parallels\AsyncPool.cs" /> | |
@@ -144,11 +140,7 | |||||
144 | <Compile Include="Diagnostics\LogEventArgs.cs" /> |
|
140 | <Compile Include="Diagnostics\LogEventArgs.cs" /> | |
145 | <Compile Include="Diagnostics\LogEventArgsT.cs" /> |
|
141 | <Compile Include="Diagnostics\LogEventArgsT.cs" /> | |
146 | <Compile Include="Diagnostics\Extensions.cs" /> |
|
142 | <Compile Include="Diagnostics\Extensions.cs" /> | |
147 | <Compile Include="IComponentContainer.cs" /> |
|
|||
148 | <Compile Include="PromiseEventType.cs" /> |
|
143 | <Compile Include="PromiseEventType.cs" /> | |
149 | <Compile Include="ComponentContainer.cs" /> |
|
|||
150 | <Compile Include="DisposablePool.cs" /> |
|
|||
151 | <Compile Include="ObjectPool.cs" /> |
|
|||
152 | <Compile Include="Parallels\AsyncQueue.cs" /> |
|
144 | <Compile Include="Parallels\AsyncQueue.cs" /> | |
153 | <Compile Include="PromiseT.cs" /> |
|
145 | <Compile Include="PromiseT.cs" /> | |
154 | <Compile Include="IDeferred.cs" /> |
|
146 | <Compile Include="IDeferred.cs" /> | |
@@ -180,6 +172,16 | |||||
180 | <Compile Include="SuccessPromiseT.cs" /> |
|
172 | <Compile Include="SuccessPromiseT.cs" /> | |
181 | <Compile Include="PromiseAwaiterT.cs" /> |
|
173 | <Compile Include="PromiseAwaiterT.cs" /> | |
182 | <Compile Include="PromiseAwaiter.cs" /> |
|
174 | <Compile Include="PromiseAwaiter.cs" /> | |
|
175 | <Compile Include="Components\ComponentContainer.cs" /> | |||
|
176 | <Compile Include="Components\Disposable.cs" /> | |||
|
177 | <Compile Include="Components\DisposablePool.cs" /> | |||
|
178 | <Compile Include="Components\ObjectPool.cs" /> | |||
|
179 | <Compile Include="Components\ServiceLocator.cs" /> | |||
|
180 | <Compile Include="Components\IInitializable.cs" /> | |||
|
181 | <Compile Include="TaskController.cs" /> | |||
|
182 | <Compile Include="Components\App.cs" /> | |||
|
183 | <Compile Include="Components\IRunnable.cs" /> | |||
|
184 | <Compile Include="Components\ExecutionState.cs" /> | |||
183 | </ItemGroup> |
|
185 | </ItemGroup> | |
184 | <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> |
|
186 | <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> | |
185 | <ItemGroup /> |
|
187 | <ItemGroup /> | |
@@ -252,4 +254,7 | |||||
252 | </Properties> |
|
254 | </Properties> | |
253 | </MonoDevelop> |
|
255 | </MonoDevelop> | |
254 | </ProjectExtensions> |
|
256 | </ProjectExtensions> | |
|
257 | <ItemGroup> | |||
|
258 | <Folder Include="Components\" /> | |||
|
259 | </ItemGroup> | |||
255 | </Project> No newline at end of file |
|
260 | </Project> |
@@ -2,9 +2,7 | |||||
2 | using System; |
|
2 | using System; | |
3 | using System.Collections.Generic; |
|
3 | using System.Collections.Generic; | |
4 | using System.IO; |
|
4 | using System.IO; | |
5 | using System.Linq; |
|
5 | using Implab.Components; | |
6 | using System.Text; |
|
|||
7 | using System.Threading.Tasks; |
|
|||
8 |
|
6 | |||
9 | namespace Implab.Parsing { |
|
7 | namespace Implab.Parsing { | |
10 | /// <summary> |
|
8 | /// <summary> |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now