using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace FLocal.Core { class Registry where TData : IDataObject, new() where TKey : struct { public static readonly Registry instance = new Registry(); private Dictionary storage; private HashSet locks; protected Registry() { this.storage = new Dictionary(); this.locks = new HashSet(); } public TData Get(TKey id) { if(this.locks.Contains(id)) throw new CriticalException("Locked"); if(!this.storage.ContainsKey(id)) { try { this.locks.Add(id); //this.storage[id] = IDataObject.CreateByIdFromRegistry(id); //this.storage[id] = TData.CreateByIdFromRegistry(id); TData obj = new TData(); obj.CreateByIdFromRegistry(id); this.storage[id] = obj; this.locks.Remove(id); } catch(FLocalException e) { this.locks.Remove(id); throw e; } } return this.storage[id]; } public bool IsCached(TKey id) { if(this.locks.Contains(id)) throw new CriticalException("locked"); return this.storage.ContainsKey(id); } } }