hwpluginsimulation/mocksy/src/rotatingstrbuf.cpp
changeset 0 3553901f7fa8
child 24 6638e7f4bd8f
equal deleted inserted replaced
-1:000000000000 0:3553901f7fa8
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 
       
    17 #include "rotatingstrbuf.h"
       
    18 
       
    19 CRotatingStrBuf* CRotatingStrBuf::NewL(TInt aSize)
       
    20 	{
       
    21 	CRotatingStrBuf *self = new (ELeave) CRotatingStrBuf;
       
    22 	CleanupStack::PushL(self);
       
    23 	self->ConstructL(aSize);
       
    24 	CleanupStack::Pop();
       
    25 	return self;
       
    26 	}
       
    27 	
       
    28 CRotatingStrBuf::~CRotatingStrBuf()
       
    29 	{
       
    30 	TEntry* entry;
       
    31 	while (!iQueue.IsEmpty())
       
    32 		{
       
    33 		entry = iQueue.First();
       
    34 		entry->iLink.Deque();
       
    35 		delete entry->iText;
       
    36 		delete entry;
       
    37 		}
       
    38 	}
       
    39 
       
    40 CRotatingStrBuf::CRotatingStrBuf()
       
    41 	:iQueue(_FOFF(TEntry,iLink))
       
    42 	{
       
    43 	}
       
    44 
       
    45 void CRotatingStrBuf::ConstructL(TInt aSize)
       
    46 	{
       
    47 	for(TInt cnt=0; cnt<aSize; cnt++)
       
    48 		{
       
    49 		TEntry* entry = new (ELeave) TEntry;
       
    50 		entry->iText = NULL;
       
    51 		iQueue.AddLast(*entry);
       
    52 		}
       
    53 	}
       
    54 
       
    55 void CRotatingStrBuf::Put(const TDesC& aText)
       
    56 	{
       
    57 	TEntry* entry = iQueue.Last();
       
    58 	// move last entry to the top of the queue
       
    59 	entry->iLink.Deque();
       
    60 	iQueue.AddFirst(*entry);
       
    61 	// alocate/resize buffer
       
    62 	if (entry->iText == NULL)
       
    63 		entry->iText = HBufC::New(aText.Length());
       
    64 	else if (entry->iText->Des().MaxLength() < aText.Length())
       
    65 		entry->iText = entry->iText->ReAlloc(aText.Length());
       
    66 	// copy the text
       
    67 	if (entry->iText != NULL) // New or ReAlloc may have fail.
       
    68 		*entry->iText = aText;
       
    69 	}
       
    70 
       
    71 HBufC* CRotatingStrBuf::Get()
       
    72 	{
       
    73 	TDblQueIter<TEntry> iter(iQueue);
       
    74 	iter.SetToLast();
       
    75 
       
    76 	TEntry* entry;
       
    77 	while ((entry = iter--) != NULL && entry->iText == NULL)
       
    78 		{};
       
    79 	
       
    80 	if (entry != NULL)
       
    81 		{
       
    82 		HBufC* ret = entry->iText;
       
    83 		entry->iText = NULL;
       
    84 		return ret;
       
    85 		}
       
    86 	return NULL;
       
    87 	}
       
    88