crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianStructuresLib/CodeSegments/CodeSegDefinitionCollection.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.Collections;
       
    19 using System.Collections.Generic;
       
    20 
       
    21 namespace SymbianStructuresLib.CodeSegments
       
    22 {
       
    23 	public class CodeSegDefinitionCollection : IEnumerable<CodeSegDefinition>
       
    24 	{
       
    25 		#region Constructors
       
    26 		public CodeSegDefinitionCollection()
       
    27 			: this( 10 )
       
    28 		{
       
    29 		}
       
    30 
       
    31 		public CodeSegDefinitionCollection( int aGranularity )
       
    32 		{
       
    33 			iEntries = new List<CodeSegDefinition>( aGranularity );
       
    34 		}
       
    35 
       
    36         public CodeSegDefinitionCollection( IEnumerable<CodeSegDefinition> aCopy )
       
    37         {
       
    38             iEntries = new List<CodeSegDefinition>();
       
    39             iEntries.AddRange( aCopy );
       
    40         }
       
    41 		#endregion
       
    42 
       
    43 		#region API
       
    44 		public void Reset()
       
    45 		{
       
    46             lock ( iEntries )
       
    47             {
       
    48                 iEntries.Clear();
       
    49             }
       
    50 		}
       
    51 
       
    52         public void Add( CodeSegDefinition aEntry )
       
    53 		{
       
    54             if ( string.IsNullOrEmpty( aEntry.FileName ) )
       
    55             {
       
    56                 throw new ArgumentException( "File name invalid" );
       
    57             }
       
    58 
       
    59             lock ( iEntries )
       
    60             {
       
    61                 if ( !Contains( aEntry ) )
       
    62                 {
       
    63                     iEntries.Add( aEntry );
       
    64                 }
       
    65             }
       
    66 		}
       
    67 
       
    68         public void AddRange( IEnumerable<CodeSegDefinition> aItems )
       
    69         {
       
    70             foreach ( CodeSegDefinition entry in aItems )
       
    71             {
       
    72                 Add( entry );
       
    73             }
       
    74         }
       
    75 
       
    76         public void Remove( CodeSegDefinition aEntry )
       
    77         {
       
    78             lock ( iEntries )
       
    79             {
       
    80                 int index = IndexOf( aEntry );
       
    81                 if ( index >= 0 )
       
    82                 {
       
    83                     iEntries.RemoveAt( index );
       
    84                 }
       
    85             }
       
    86         }
       
    87 
       
    88 		public void SortByFileName()
       
    89 		{
       
    90             lock ( iEntries )
       
    91             {
       
    92                 iEntries.Sort( new Internal.CSDCompareByFileName() );
       
    93             }
       
    94 		}
       
    95 
       
    96 		public void SortByAddress()
       
    97 		{
       
    98             lock ( iEntries )
       
    99             {
       
   100                 iEntries.Sort( new Internal.CSDCompareByAddress() );
       
   101             }
       
   102         }
       
   103 
       
   104         public bool Contains( string aFileName )
       
   105         {
       
   106             lock ( iEntries )
       
   107             {
       
   108                 CodeSegDefinition temp = new CodeSegDefinition( aFileName );
       
   109                 return Contains( temp );
       
   110             }
       
   111         }
       
   112 
       
   113         public bool Contains( CodeSegDefinition aEntry )
       
   114         {
       
   115             lock ( iEntries )
       
   116             {
       
   117                 int index = IndexOf( aEntry );
       
   118                 bool ret = ( index >= 0 );
       
   119                 return ret;
       
   120             }
       
   121         }
       
   122 
       
   123         public int IndexOf( CodeSegDefinition aEntry )
       
   124         {
       
   125             Predicate<CodeSegDefinition> predicate = delegate( CodeSegDefinition search )
       
   126             { 
       
   127                 return search.Equals( aEntry ); 
       
   128             };
       
   129             //
       
   130             lock ( iEntries )
       
   131             {
       
   132                 int ret = iEntries.FindIndex( predicate );
       
   133                 return ret;
       
   134             }
       
   135         }
       
   136 
       
   137         public CodeSegDefinition DefinitionByAddress( uint aAddress )
       
   138         {
       
   139             Predicate<CodeSegDefinition> predicate = delegate( CodeSegDefinition validate )
       
   140             { 
       
   141                 return validate.Contains( aAddress );
       
   142             };
       
   143             //
       
   144             lock ( iEntries )
       
   145             {
       
   146                 CodeSegDefinition ret = iEntries.Find( predicate );
       
   147                 return ret;
       
   148             }
       
   149         }
       
   150 		#endregion
       
   151 
       
   152 		#region Properties
       
   153 		public int Count
       
   154 		{
       
   155 			get
       
   156             {
       
   157                 lock ( iEntries )
       
   158                 {
       
   159                     return iEntries.Count;
       
   160                 }
       
   161             }
       
   162 		}
       
   163 
       
   164 		public CodeSegDefinition this[ int aIndex ]
       
   165 		{
       
   166             get
       
   167             {
       
   168                 lock ( iEntries )
       
   169                 {
       
   170                     return iEntries[ aIndex ];
       
   171                 }
       
   172             }
       
   173 		}
       
   174 
       
   175         public CodeSegDefinition this[ uint aAddress ]
       
   176         {
       
   177             get
       
   178             {
       
   179                 CodeSegDefinition ret = null;
       
   180                 //
       
   181                 foreach ( CodeSegDefinition entry in iEntries )
       
   182                 {
       
   183                     if ( entry.Contains( aAddress ) )
       
   184                     {
       
   185                         ret = entry;
       
   186                         break;
       
   187                     }
       
   188                 }
       
   189                 //
       
   190                 return ret;
       
   191             }
       
   192         }
       
   193 		#endregion
       
   194 
       
   195         #region IEnumerable Members
       
   196         public IEnumerator<CodeSegDefinition> GetEnumerator()
       
   197         {
       
   198             foreach ( CodeSegDefinition entry in iEntries )
       
   199             {
       
   200                 yield return entry;
       
   201             }
       
   202         }
       
   203  
       
   204         IEnumerator IEnumerable.GetEnumerator()
       
   205         {
       
   206             foreach ( CodeSegDefinition entry in iEntries )
       
   207             {
       
   208                 yield return entry;
       
   209             }
       
   210         }
       
   211         #endregion
       
   212 
       
   213 		#region Data members
       
   214 		private readonly List<CodeSegDefinition> iEntries;
       
   215 		#endregion
       
   216     }
       
   217 }