Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface IHasBeenInstanced
- {
- LiveIdInstanceManager.LiveId LiveId { get; }
- }
- public class LiveIdInstanceManager
- {
- #region Clases auxiliares
- /// <summary>
- /// Clase base para una identificación unica por tipo.
- ///
- /// Quizás me plantee añadir un contador de referencias si es necesario
- /// </summary>
- public abstract class LiveId: IDisposable
- {
- int _InstanceId;
- public event Action<LiveId> InstanceIdChanged;
- protected LiveId( LiveIdInstanceManager IdManager )
- {
- this.IdManager = IdManager;
- }
- public abstract void SetToNewIdManager( LiveIdInstanceManager newIdManager );
- public int InstanceId
- {
- get { return _InstanceId; }
- protected set
- {
- if ( _InstanceId != value )
- {
- _InstanceId = value;
- if ( InstanceIdChanged!=null )
- InstanceIdChanged( this );
- }
- }
- }
- public LiveIdInstanceManager IdManager { get; protected set; }
- #region Miembros de IDisposable
- public abstract void Dispose( );
- #endregion
- }
- /// <summary>
- /// Clase para identificación unica por tipo
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public class LiveId<T>: LiveId where T: IHasBeenInstanced
- {
- public T Instance { get; private set; }
- public LiveId( LiveIdInstanceManager IdManager, T Instance )
- : base( IdManager )
- {
- this.Instance = Instance;
- InstanceId = IdManager.RegisterInstance( Instance );
- }
- public override void SetToNewIdManager( LiveIdInstanceManager newIdManager )
- {
- IdManager.UnRegisterInstance( Instance );
- IdManager = newIdManager;
- InstanceId = IdManager.RegisterInstance( Instance );
- }
- public override void Dispose( )
- {
- IdManager.UnRegisterInstance( Instance );
- }
- }
- /// <summary>
- /// Colección de LiveId
- /// </summary>
- public class LiveIdCollection: List<IHasBeenInstanced> { }
- #endregion
- Dictionary<Type, LiveIdCollection> IdCollections = new Dictionary<Type, LiveIdCollection>( );
- //----------------------------------------------------------------------------------------
- public int RegisterInstance( IHasBeenInstanced Instance )
- {
- var type = Instance.GetType( );
- if ( !IdCollections.ContainsKey( type ) ) IdCollections.Add( type, new LiveIdCollection( ) );
- int instanceId = (IdCollections[type].Count == 0) ? 0 : IdCollections[type].Max( m => m.LiveId.InstanceId );
- instanceId++;
- IdCollections[type].Add( Instance );
- return instanceId;
- }
- //----------------------------------------------------------------------------------------
- public void UnRegisterInstance( IHasBeenInstanced Instance )
- {
- var type = Instance.GetType( );
- IdCollections[type].Remove( Instance );
- }
- //----------------------------------------------------------------------------------------
- public void AgregateInstances( LiveIdInstanceManager other )
- {
- foreach ( var pair in other.IdCollections )
- {
- foreach ( var instance in pair.Value )
- {
- instance.LiveId.SetToNewIdManager( this );
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment