CharcoStudios

LiveIdManager

Nov 5th, 2012
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.02 KB | None | 0 0
  1.     public interface IHasBeenInstanced
  2.     {
  3.         LiveIdInstanceManager.LiveId LiveId { get; }
  4.     }
  5.  
  6.     public class LiveIdInstanceManager
  7.     {
  8.         #region Clases auxiliares
  9.         /// <summary>
  10.         /// Clase base para una identificación unica por tipo.
  11.         ///
  12.         /// Quizás me plantee añadir un contador de referencias si es necesario
  13.         /// </summary>
  14.         public abstract class LiveId: IDisposable
  15.         {
  16.             int _InstanceId;
  17.  
  18.             public event Action<LiveId> InstanceIdChanged;
  19.  
  20.             protected LiveId( LiveIdInstanceManager IdManager )
  21.             {
  22.                 this.IdManager = IdManager;
  23.             }
  24.  
  25.             public abstract void SetToNewIdManager( LiveIdInstanceManager newIdManager );
  26.  
  27.             public int InstanceId
  28.             {
  29.                 get { return _InstanceId; }
  30.                 protected set
  31.                 {
  32.                     if ( _InstanceId != value )
  33.                     {
  34.                         _InstanceId = value;
  35.                         if ( InstanceIdChanged!=null )
  36.                             InstanceIdChanged( this );
  37.                     }
  38.                 }
  39.             }
  40.      
  41.             public LiveIdInstanceManager IdManager { get; protected set; }
  42.  
  43.             #region Miembros de IDisposable
  44.  
  45.             public abstract void Dispose( );
  46.  
  47.             #endregion
  48.         }
  49.  
  50.         /// <summary>
  51.         /// Clase para identificación unica por tipo
  52.         /// </summary>
  53.         /// <typeparam name="T"></typeparam>
  54.         public class LiveId<T>: LiveId where T: IHasBeenInstanced
  55.         {
  56.             public T Instance { get; private set; }
  57.  
  58.             public LiveId( LiveIdInstanceManager IdManager, T Instance )
  59.                 : base( IdManager )
  60.             {
  61.                 this.Instance = Instance;
  62.                 InstanceId = IdManager.RegisterInstance( Instance );
  63.             }
  64.  
  65.             public override void SetToNewIdManager( LiveIdInstanceManager newIdManager )
  66.             {
  67.                 IdManager.UnRegisterInstance( Instance );
  68.                 IdManager = newIdManager;
  69.                 InstanceId = IdManager.RegisterInstance( Instance );
  70.             }
  71.  
  72.             public override void Dispose( )
  73.             {
  74.                 IdManager.UnRegisterInstance( Instance );
  75.             }
  76.         }
  77.  
  78.  
  79.         /// <summary>
  80.         /// Colección de LiveId
  81.         /// </summary>
  82.         public class LiveIdCollection: List<IHasBeenInstanced> { }
  83.  
  84.         #endregion
  85.  
  86.         Dictionary<Type, LiveIdCollection> IdCollections = new Dictionary<Type, LiveIdCollection>( );
  87.  
  88.         //----------------------------------------------------------------------------------------
  89.         public int RegisterInstance( IHasBeenInstanced Instance )
  90.         {
  91.             var type = Instance.GetType( );
  92.             if ( !IdCollections.ContainsKey( type ) ) IdCollections.Add( type, new LiveIdCollection( ) );
  93.  
  94.             int instanceId = (IdCollections[type].Count == 0) ?  0 : IdCollections[type].Max( m => m.LiveId.InstanceId );
  95.  
  96.             instanceId++;
  97.  
  98.             IdCollections[type].Add( Instance );
  99.  
  100.             return instanceId;
  101.         }
  102.  
  103.         //----------------------------------------------------------------------------------------
  104.         public void UnRegisterInstance( IHasBeenInstanced Instance )
  105.         {
  106.             var type = Instance.GetType( );
  107.             IdCollections[type].Remove( Instance );
  108.         }
  109.  
  110.         //----------------------------------------------------------------------------------------
  111.         public void AgregateInstances( LiveIdInstanceManager other )
  112.         {
  113.             foreach ( var pair in other.IdCollections )
  114.             {                                  
  115.                 foreach ( var instance in pair.Value )
  116.                 {
  117.                     instance.LiveId.SetToNewIdManager( this );
  118.                 }
  119.             }                                                              
  120.         }
  121.     }
Add Comment
Please, Sign In to add comment