osncore/osncore/src/alfstringpool.cpp
changeset 17 3eca7e70b1b8
parent 3 4526337fb576
equal deleted inserted replaced
3:4526337fb576 17:3eca7e70b1b8
     1 /*
       
     2 * Copyright (c) 2007 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:  Implementation of the common string pool for widget and the factory model class.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <stdlib.h>
       
    19 
       
    20 
       
    21 #include "osn/alfstringpool.h"
       
    22 #include "osn/alfstring.h"
       
    23 
       
    24 
       
    25 const int KPoolAllocSize = 32;
       
    26 
       
    27 namespace osncore
       
    28     {
       
    29 class CAlfStringPoolImpl
       
    30     {
       
    31 public:    
       
    32     CAlfStringPoolImpl():iPool(0),iItemCount(0),iAllocCount(0)
       
    33         {
       
    34             
       
    35         }
       
    36     ~CAlfStringPoolImpl()
       
    37         {
       
    38         delete [] iPool;    
       
    39         }
       
    40     CAlfString* iPool;
       
    41     int iItemCount;
       
    42     int iAllocCount;
       
    43     };    
       
    44   
       
    45 
       
    46 //-----------------------------------------------------------------------
       
    47 // Only Method to access Strings from the String pool
       
    48 //-----------------------------------------------------------------------
       
    49 static CAlfString AppendToPool( 
       
    50     const CAlfString& aString, 
       
    51     CAlfStringPoolImpl*& aImpl)
       
    52     {
       
    53     bool found(false);
       
    54     CAlfString ret(aString);
       
    55     
       
    56     //Look if this string is present then just return the corresponding RAlfString else create new one and add it to the list
       
    57     for(int i=0;i<aImpl->iItemCount && !found;i++)
       
    58         {
       
    59         if( aImpl->iPool[i] == aString) //element found
       
    60             {
       
    61             found = true;
       
    62             ret = aImpl->iPool[i];
       
    63             }
       
    64         }
       
    65     if(!found)
       
    66         {
       
    67         if(aImpl->iItemCount < aImpl->iAllocCount )
       
    68             {
       
    69             aImpl->iPool[aImpl->iItemCount++] = aString;
       
    70             }
       
    71         else // // reserve more space for an array
       
    72             {
       
    73             CAlfString* tmp = (CAlfString*) realloc(aImpl->iPool, (sizeof (CAlfString) * KPoolAllocSize) );
       
    74             if(tmp)
       
    75                 {
       
    76                 aImpl->iAllocCount += KPoolAllocSize;
       
    77                 aImpl->iPool = tmp;
       
    78                 
       
    79                 aImpl->iPool[aImpl->iItemCount++] = aString;     
       
    80                 }
       
    81             else
       
    82                 {
       
    83                 User::LeaveIfNull(tmp);   
       
    84                 }    
       
    85             }   
       
    86         }
       
    87     return ret;
       
    88     }
       
    89 
       
    90 
       
    91 
       
    92 EXPORT_C  CAlfString CAlfStringPool::CreateStringL( const char* aString)
       
    93     {
       
    94     if(aString)
       
    95         {
       
    96         CAlfString tmp(aString);
       
    97         return AppendToPool(tmp,iImpl);    
       
    98         }
       
    99     return CAlfString();
       
   100     }
       
   101 
       
   102 EXPORT_C  CAlfString CAlfStringPool::CreateStringL( const CAlfString& aString)
       
   103     {
       
   104     return AppendToPool(aString,iImpl);
       
   105     }
       
   106 
       
   107 EXPORT_C CAlfStringPool::CAlfStringPool():iImpl(0)
       
   108     {
       
   109     iImpl = new(ELeave) CAlfStringPoolImpl;
       
   110     iImpl->iPool = new(ELeave)CAlfString[KPoolAllocSize];
       
   111     iImpl->iAllocCount = KPoolAllocSize;
       
   112     
       
   113     }
       
   114 
       
   115 //-----------------------------------------------------------------------
       
   116 // Default Destructor
       
   117 //-----------------------------------------------------------------------
       
   118 EXPORT_C CAlfStringPool::~CAlfStringPool()
       
   119     {
       
   120     
       
   121     delete iImpl;
       
   122     iImpl = NULL;
       
   123     }
       
   124 
       
   125 
       
   126   }