crashanalysercmd/Libraries/Engine/CrashItemLib/Crash/Base/CIElementDictionary.cs
changeset 0 818e61de6cd1
equal deleted inserted replaced
-1:000000000000 0:818e61de6cd1
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 * 
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 using System;
       
    18 using System.Text;
       
    19 using System.IO;
       
    20 using System.Collections.Generic;
       
    21 using System.ComponentModel;
       
    22 using CrashItemLib.Crash;
       
    23 using CrashItemLib.Crash.Base;
       
    24 using CrashItemLib.Crash.Registers;
       
    25 using CrashItemLib.Crash.Container;
       
    26 using SymbianStructuresLib.Uids;
       
    27 
       
    28 namespace CrashItemLib.Crash.Base
       
    29 {
       
    30     public class CIElementDictionary<T> : CIElement, IEnumerable<T> where T: CIElement
       
    31 	{
       
    32 		#region Constructors
       
    33         public CIElementDictionary( CIContainer aContainer )
       
    34             : base( aContainer )
       
    35 		{
       
    36 		}
       
    37 		#endregion
       
    38 
       
    39         #region API
       
    40         [Browsable( false )]
       
    41         [EditorBrowsable( EditorBrowsableState.Never )]
       
    42         public override void AddChild( CIElement aChild )
       
    43         {
       
    44             throw new NotSupportedException( "Use Add() instead" );
       
    45         }
       
    46 
       
    47         [Browsable(false )]
       
    48         [EditorBrowsable( EditorBrowsableState.Never )]
       
    49         public override void AddChildren( CIElement[] aChildren )
       
    50         {
       
    51             throw new NotSupportedException( "Use Add() instead" );
       
    52         }
       
    53 
       
    54         [Browsable(false )]
       
    55         [EditorBrowsable( EditorBrowsableState.Never )]
       
    56         public override void RemoveChild( CIElement aChild )
       
    57         {
       
    58             throw new NotSupportedException( "Use Remove() instead" );
       
    59         }
       
    60 
       
    61         public override void Clear()
       
    62         {
       
    63             base.Clear();
       
    64             iDictionary.Clear();
       
    65         }
       
    66 
       
    67         public virtual bool Contains( T aEntry )
       
    68         {
       
    69             CIElement element = CheckValid( aEntry );
       
    70             return iDictionary.ContainsKey( element.Id );
       
    71         }
       
    72 
       
    73         public override bool Contains( CIElement aElement )
       
    74         {
       
    75             return this.Contains( aElement.Id );
       
    76         }
       
    77 
       
    78         public override bool Contains( CIElementId aId )
       
    79         {
       
    80             return iDictionary.ContainsKey( aId );
       
    81         }
       
    82 
       
    83         public virtual bool Add( T aEntry )
       
    84         {
       
    85             CIElement element = CheckValid( aEntry );
       
    86 
       
    87             bool needsAdd = iDictionary.ContainsKey( element.Id ) == false;
       
    88             if ( needsAdd )
       
    89             {
       
    90                 iDictionary.Add( element.Id, aEntry );
       
    91 
       
    92                 // Treat as though it was added as a child
       
    93                 base.OnElementAddedToSelf( element );
       
    94             }
       
    95 
       
    96             return needsAdd;
       
    97         }
       
    98 
       
    99         public virtual void AddRange( IEnumerable<T> aEnumerable )
       
   100         {
       
   101             foreach ( T t in aEnumerable )
       
   102             {
       
   103                 Add( t );
       
   104             }
       
   105         }
       
   106 
       
   107         public virtual void AddRange( T[] aArray )
       
   108         {
       
   109             AddRange( (IEnumerable<T>) aArray );
       
   110         }
       
   111 
       
   112         public virtual void Remove( T aEntry )
       
   113         {
       
   114             CIElement element = CheckValid( aEntry );
       
   115             if ( iDictionary.ContainsKey( element.Id ) )
       
   116             {
       
   117                 iDictionary.Remove( element.Id );
       
   118             }
       
   119         }
       
   120 
       
   121         public override bool IsInContainer
       
   122         {
       
   123             get { return base.IsInContainer; }
       
   124             internal set
       
   125             {
       
   126                 base.IsInContainer = value;
       
   127                 //
       
   128                 foreach ( KeyValuePair<CIElementId, T> kvp in iDictionary )
       
   129                 {
       
   130                     CIElement element = kvp.Value as CIElement;
       
   131                     if ( element != null )
       
   132                     {
       
   133                         element.IsInContainer = value;
       
   134                     }
       
   135                 }
       
   136             }
       
   137         }
       
   138         #endregion
       
   139 
       
   140         #region Properties
       
   141         public new int Count
       
   142         {
       
   143             get { return iDictionary.Count; }
       
   144         }
       
   145 
       
   146         public new T this[ CIElementId aId ]
       
   147         {
       
   148             get
       
   149             {
       
   150                 T ret = default(T);
       
   151                 //
       
   152                 if ( iDictionary.ContainsKey( aId ) )
       
   153                 {
       
   154                     ret = iDictionary[ aId ];
       
   155                 }
       
   156                 //
       
   157                 return ret;
       
   158             }
       
   159         }
       
   160         #endregion
       
   161 
       
   162         #region From CIElement
       
   163         internal override void DoFinalize( CIElementFinalizationParameters aParams, Queue<CIElement> aCallBackLast, bool aForceFinalize )
       
   164         {
       
   165             DoFinalizeElements( aParams, aCallBackLast, aForceFinalize, this );
       
   166             base.DoFinalize( aParams, aCallBackLast, aForceFinalize );
       
   167         }
       
   168 
       
   169         internal override void GetChildrenByType<ChildType>( CIElementList<ChildType> aList, TChildSearchType aType, Predicate<ChildType> aPredicate )
       
   170         {
       
   171  	        // Get all direct children, and if recusion enabled, then fetch the
       
   172             // entire tree.
       
   173             Type t = typeof( ChildType );
       
   174             foreach( T entry in this )
       
   175             {
       
   176                 CIElement element = entry;
       
   177                 //
       
   178                 if ( t.IsAssignableFrom( element.GetType() ) )
       
   179                 {
       
   180                     // Get entry of correct type
       
   181                     ChildType objectEntry = (ChildType) ( (object) element );
       
   182 
       
   183                     // Check whether it is suitable for inclusion via our predicate
       
   184                     bool addEntry = true;
       
   185                     if ( aPredicate != null )
       
   186                     {
       
   187                         addEntry = aPredicate( objectEntry );
       
   188                     }
       
   189 
       
   190                     // Is it okay to take the entry?
       
   191                     if ( addEntry )
       
   192                     {
       
   193                         aList.Add( objectEntry );
       
   194                     }
       
   195                 }
       
   196 
       
   197                 // Get the element's children
       
   198                 if ( aType == TChildSearchType.EEntireHierarchy )
       
   199                 {
       
   200                     element.GetChildrenByType<ChildType>( aList, aType, aPredicate );
       
   201                 }
       
   202             }
       
   203         } 
       
   204         #endregion
       
   205 
       
   206         #region Internal methods
       
   207         protected virtual CIElement CheckValid( T aEntry )
       
   208         {
       
   209             CIElement element = aEntry as CIElement;
       
   210             if ( element == null )
       
   211             {
       
   212                 throw new NotSupportedException( "CIElementDictionary can only contain CIElement derived objects" );
       
   213             }
       
   214             return element;
       
   215         }
       
   216         #endregion
       
   217 
       
   218         #region From IEnumerable<T>
       
   219         public new IEnumerator<T> GetEnumerator()
       
   220         {
       
   221             foreach ( KeyValuePair<CIElementId, T > kvp in iDictionary )
       
   222             {
       
   223                 yield return kvp.Value;
       
   224             }
       
   225         }
       
   226 
       
   227         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
       
   228         {
       
   229             foreach ( KeyValuePair<CIElementId, T> kvp in iDictionary )
       
   230             {
       
   231                 yield return kvp.Value;
       
   232             }
       
   233         }
       
   234         #endregion
       
   235 
       
   236         #region Data members
       
   237         private SortedDictionary<CIElementId, T> iDictionary = new SortedDictionary<CIElementId, T>();
       
   238         #endregion
       
   239     }
       
   240 }