crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianUtils/TextUtilities/Readers/Types/Array/AsyncArrayReader.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.IO;
       
    19 using System.Text;
       
    20 using System.Threading;
       
    21 using System.Collections;
       
    22 using SymbianUtils.Tracer;
       
    23 
       
    24 namespace SymbianUtils.TextUtilities.Readers.Types.Array
       
    25 {
       
    26     public interface AsyncArrayObjectSupplier<TType>
       
    27     {
       
    28         #region AsyncArrayObjectSupplier interface declaration
       
    29         int ObjectCount { get; }
       
    30         TType this[ int aIndex ] { get; }
       
    31         #endregion
       
    32     }
       
    33 
       
    34 	public abstract class AsyncArrayReader<TType> : AsyncReaderBase
       
    35 	{
       
    36 		#region Constructors
       
    37         protected AsyncArrayReader( AsyncArrayObjectSupplier<TType> aObjectSupplier )
       
    38             : this( aObjectSupplier, null )
       
    39         {
       
    40         }
       
    41 
       
    42         protected AsyncArrayReader( AsyncArrayObjectSupplier<TType> aObjectSupplier, ITracer aTracer )
       
    43             : base( aTracer )
       
    44 		{
       
    45             iObjectSupplier = aObjectSupplier;
       
    46 		}
       
    47 		#endregion
       
    48 
       
    49         #region Abstract reading framework
       
    50         protected abstract void HandleObject( TType aObject, int aIndex, int aCount );
       
    51 		#endregion
       
    52 
       
    53         #region From AsyncReaderBase
       
    54         protected override long Size
       
    55         {
       
    56             get
       
    57             {
       
    58                 long size = 0;
       
    59                 //
       
    60                 lock( this )
       
    61                 {
       
    62                     size = iObjectSupplier.ObjectCount;
       
    63                 }
       
    64                 //
       
    65                 return size;
       
    66             }
       
    67         }
       
    68 
       
    69         protected override long Position
       
    70         {
       
    71             get
       
    72             {
       
    73                 long position = 0;
       
    74                 //
       
    75                 lock( this )
       
    76                 {
       
    77                     position = iCurrentIndex;
       
    78                 }
       
    79                 //
       
    80                 return position;
       
    81             }
       
    82         }
       
    83         #endregion
       
    84 
       
    85         #region Properties
       
    86         public int CurrentIndex
       
    87         {
       
    88             get
       
    89             {
       
    90                 lock( this )
       
    91                 {
       
    92                     return iCurrentIndex;
       
    93                 }
       
    94             }
       
    95         }
       
    96 
       
    97         protected AsyncArrayObjectSupplier<TType> ObjectSupplier
       
    98         {
       
    99             get { return iObjectSupplier; }
       
   100         }
       
   101         #endregion
       
   102 
       
   103 		#region Internal methods
       
   104 		protected override void PerformOperation()
       
   105 		{
       
   106             int count = 0;
       
   107             lock( this )
       
   108             {
       
   109                 count = iObjectSupplier.ObjectCount;
       
   110                 iCurrentIndex = -1;
       
   111             }
       
   112 
       
   113             System.DateTime tenPercentTime = DateTime.Now;
       
   114 
       
   115             int newProgress = 0;
       
   116             int oldProgress = 0;
       
   117             while( CurrentIndex < count - 1 )
       
   118             {
       
   119                 lock( this )
       
   120                 {
       
   121                     ++iCurrentIndex;
       
   122                 }
       
   123                 //
       
   124                 lock( this )
       
   125                 {
       
   126                     TType obj = iObjectSupplier[ iCurrentIndex ];
       
   127                     HandleObject( obj, iCurrentIndex-1, count );
       
   128                     newProgress = Progress;
       
   129                 }
       
   130                 //
       
   131                 if	( newProgress != oldProgress )
       
   132                 {
       
   133 #if DEBUG
       
   134                     if ( newProgress > 0 && ( newProgress % 10 ) == 0 )
       
   135                     {
       
   136                         System.DateTime now = DateTime.Now;
       
   137                         long intermediateTickDuration = ( ( now.Ticks - tenPercentTime.Ticks ) / 100 );
       
   138                         System.Diagnostics.Debug.WriteLine( newProgress.ToString( "d2" ) + " % COMPLETE - " + intermediateTickDuration.ToString( "d12" ) );
       
   139                         tenPercentTime = now;
       
   140                     }
       
   141 #endif
       
   142                     NotifyEvent( TEvent.EReadingProgress );
       
   143                     oldProgress = newProgress;
       
   144                 }
       
   145             }
       
   146 
       
   147             System.DateTime endTime = DateTime.Now;
       
   148             long tickDuration = ( ( endTime.Ticks - iOperationStartTime.Ticks ) / 100 );
       
   149             System.Diagnostics.Debug.WriteLine( "ARRAY READ COMPLETE - " + tickDuration.ToString( "d12" ) );
       
   150         }
       
   151 		#endregion
       
   152 
       
   153         #region Data members
       
   154         private int iCurrentIndex = -1;
       
   155         private readonly AsyncArrayObjectSupplier<TType> iObjectSupplier;
       
   156         #endregion
       
   157 	}
       
   158 }