examples/Base/ArraysAndLists/DynamicArrays/DynamicArrays.cpp

00001 /*
00002 Copyright (c) 2000-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description: Examples to demonstrate arrays  
00028 */
00029 
00030 #include "CommonFramework.h"
00031 #include <e32math.h>
00032 #include <e32std.h>
00033 
00034 _LIT(KMsgNewLine,"\n");
00035 _LIT(KMsgPressAnyKey," (press any key to continue)\n");
00036 _LIT(KFormat1,"X%u");
00037 
00038 const TText* firstElement =  _S("DynamicArray: first");
00039 const TText* secondElement = _S("Example Code: second");
00040 const TText* thirdElement =  _S("ArrayKeys Ex: third");
00041 
00042 //
00043 // A T type class used in the example demonstrating the:
00044 // CArrayFixFlat class
00045 //
00046 class TElement
00047         {
00048 public :
00049         TElement();
00050 public :
00051         TBuf<4> iData;
00052         };
00053 
00054 TElement::TElement()
00055         {
00056         _LIT(KTextBase,"BASE");
00057         iData = KTextBase;
00058         }
00059 
00060 class CArrayElement
00061         {
00062 public :
00063         CArrayElement();
00064         TBool operator==(const CArrayElement& aElement) const;
00065 public :
00066         TBuf<4> iData;
00067         };
00068 
00069 CArrayElement::CArrayElement()
00070         {
00071         _LIT(KTextBase,"BASE");
00072         iData = KTextBase;
00073         }
00074 
00075 TBool CArrayElement::operator==(const CArrayElement& aElement) const
00076         {
00077         return (iData == aElement.iData);
00078         }
00079 
00080 //
00081 // A CBase derived class used in the example demonstrating the CArrayPtrFlat
00082 // array
00083 //
00084 class CElement : public CBase
00085         {
00086 public :
00087         CElement();
00088         ~CElement();
00089         void SetTextL(const TDesC& aText);
00090 public :
00091         HBufC* iBuf;
00092         };
00093 
00094 CElement::CElement()
00095         {
00096         }
00097 
00098 CElement::~CElement()
00099         {
00100         delete iBuf;
00101         }
00102 
00107 void CElement::SetTextL(const TDesC& aText)
00108         {
00109         if (!iBuf)
00110                 {
00111                 // The buffer is not created.
00112                 // Hence, create the heap buffer.
00113                 // Set the length of CElement::iBuf to the length of aText.
00114                 iBuf  = HBufC::NewL(aText.Length());
00115                 }
00116 
00117         else if(iBuf->Length() != aText.Length() )
00118                 {
00119                 // The Buffer exists but is a different size to the input text.
00120                 iBuf = iBuf->ReAllocL(aText.Length());
00121                 }
00122         // Copy the contents of aText to iBuf.
00123         *iBuf = aText;
00124         }
00125 
00126 TInt Order64Bit(const TInt64& aLeft, const TInt64& aRight)
00127      {
00128      if (aLeft<aRight)
00129          return -1;
00130     if (aLeft==aRight)
00131          return 0;
00132      return 1;
00133      }
00134 
00141 class TIntPtrElement
00142         {
00143 public :
00144         TIntPtrElement();
00145         void SetIntPtr(TInt* aPtr);
00146 public :
00147         TBuf<4> iData;
00152         TInt* iIntPtr;
00153         };
00154 
00158 TIntPtrElement::TIntPtrElement()
00159         {
00160         _LIT(KTextBase,"BASE");
00161         iData = KTextBase;
00162         }
00163 
00168 void TIntPtrElement::SetIntPtr(TInt* aPtr)
00169         {
00170         iIntPtr = aPtr;
00171         }
00179 class TKeyTIntPtr : public TKeyArrayFix
00180         {
00181 public:
00182         TKeyTIntPtr(TInt aOffset, TKeyCmpText aType);
00183         TKeyTIntPtr(TInt aOffset, TKeyCmpNumeric aType);
00184         TAny* At(TInt aIndex);
00185         };
00186 
00192 TKeyTIntPtr::TKeyTIntPtr(TInt aOffset, TKeyCmpText aType):TKeyArrayFix(aOffset,aType)
00193         {
00194         }
00195 
00200 TKeyTIntPtr::TKeyTIntPtr(TInt aOffset, TKeyCmpNumeric aType):TKeyArrayFix(aOffset,aType)
00201         {
00202         }
00203 
00210 TAny* TKeyTIntPtr::At(TInt aIndex)
00211         {
00212         if (aIndex==KIndexPtr)
00213         {
00214         return((TUint8 *)iPtr+iKeyOffset);
00215         }
00216         // Get the address of the array element.
00217         TIntPtrElement* ptr = (TIntPtrElement *)(iBase->Ptr(aIndex*sizeof(TIntPtrElement)).Ptr()+iKeyOffset);
00218         // The TIntPtrElement::iIntPtr points to the key.
00219         // Return the key.
00220         return(ptr->iIntPtr);
00221         }
00222 
00227 void PrintFixIntPtrArray(CArrayFixFlat<TIntPtrElement>& aFixFlat)
00228         {
00229         _LIT(KMsgContentsIntPtr,"Contents of the array of TIntPtrElement objects.\n");
00230         console->Printf(KMsgContentsIntPtr);
00231         for (TInt forix = 0; forix < aFixFlat.Count(); forix++)
00232                 {
00233                 console->Printf(KMsgNewLine);
00234                 _LIT(KMsgBuf,"Index: %d\n Descriptor:");
00235                 console->Printf(KMsgBuf,forix);
00236                 console->Printf(aFixFlat[forix].iData);
00237                 _LIT(KMsgInt,"\t\tInteger Value: %d");
00238                 console->Printf(KMsgInt,*(aFixFlat[forix].iIntPtr));
00239                 }
00240         console->Printf(KMsgNewLine);
00241         }
00242 
00247 void PrintFixArray(CArrayFixFlat<TElement>& aFixFlat)
00248         {
00249         for (TInt forix = 0; forix < aFixFlat.Count(); forix++)
00250                 {
00251                 console->Printf(KMsgNewLine);
00252                 console->Printf(aFixFlat[forix].iData);
00253                 }
00254         console->Printf(KMsgNewLine);
00255         }
00256 
00261 void PrintPtrArray(CArrayPtrFlat<CElement>* aPtrFlat)
00262         {
00263         _LIT(KCommonFormat4,"\n%S");
00264         for (TInt forix = 0; forix < aPtrFlat->Count(); forix++)
00265                 {
00266                 console->Printf(KCommonFormat4,((*aPtrFlat)[forix])->iBuf);
00267                 }
00268         }
00269 
00273 class TMySwap : public TSwap
00274         {
00275 public:
00276         TMySwap(CArrayFixFlat<TElement>* aArray);
00277         void Swap(TInt aLeft,TInt aRight);
00278 private:
00279                                 // The Swap() function is defined for this array.
00280         CArrayFixFlat<TElement>* iArray;
00281         };
00282 
00286 TMySwap::TMySwap(CArrayFixFlat<TElement>* aArray):iArray(aArray)
00287         {
00288         }
00289 
00295 void TMySwap::Swap(TInt aLeft,TInt aRight)
00296         {
00297         TBuf<4> tbuf(iArray->At(aLeft).iData);
00298         iArray->At(aLeft).iData.Copy(iArray->At(aRight).iData);
00299         iArray->At(aRight).iData.Copy(tbuf);
00300         }
00301 
00307 void DemoPakArrayFindL(CArrayPak<TText>& aPakFlat)
00308         {
00309         // Append TText strings to the aPakFlat array.
00310 
00311         // The length of the string to be appended is increased by one
00312         // to insert the '\0' character at the end of the TText string.
00313         // This is needed as the '\0' character is absent in descriptors.
00314         aPakFlat.AppendL(*firstElement,(User::StringLength(firstElement)+1)*sizeof(TText));
00315 
00316         // Insert the second element into the array.
00317         aPakFlat.AppendL(*secondElement,(User::StringLength(secondElement)+1)*sizeof(TText));
00318 
00319         // Insert the third element into the array.
00320         aPakFlat.AppendL(*thirdElement,(User::StringLength(thirdElement)+1)*sizeof(TText));
00321 
00322         // Print the array elements.
00323         _LIT(KCommonFormat7,"Array Contents");
00324         console->Printf(KCommonFormat7);
00325         console->Printf(KMsgPressAnyKey);
00326         console->Getch();
00327 
00328         TInt length = aPakFlat.Count();
00329         for(TInt ix = 0; ix < length; ix++)
00330                 {
00331                 console->Printf(TPtrC(&aPakFlat[ix]));
00332                 console->Printf(KMsgNewLine);
00333                 }
00334 
00335         // Find an element in the array.
00336         _LIT(KMsgFind,"\n--->Find");
00337         console->Printf(KMsgFind);
00338         _LIT(KMsgFindElement,"\nSearching the array for:");
00339         console->Printf(KMsgFindElement);
00340 
00341         // The descriptor, buf contains the string "Code".
00342         // The CArrayPak::Find() function will try to find this string in the array.
00343         console->Printf(TPtrC(thirdElement));
00344 
00345         // The key to find the element.
00346         // The length of the key is set to 12.
00347         // Thus, first 12 characters of all strings are compared with the key.
00348         TKeyArrayPak keypak(0,ECmpNormal,12);
00349 
00350         // Initialise the index to -1.
00351         TInt index = -1;
00352 
00353         // Perform the search.
00354         if(aPakFlat.Find(*thirdElement,keypak,index) == 0)
00355                 {
00356                 // Successful search.
00357                 // The index variable holds the index of the element found.
00358                 _LIT(KElementFound,"\nElement found at %d\n");
00359                 console->Printf(KElementFound,index);
00360                 }
00361         else
00362                 {
00363                 // Unsuccessful search.
00364                 // The element is not found.
00365                 _LIT(KElementNotFound,"\nElement not found");
00366                 console->Printf(KElementNotFound);
00367                 }
00368         }
00369 
00375 void DemoVarArraySortL(CArrayVar<TText>& aVarFlat)
00376         {
00377         // Append TText strings to the aVarFlat array.
00378 
00379         // The length of the string to be appended is increased by one
00380         // to insert the '\0' character at the end of the TText string.
00381         // This is needed as the '\0' character is absent in descriptors.
00382         aVarFlat.AppendL(*firstElement,(User::StringLength(firstElement)+1)*sizeof(TText));
00383 
00384         // Insert the second and third elements into the array.
00385         aVarFlat.AppendL(*secondElement,(User::StringLength(secondElement)+1)*sizeof(TText));
00386         aVarFlat.AppendL(*thirdElement,(User::StringLength(thirdElement)+1)*sizeof(TText));
00387 
00388         // Print the array elements.
00389         _LIT(KCommonFormat6,"Array Contents [Before Sort]");
00390         console->Printf(KCommonFormat6);
00391 
00392         console->Printf(KMsgPressAnyKey);
00393         console->Getch();
00394 
00395         TInt length = aVarFlat.Count();
00396         for(TInt ix = 0; ix < length; ix++)
00397                 {
00398                 console->Printf(TPtrC(&aVarFlat[ix]));
00399                 console->Printf(KMsgNewLine);
00400                 }
00401 
00402         // The key to find the element.
00403         // The length of the key is set to 12.
00404         // Thus, first 12 characters of the string are compared with the key.
00405         TKeyArrayVar keyvar(0,ECmpFolded,12);
00406 
00407         // Sort the array using the TKeyArrayVar key.
00408         aVarFlat.Sort(keyvar);
00409 
00410         // Print the sorted array elements.
00411         _LIT(KMsgSort,"\n--->Sort");
00412         console->Printf(KMsgSort);
00413         console->Printf(KMsgPressAnyKey);
00414         console->Getch();
00415 
00416         for(TInt ix = 0; ix < length; ix++)
00417                 {
00418                 console->Printf(TPtrC(&aVarFlat[ix]));
00419                 console->Printf(KMsgNewLine);
00420                 }
00421         }
00426 void DemoFlatArrayL(CArrayFixFlat<TElement>& aFixFlat)
00427         {
00428         TElement theElement;
00429 
00430         TInt value;
00431         for (value = 0; value < 3; value++)
00432                 {
00433                 theElement.iData.Format(KFormat1,value);
00434                 aFixFlat.AppendL(theElement);
00435                 }
00436 
00437                                 // Show length of each element.
00438                                 // The length of each element of the array is the size of the
00439                                 // TElement class.
00440         _LIT(KCommonFormat2,"\nLength()=%d");
00441         console->Printf(KCommonFormat2,aFixFlat.Length());
00442 
00443                                 // Show number of elements.
00444         _LIT(KCommonFormat3,"\nCount()=%d");
00445         console->Printf(KCommonFormat3,aFixFlat.Count());
00446 
00447                                 // Show each element
00448         PrintFixArray(aFixFlat);
00449                                 //
00450                                 // InsertL  * * * * * * * * *
00451                                 //
00452         _LIT(KMsgInsert,"\n\n--->InsertL");
00453         console->Printf(KMsgInsert);
00454         _LIT(KMsgPressAnyKey," (press any key to continue)\n");
00455         console->Printf(KMsgPressAnyKey);
00456         console->Getch();
00457 
00458                                 // Insert elements
00459                                 // ... at the beginning,
00460         _LIT(KMsgBEG,"BEG");
00461         theElement.iData = KMsgBEG;
00462         aFixFlat.InsertL(0,theElement);
00463 
00464                                 // ... near the middle,
00465         _LIT(KMsgMID,"MID");
00466         theElement.iData = KMsgMID;
00467         aFixFlat.InsertL(2,theElement);
00468 
00469                                 // ... at the end.
00470                                 // This is the same as using the AppendL() function
00471         _LIT(KMsgEND,"END");
00472         theElement.iData = KMsgEND;
00473         aFixFlat.InsertL(aFixFlat.Count(),theElement);
00474 
00475                                 // Show number of elements
00476         _LIT(KCommonFormat5,"Count()=%d");
00477         console->Printf(KCommonFormat5,aFixFlat.Count());
00478 
00479                                 // Show each element
00480         PrintFixArray(aFixFlat);
00481 
00482                                 // Define the key to sort the array.
00483                                 // The data member of the TElement class, iData, is the key.
00484         TKeyArrayFix keyfix(_FOFF(TElement,iData),ECmpFolded);
00485 
00486                                 // Sort the array.
00487         aFixFlat.Sort(keyfix);
00488 
00489                                 // Print the sorted array.
00490         _LIT(KMsgSort,"\n--->Sort");
00491         console->Printf(KMsgSort);
00492         console->Printf(KMsgPressAnyKey);
00493         console->Getch();
00494 
00495         PrintFixArray(aFixFlat);
00496 
00497                                 //
00498                                 // Delete  * * * * * * * * *
00499                                 //
00500         _LIT(KMsgDelete,"\n\n--->Delete");
00501         console->Printf(KMsgDelete);
00502         console->Printf(KMsgPressAnyKey);
00503         console->Getch();
00504 
00505                                 // Delete the 2nd, 4th and 5th elements. Note:
00506                                 //
00507                                 //  1. We use position to identify the elements (i.e. offset)
00508                                 //  2. As elements are deleted, the position of other
00509                                 //       elements changes.
00510 
00511         aFixFlat.Delete(1);             // delete the 2nd
00512         aFixFlat.Delete(2,2);   // delete what are now the 3rd and 4th
00513 
00514         aFixFlat.Compress();    // compress the array
00515 
00516                                 // Show number of elements
00517         console->Printf(KCommonFormat5,aFixFlat.Count());
00518 
00519                                 // Show each element
00520         PrintFixArray(aFixFlat);
00521                                 //
00522                                 // Reset  * * * * * * * * *
00523                                 //
00524         _LIT(KMsgReset,"\n\n--->Reset");
00525         console->Printf(KMsgReset);
00526         console->Printf(KMsgPressAnyKey);
00527         console->Getch();
00528 
00529                                 // reset the array (i.e. empty it)
00530         aFixFlat.Reset();
00531 
00532                                 // Show number of elements
00533         console->Printf(KCommonFormat5,aFixFlat.Count());
00534 
00535                                 // Show each element
00536         PrintFixArray(aFixFlat);
00537 
00538                                 //
00539                                 // ExpandL & ExtendL  * * * * * * * * *
00540                                 //
00541         _LIT(KMsgExpandExtend,"\n\n--->ExpandL & ExtendL");
00542         console->Printf(KMsgExpandExtend);
00543         console->Printf(KMsgPressAnyKey);
00544         console->Getch();
00545 
00546                                 // re-build the array
00547         _LIT(KFormat6,"%u");
00548         for (value = 0; value < 3; value++)
00549                 {
00550                 theElement.iData.Format(KFormat6,value);
00551                 aFixFlat.AppendL(theElement);
00552                 }
00553                                 // Expand by constructing a new element at position 1.
00554                                 // Extend the array.
00555                                 //
00556                                 // Note the use of the TElement default constructor
00557                                 // in both cases
00558         aFixFlat.ExpandL(1);
00559         aFixFlat.ExtendL();
00560 
00561                                 // Show number of elements
00562         console->Printf(KCommonFormat5,aFixFlat.Count());
00563 
00564                                 // Show each element
00565         PrintFixArray(aFixFlat);
00566 
00567                                 //
00568                                 // ResizeL * * * * * * * * * * * *
00569                                 //
00570         _LIT(KMsgResize,"\n\n--->ResizeL");
00571         console->Printf(KMsgResize);
00572         console->Printf(KMsgPressAnyKey);
00573         console->Getch();
00574 
00575                                 // Resize the array so that it only contains
00576                                 // one element
00577         aFixFlat.ResizeL(1);
00578 
00579                                 // Resize so that it contains 3 elements.
00580                                 // The two new elements are bit-wise copies
00581                                 // of a TElement object constructed using
00582                                 // its default constructor.
00583         aFixFlat.ResizeL(3);
00584 
00585                                 // Resize so that it contains 5 elements.
00586                                 // The two new elements are bit-wise copies of
00587                                 // the TElement object passed through
00588                                 // the reference.
00589         _LIT(KMsgXXX,"XXX");
00590         theElement.iData = KMsgXXX;
00591         aFixFlat.ResizeL(5,theElement);
00592 
00593                                 // Show number of elements
00594         console->Printf(KCommonFormat5,aFixFlat.Count());
00595 
00596                                 // Show each element
00597         PrintFixArray(aFixFlat);
00598 
00599                                 //
00600                                 // SetReserveL * * * * * * * * * * * *
00601                                 //
00602         _LIT(KMsgSetReserve,"\n\n--->SetReserveL");
00603         console->Printf(KMsgSetReserve);
00604         console->Printf(KMsgPressAnyKey);
00605         console->Getch();
00606 
00607                                 // Reserve sufficient space to append another
00608                                 // five elements.
00609                                 // This function may leave if there is
00610                                 // insufficient memory.
00611                                 // NOTE: this does NOT increase the number of
00612                                 //         elements in the array.
00613         aFixFlat.SetReserveL(5);
00614 
00615                                 // We can now append five elements and be sure that
00616                                 // no leave will occur.
00617         _LIT(KMsgDoubleAtoE,"AABBCCDDEE");
00618         TBufC<10> source(KMsgDoubleAtoE);
00619         TInt forix;
00620         for (forix = 0; forix < source.Length(); forix+=2)
00621                 {
00622                 theElement.iData = source.Mid(forix,2);
00623                 aFixFlat.AppendL(theElement);
00624                 }
00625 
00626                                 // Show number of elements
00627         console->Printf(KCommonFormat5,aFixFlat.Count());
00628 
00629                                 // Show each element
00630         PrintFixArray(aFixFlat);
00631 
00632                                 // An object of the TMySwap class.
00633                                 // It is used to swap two elements of the array.
00634         TMySwap swap(&aFixFlat);
00635 
00636                                 // Swap the first and the last element of the array.
00637         swap.Swap(0,(aFixFlat.Count() - 1));
00638 
00639                                 // Print the array.
00640         _LIT(KMsgSwap,"\n--->Swap [First and Last element]");
00641         console->Printf(KMsgSwap);
00642         console->Printf(KMsgPressAnyKey);
00643         console->Getch();
00644 
00645         PrintFixArray(aFixFlat);
00646         console->Printf(KMsgPressAnyKey);
00647         console->Getch();
00648         }
00649 
00654 void DemoPtrArrayL(CArrayPtrFlat<CElement>& aPtrFlat)
00655         {
00656         CElement*  ptr;
00657         ptr = new (ELeave) CElement;
00658         _LIT(KMsgFirstElement,"First Element");
00659         ptr->SetTextL(KMsgFirstElement);
00660         aPtrFlat.AppendL(ptr);
00661 
00662 
00663         ptr = new (ELeave) CElement;
00664         _LIT(KMsgSecondElement,"Second Element");
00665         ptr->SetTextL(KMsgSecondElement);
00666         aPtrFlat.AppendL(ptr);
00667 
00668 
00669         ptr = new (ELeave) CElement;
00670         _LIT(KMsgThirdElement,"Third Element");
00671         ptr->SetTextL(KMsgThirdElement);
00672         aPtrFlat.AppendL(ptr);
00673 
00674 
00675                                 // Show length of each element
00676         _LIT(KCommonFormat2,"\nLength()=%d");
00677         console->Printf(KCommonFormat2,aPtrFlat.Length());
00678 
00679                                 // Show number of elements
00680         _LIT(KCommonFormat3,"\nCount()=%d");
00681         console->Printf(KCommonFormat3,aPtrFlat.Count());
00682 
00683                                 // Show each element
00684         PrintPtrArray(&aPtrFlat);
00685                                 //
00686                                 // InsertL  * * * * * * * * *
00687                                 //
00688         _LIT(KMsgInsert,"\n\n--->InsertL");
00689         console->Printf(KMsgInsert);
00690         console->Printf(KMsgPressAnyKey);
00691         console->Getch();
00692 
00693                                 // Insert an element at the beginning
00694                                 // of the array.
00695 
00696         ptr = new (ELeave) CElement;
00697         _LIT(KMsgInsertedBeg,"Inserted @ beginning Element");
00698         ptr->SetTextL(KMsgInsertedBeg);
00699         aPtrFlat.InsertL(0,ptr);
00700 
00701                                 // Show number of elements
00702         _LIT(KCommonFormat5,"Count()=%d");
00703         console->Printf(KCommonFormat5,aPtrFlat.Count());
00704 
00705                                 // Show each element
00706         PrintPtrArray(&aPtrFlat);
00707 
00708                                 //
00709                                 // Delete  * * * * * * * * *
00710                                 //
00711         _LIT(KMsgDelete,"\n\n--->Delete");
00712         console->Printf(KMsgDelete);
00713         console->Printf(KMsgPressAnyKey);
00714         console->Getch();
00715 
00716                                 // Delete the last two elements from the array BUT
00717                                 // first we must get a reference to those elements
00718                                 // (pointers to CElement objects) otherwise
00719                                 // the CElement objects are orphaned.
00720                                 //
00721                                 // Here, we destroy those CElement objects.
00722                                 //
00723                                 // There are two alternative ways of indexing into
00724                                 // the array, using either At() or the [] operator
00725 
00726                                 // NOTE that the code below could be compressed to:
00727                                 //
00728                                 // delete (*ptrflat)[aPtrFlat.Count()-1];
00729                                 // delete (*ptrflat)[aPtrFlat.Count()-2];
00730                                 // aPtrFlat.Delete(aPtrFlat.Count()-2,2);
00731 
00732         TInt index = -1;
00733         index = aPtrFlat.Count();
00734 
00735         ptr = aPtrFlat[--index];
00736         aPtrFlat.Delete(index);
00737         delete ptr;
00738 
00739         ptr = aPtrFlat.At(--index);
00740         aPtrFlat.Delete(index);
00741         delete ptr;
00742 
00743                                 // Show number of elements
00744         console->Printf(KCommonFormat5,aPtrFlat.Count());
00745 
00746                                 // Show each element
00747         PrintPtrArray(&aPtrFlat);
00748 
00749                                 //
00750                                 // At & the [] operator * * * * * * * * * * * *
00751                                 //
00752         _LIT(KMsgAt,"\n\n--->At() and the operator []");
00753         console->Printf(KMsgAt);
00754         console->Printf(KMsgPressAnyKey);
00755         console->Getch();
00756 
00757                                 // Make a change to the object pointed to by the first element in the array
00758         _LIT(KMsgNewTextFirst,"New text for the first CElement");
00759         _LIT(KMsgNewTextSecond,"New text for the second CElement");
00760         aPtrFlat[0]->SetTextL(KMsgNewTextFirst);
00761         aPtrFlat.At(1)->SetTextL(KMsgNewTextSecond);
00762 
00763                                 // Show number of elements
00764         console->Printf(KCommonFormat5,aPtrFlat.Count());
00765 
00766                                 // Show each element
00767         PrintPtrArray(&aPtrFlat);
00768 
00769                                 //
00770                                 // ResetAndDestroy  * * * * * * * * *
00771                                 //
00772         _LIT(KMsgResetDestroy,"\n\n--->ResetAndDestroy");
00773         console->Printf(KMsgResetDestroy);
00774         console->Printf(KMsgPressAnyKey);
00775         console->Getch();
00776 
00777                                 // destroy all of the CElement objects and reset the
00778                                 // array.
00779         aPtrFlat.ResetAndDestroy();
00780 
00781                                 // Show number of elements
00782         console->Printf(KCommonFormat3,aPtrFlat.Count());
00783 
00784                                 // Show each element
00785         PrintPtrArray(&aPtrFlat);
00786 
00787         console->Printf(KMsgNewLine);
00788         }
00789 
00795 void DemoTKeyDerivedL(CArrayFixFlat<TIntPtrElement>& aFixFlat)
00796         {
00797         TIntPtrElement theIntPtrElement;
00798 
00799         // Create pointers to integers.
00800         TInt* ptrInt1 = new(ELeave)TInt(10);
00801         CleanupStack::PushL(ptrInt1);
00802         TInt* ptrInt2 = new(ELeave)TInt(0);
00803         CleanupStack::PushL(ptrInt2);
00804         TInt* ptrInt3 = new(ELeave)TInt(5);
00805         CleanupStack::PushL(ptrInt3);
00806 
00807         // Create an array of pointers.
00808         TInt* ptrInt[3] = {ptrInt1,ptrInt2,ptrInt3};
00809 
00810         // Append elements into the ptrElementArray array.
00811         TInt value;
00812         for (value = 0; value < 3; value++)
00813                 {
00814                 theIntPtrElement.iData.Format(KFormat1,value);
00815                 theIntPtrElement.SetIntPtr(ptrInt[value]);
00816                 aFixFlat.AppendL(theIntPtrElement);
00817                 }
00818 
00819         // Print the contents of the array.
00820         PrintFixIntPtrArray(aFixFlat);
00821 
00822                                 // Define the key to find an element in the array.
00823                                 // Use the TKey derived class, TKeyTIntPtr to do this.
00824         TKeyTIntPtr keyIntPtr(_FOFF(TIntPtrElement,iIntPtr),ECmpTInt);
00825 
00826         _LIT(KMsgFind,"\n--->Find");
00827         console->Printf(KMsgFind);
00828         console->Printf(KMsgPressAnyKey);
00829         console->Getch();
00830 
00831         // Initialise to -1.
00832         // Its value will be set to 0 if the Search is successful.
00833         TInt index = -1;
00834 
00835         // Perform the search.
00836         TInt res = aFixFlat.Find        (
00837                                                                 theIntPtrElement, // The object whose
00838                                                                 // key is used for comparison.
00839                                                                 keyIntPtr, // The key object that
00840                                                                 // defines the property of the key.
00841                                                                 index
00842                                                                 );
00843 
00844         if(res == 0)
00845                 {
00846                 // The index variable holds the index of the element found.
00847                 _LIT(KElementFound,"\nElement found at index: %d\n");
00848                 console->Printf(KElementFound,index);
00849                 }
00850 
00851         else
00852                 {
00853                 // The element is not found.
00854                 _LIT(KElementNotFound,"\nElement not found");
00855                 console->Printf(KElementNotFound);
00856                 }
00857 
00858         CleanupStack::PopAndDestroy(3, ptrInt1); // ptrInt3, ptrInt2 and ptrInt1.
00859         }
00860 
00865 void DemoRArrayL(RArray<CArrayElement>& aArray)
00866         {
00867         CArrayElement arrayElement;
00868 
00869                         //
00870                         // AppendL  * * * * * * * * *
00871                         //
00872         _LIT(KMsgAppend,"\n\n--->AppendL");
00873         console->Printf(KMsgAppend);
00874 
00875         console->Printf(KMsgPressAnyKey);
00876         console->Getch();
00877 
00878         TInt value;
00879         for (value = 0; value < 3; value++)
00880                 {
00881                 // Formats and copies text into this descriptor, replacing any existing data.
00882                 arrayElement.iData.Format(KFormat1,value);
00883                 aArray.AppendL(arrayElement); // leaves with one of the system wide error codes, if the operation fails.
00884                 }
00885 
00886                                 // Show number of elements
00887         _LIT(KCommonFormat3,"\nCount()=%d");
00888         console->Printf(KCommonFormat3,aArray.Count());
00889 
00890                                 // Show each element
00891         TInt forix;
00892         _LIT(KCommonFormat4,"\n%S");
00893         for (forix = 0; forix < aArray.Count(); forix++)
00894                 {
00895                 console->Printf(KCommonFormat4,&(aArray)[forix].iData);
00896                 }
00897 
00898                         //
00899                         // InsertL  * * * * * * * * *
00900                         //
00901         _LIT(KMsgInsert,"\n\n--->InsertL");
00902         console->Printf(KMsgInsert);
00903         console->Printf(KMsgPressAnyKey);
00904         console->Getch();
00905 
00906                                 // Inserts objects into the array at a specified position.
00907                                 // It increases the size of the dynamic array.
00908                                 // This function leaves with one of the system error codes, if the operation fails.
00909 
00910                                 // Insert arrayElements
00911                                 // ... at the beginning,
00912         _LIT(KMsgBEG,"BEG");
00913         arrayElement.iData = KMsgBEG;
00914         aArray.InsertL(arrayElement, 0);
00915 
00916                                 // ... near the middle,
00917         _LIT(KMsgMID,"MID");
00918         arrayElement.iData = KMsgMID;
00919         aArray.InsertL(arrayElement, 2);
00920 
00921                                 // ... at the end.
00922                                 // This is the same as using the AppendL() function
00923         _LIT(KMsgEND,"END");
00924         arrayElement.iData = KMsgEND;
00925         aArray.InsertL(arrayElement,aArray.Count());
00926 
00927                                 // Show number of elements
00928         _LIT(KCommonFormat5,"Count()=%d");
00929         console->Printf(KCommonFormat5,aArray.Count());
00930 
00931                         //
00932                         // Access elements  * * * * * * * * *
00933                         //
00934         _LIT(KMsgAccess,"\n\n--->Access");
00935         console->Printf(KMsgAccess);
00936         console->Printf(KMsgPressAnyKey);
00937         console->Getch();
00938 
00939                                 // Show each element
00940         for (forix = 0; forix < aArray.Count(); forix++)
00941                 console->Printf(KCommonFormat4,&(aArray)[forix].iData);
00942 
00943                         //
00944                         // Delete  * * * * * * * * *
00945                         //
00946         _LIT(KMsgDelete,"\n\n--->Delete");
00947         console->Printf(KMsgDelete);
00948         console->Printf(KMsgPressAnyKey);
00949         console->Getch();
00950 
00951         TInt index2;
00952         index2 = aArray.Count();
00953 
00954                         // Removes the objects at a specified position from the array.
00955                         // It shrinks the array.
00956 
00957                         // Delete the last element
00958         arrayElement = (aArray)[--index2];
00959         aArray.Remove(index2);
00960 
00961                     // Show number of elements
00962         console->Printf(KCommonFormat5,aArray.Count());
00963 
00964                         // Show each element
00965         for (forix = 0; forix < aArray.Count(); forix++)
00966                 console->Printf(KCommonFormat4,&(aArray)[forix].iData);
00967 
00968                         //
00969                         // Find  * * * * * * * * *
00970                         //
00971         _LIT(KMsgFind,"\n\n--->Find in various ways");
00972         console->Printf(KMsgFind);
00973         console->Printf(KMsgPressAnyKey);
00974         console->Getch();
00975 
00976         TInt pos =0;
00977 
00978                         // Find a particular element in different ways.
00979         arrayElement = (aArray)[--index2];
00980 
00981         _LIT(KCommonFormat6,"\nElement %S is found at position %d");
00982 
00983         // Finds the first object in the array which matches the specified object using a sequential search and a matching algorithm.
00984         pos=aArray.Find(arrayElement, TIdentityRelation<CArrayElement>());
00985                         console->Printf(KCommonFormat6, &(aArray)[pos].iData, pos);
00986 
00987         // Finds the last object in the array which matches the specified object using a sequential search and a matching algorithm.
00988         pos=aArray.FindReverse(arrayElement, TIdentityRelation<CArrayElement>());
00989                         console->Printf(KCommonFormat6, &(aArray)[pos].iData, pos);
00990 
00991         // Finds the object in the array which matches the specified object using a binary search technique.It assumes that objects are in signed key order
00992         pos=aArray.FindInSignedKeyOrder(arrayElement);
00993                                 console->Printf(KCommonFormat6,&(aArray)[pos].iData, pos);
00994 
00995         //      Finds the object in the array which matches the specified object using a binary search technique. It assumes that objects are in unsigned key order.
00996         pos=aArray.FindInUnsignedKeyOrder(arrayElement);
00997                                 console->Printf(KCommonFormat6,&(aArray)[pos].iData, pos);
00998 
00999         // Finds the object in the array which matches the specified object using a binary search technique.In case of more than one matching element, finds last
01000         // match as specified in the second parameter.It assumes that objects are in signed key order
01001         pos=aArray.SpecificFindInSignedKeyOrder(arrayElement, EArrayFindMode_Last);
01002                         console->Printf(KCommonFormat6,&(aArray)[pos].iData, pos);
01003 
01004         // Finds the object in the array which matches the specified object using a binary search technique.In case of more than one matching element, finds last
01005         // match as specified in the second parameter.It assumes that objects are in unsigned key order
01006         pos=aArray.SpecificFindInUnsignedKeyOrder(arrayElement, EArrayFindMode_Last);
01007                                 console->Printf(KCommonFormat6,&(aArray)[pos].iData, pos);
01008 
01009                         // Closes the array and frees all memory allocated to the array
01010         aArray.Close();
01011         }
01012 
01013 void DemoRArraySort()
01014         {
01015                         //
01016                         // Sort  * * * * * * * * *
01017                         //
01018         _LIT(KSortFind,"\n\n--->Sort");
01019         console->Printf(KSortFind);
01020         console->Printf(KMsgPressAnyKey);
01021         console->Getch();
01022 
01023         TLinearOrder<TInt64> Int64Order(Order64Bit);
01024         TInt count=10;
01025 
01026                         // Construct two arrays of 64-bit signed integer type objects.
01027                         // Append random objects to first array.
01028                         // Sort all objects of first array in the second array
01029 
01030         RArray<TInt64> firstArray(count);
01031     RArray<TInt64> secondArray(count);
01032 
01033     for (TInt j=0; j<count; j++)
01034                 {
01035         TInt64 x=(TInt64)(((TUint)Math::Random() %count));
01036 
01037         firstArray.Append(x);
01038         secondArray.InsertInOrderAllowRepeats(x,Int64Order);
01039                 }
01040 
01041         // Show the initial array elements of first array.
01042         _LIT(KInitialArrayElements,"\nInitial array elements are:");
01043         console->Printf(KInitialArrayElements);
01044 
01045         _LIT(KCommonFormat7," %d");
01046         TInt forix;
01047         for (forix = 0; forix < count; forix++)
01048                 console->Printf(KCommonFormat7,firstArray[forix]);
01049 
01050         // Sorts the objects within the array using the specified TLinearOrder
01051         secondArray.Sort(Int64Order);
01052 
01053         // Show sorted array elements of second array
01054         _LIT(KSortedArrayElements,"\nSorted array elements are:");
01055         console->Printf(KSortedArrayElements);
01056         for (forix = 0; forix < count; forix++)
01057                 console->Printf(KCommonFormat7,secondArray[forix]);
01058 
01059         console->Getch();
01060         console->Printf(KMsgNewLine);
01061 
01062                 // Closes both the arrays and frees all the memory allocated to these arrays
01063     firstArray.Close();
01064         secondArray.Close();
01065         }
01066 
01067 void DemoRPointerArrayL(RPointerArray<CElement>& aPointerArray)
01068         {
01069         CElement*  elementptr;
01070 
01071                         //
01072                         // AppendL  * * * * * * * * *
01073                         //
01074         _LIT(KMsgAppend,"\n\n--->AppendL");
01075         console->Printf(KMsgAppend);
01076         console->Printf(KMsgPressAnyKey);
01077         console->Getch();
01078 
01079                         // Append the first element.
01080         elementptr = new (ELeave) CElement;
01081         _LIT(KMsgFirstElement,"First Element");
01082         elementptr->SetTextL(KMsgFirstElement);
01083         aPointerArray.AppendL(elementptr);
01084 
01085                         // Append the second element.
01086         elementptr = new (ELeave) CElement;
01087         _LIT(KMsgSecondElement,"Second Element");
01088         elementptr->SetTextL(KMsgSecondElement);
01089         aPointerArray.AppendL(elementptr);
01090 
01091                         // Append the third element.
01092         elementptr = new (ELeave) CElement;
01093         _LIT(KMsgThirdElement,"Third Element");
01094         elementptr->SetTextL(KMsgThirdElement);
01095         aPointerArray.AppendL(elementptr);
01096 
01097                                 // Show number of elements
01098         _LIT(KCommonFormat3,"\nCount()=%d");
01099         console->Printf(KCommonFormat3,aPointerArray.Count());
01100 
01101                                 // Show each element
01102         _LIT(KCommonFormat4,"\n%S");
01103         TInt forix;
01104         for (forix = 0; forix < aPointerArray.Count(); forix++)
01105                 {
01106                 console->Printf(KCommonFormat4,aPointerArray[forix]->iBuf);
01107                 }
01108 
01109                         //
01110                         // InsertL  * * * * * * * * *
01111                         //
01112         _LIT(KMsgInsert,"\n\n--->InsertL");
01113         console->Printf(KMsgInsert);
01114         console->Printf(KMsgPressAnyKey);
01115         console->Getch();
01116 
01117                                 // Inserts objects into the array at a specified position.
01118                                 // It increases the size of the dynamic array.
01119 
01120                                 // Insert an element at the beginning
01121                                 // of the array
01122         elementptr = new (ELeave) CElement;
01123         _LIT(KMsgInsertedBeg,"Inserted @ beginning Element");
01124         elementptr->SetTextL(KMsgInsertedBeg);
01125         aPointerArray.InsertL(elementptr, 0);
01126 
01127                                 // Show number of elements
01128         _LIT(KCommonFormat5,"Count()=%d");
01129         console->Printf(KCommonFormat5,aPointerArray.Count());
01130 
01131                         //
01132                         // Access  * * * * * * * * *
01133                         //
01134                         // Show each element
01135         for (forix = 0; forix < aPointerArray.Count(); forix++)
01136                 {
01137                 console->Printf(KCommonFormat4,aPointerArray[forix]->iBuf);
01138                 }
01139 
01140                         //
01141                         // Delete  * * * * * * * * *
01142                         //
01143         _LIT(KMsgDelete,"\n\n--->Delete");
01144         console->Printf(KMsgDelete);
01145         console->Printf(KMsgPressAnyKey);
01146         console->Getch();
01147 
01148                         // Delete the last elements from the array BUT
01149                         // first we must get a reference to the element
01150                         // (pointers to CElement objects) otherwise
01151                         // the CElement objects are orphaned.
01152                         //
01153                         // Here, we destroy the CElement object. It shrinks the array.
01154                         //
01155                         // Use [] operator for indexing in to the array.
01156         TInt index1;
01157         index1 = aPointerArray.Count();
01158 
01159         elementptr = aPointerArray[--index1];
01160         aPointerArray.Remove(index1);
01161         delete elementptr;
01162 
01163                                 // Show number of elements
01164         console->Printf(KCommonFormat5,aPointerArray.Count());
01165 
01166                                 // Show each element
01167         for (forix = 0; forix < aPointerArray.Count(); forix++)
01168                 {
01169                 console->Printf(KCommonFormat4,aPointerArray[forix]->iBuf);
01170                 }
01171 
01172                         //
01173                         // Find  * * * * * * * * *
01174                         //
01175         _LIT(KMsgFind,"\n--->Find");
01176         console->Printf(KMsgFind);
01177         console->Printf(KMsgPressAnyKey);
01178         console->Getch();
01179 
01180         elementptr = aPointerArray[index1-2];
01181 
01182         _LIT(KCommonFormat8,"\nElement found at position %d");
01183 
01184         // Finds the first object pointer in the array which matches the specified object pointer, using a sequential search.
01185         TInt result=aPointerArray.Find(elementptr);
01186                 console->Printf(KCommonFormat8, result);
01187 
01188         // Finds the last object pointer in the array which matches the specified object pointer, using a sequential search.
01189         result=aPointerArray.FindReverse(elementptr);
01190                 console->Printf(KCommonFormat8, result);
01191 
01192         // Finds the object pointer in the array that matches the specified object pointer, using a binary search technique.
01193         // It assumes that object pointers in the array are in address order.
01194         result=aPointerArray.FindInAddressOrder(elementptr);
01195                 console->Printf(KCommonFormat8, result);
01196 
01197                         //
01198                         // Sort  * * * * * * * * *
01199                         //
01200         _LIT(KSortFind,"\n\n--->Sort");
01201         console->Printf(KSortFind);
01202         console->Printf(KMsgPressAnyKey);
01203         console->Getch();
01204 
01205                         // ResetAndDestroy  * * * * * * * * *
01206                         //
01207         _LIT(KMsgResetDestroy,"\n\n--->ResetAndDestroy");
01208         console->Printf(KMsgResetDestroy);
01209         console->Printf(KMsgPressAnyKey);
01210         console->Getch();
01211 
01212                                 // Empties the array and deletes the referenced objects.
01213                                 // It frees all memory allocated to the array and resets the internal state so that it is ready to be reused.
01214         aPointerArray.ResetAndDestroy();
01215         }
01216 
01217 void DemoSortRPointerArray()
01218         {
01219                         // Construct two arrays of TAny pointer objects.
01220                         // Append random objects to first pointer array.
01221                         // Sort all objects of first array in the second pointer array.
01222         RPointerArray<TAny> firstpointerarray;
01223         RPointerArray<TAny> secondpointerarray;
01224 
01225         TInt count = 10;
01226         for (TInt i=0; i<count; i++)
01227                 {
01228                 TAny* x=(TAny*)(((TUint)Math::Random() %count));
01229                 firstpointerarray.Append(x);
01230                 secondpointerarray.InsertInAddressOrderAllowRepeats(x);
01231                 }
01232 
01233         // Show initial array elements of first array
01234         _LIT(KInitialArrayElements,"\nInitial array elements are:");
01235         console->Printf(KInitialArrayElements);
01236 
01237         _LIT(KCommonFormat7,"Array Contents %d \n");
01238         TInt forix;
01239         for (forix = 0; forix < count; forix++)
01240                 {
01241                 console->Printf(KCommonFormat7,firstpointerarray[forix]);
01242                 }
01243 
01244         // Sorts the object pointers within the array into address order
01245         secondpointerarray.SortIntoAddressOrder();
01246 
01247         // Show sorted array elements of second array
01248         _LIT(KSortedArrayElements,"\nSorted array elements are:");
01249         console->Printf(KSortedArrayElements);
01250         for (forix = 0; forix < count; forix++)
01251                 {
01252                 console->Printf(KCommonFormat7,secondpointerarray[forix]);
01253                 }
01254 
01255         // Closes the arrays and frees all memory allocated to it
01256         firstpointerarray.Close();
01257         secondpointerarray.Close();
01258         }
01259 // Do the example
01260 LOCAL_C void doExampleL()
01261         {
01262         //************************************************************
01263         // Demonstrate a general fixed length array using
01264         // a flat buffer
01265         //************************************************************
01266 
01267         CArrayFixFlat<TElement>* fixflat;
01268                                 // Construct array of TElement objects where the iData
01269                                 // data member of each element contains "X0", "X1", etc
01270                                 // Uses the AppendL() function to add the members
01271                                 // and the [] operator to access elements.
01272         fixflat = new (ELeave) CArrayFixFlat<TElement>(3);
01273         CleanupStack::PushL(fixflat);
01274 
01275                                 // Demonstrate the CArrayFixFlat array.
01276         DemoFlatArrayL(*fixflat);
01277 
01278                                 // Delete the fixflat array.
01279         CleanupStack::PopAndDestroy(fixflat); // fixflat
01280         //************************************************************
01281         // Demonstrate an array of pointers to CBase
01282         // derived objects. Uses the specialised array CArrayPtrFlat
01283         //************************************************************
01284         _LIT(KMsgArrayOfPointers,"\nARRAYS OF POINTERS (to CBase derived objects)\n");
01285         console->Printf(KMsgArrayOfPointers);
01286 
01287         CArrayPtrFlat<CElement>* ptrflat;
01288 
01289                                 // Construct an array of four CElement objects each
01290                                 // containing the text "First" "second" etc
01291                                 // Uses the AppendL() function to add the members
01292                                 // and the [] operator to access elements.
01293         ptrflat = new (ELeave) CArrayPtrFlat<CElement>(16);
01294         CleanupStack::PushL(ptrflat);
01295 
01296                                 // Demonstrate the CArrayPtrFlat array.
01297         DemoPtrArrayL(*ptrflat);
01298 
01299                                 // Delete the array.
01300         CleanupStack::PopAndDestroy(ptrflat); // ptrflat
01301 
01302         //************************************************************
01303         // Demonstrate Variable and Packed arrays
01304         // Use TKey to sort these arrays
01305         //************************************************************
01306         _LIT(KMsgVarArray,"\n\nVAR ARRAY\n\n");
01307         console->Printf(KMsgVarArray);
01308 
01309                                 // Create a Var array.
01310         CArrayVarFlat<TText>* varflat = new (ELeave) CArrayVarFlat<TText>(3);
01311         CleanupStack::PushL(varflat);
01312 
01313                                 // Demonstrate the CArrayVarFlat array.
01314         DemoVarArraySortL(*varflat);
01315 
01316                                 // Delete the array.
01317         CleanupStack::PopAndDestroy(varflat); // varflat
01318 
01319         _LIT(KMsgPakArray,"\n\nPAK ARRAY\n\n");
01320         console->Printf(KMsgPakArray);
01321 
01322                                 // Create a TKeyArrayPak array.
01323         CArrayPakFlat<TText>* pakflat = new (ELeave) CArrayPakFlat<TText>(3);
01324         CleanupStack::PushL(pakflat);
01325 
01326                                 // Demonstrate the CArrayPakFlat array.
01327         DemoPakArrayFindL(*pakflat);
01328 
01329                                 // Delete the array.
01330         CleanupStack::PopAndDestroy(pakflat);  // pakflat,
01331 
01332         //************************************************************
01333         // Demonstrate the CArrayFix::Find() function using the
01334         // TKey derived class TKeyTIntPtr.
01335         //************************************************************
01336         _LIT(KMsgTKeyDerived,"\nTKEY DERIVED CLASS\n");
01337         console->Printf(KMsgPressAnyKey);
01338         console->Getch();
01339 
01340         console->Printf(KMsgTKeyDerived);
01341         CArrayFixFlat<TIntPtrElement>* ptrElementArray;
01342                                         // Construct array of TIntPtrElement objects where the iData
01343                                         // data member of each element contains "X0", "X1", etc
01344                                         // Uses the AppendL() function to add the members
01345                                         // and the [] operator to access elements.
01346         ptrElementArray = new (ELeave) CArrayFixFlat<TIntPtrElement>(3);
01347         CleanupStack::PushL(ptrElementArray);
01348 
01349                                         // Demonstrate the use of TKey derived class.
01350         DemoTKeyDerivedL(*ptrElementArray);
01351 
01352         CleanupStack::PopAndDestroy(ptrElementArray); // ptrElementArray,
01353 
01354         //*****************************************************************
01355         // Demonstrate an array of type RArray for elements of the same size
01356         //*****************************************************************
01357 
01358         _LIT(KMsgForRArray,"\n*******Arrays of fixed length objects******");
01359         console->Printf(KMsgForRArray);
01360 
01361         RArray<CArrayElement>* array;
01362 
01363                                 // Construct array of CArrayElement objects where the iData
01364                                 // data member of each element contains "X0", "X1", etc.
01365                                 // The elements of the array are instances of a class,
01366                                 // so this is a template class. Refer to the definition of
01367                                 // element CArrayElement above.
01368                                 // Uses the AppendL() function to add the members
01369                                 // and the [] operator to access elements.
01370     array = new (ELeave) RArray<CArrayElement>(16);     // Initialise the array granularity to 16
01371         CleanupStack::PushL(array);
01372                                 // Demonstrate the use of RArray.
01373     DemoRArrayL(*array);
01374 
01375         CleanupStack::PopAndDestroy(array);
01376                                 // Demonstrates sorting of the RArray.
01377         DemoRArraySort();
01378 
01379 
01380         //******************************************************************************
01381         // Demonstrate a simple and efficient array of pointers to objects RPointerArray
01382         //******************************************************************************
01383 
01384         _LIT(KMsgForRPointerArray,"\n*******Array of pointers to objects******");
01385         console->Printf(KMsgForRPointerArray);
01386 
01387         RPointerArray<CElement>* ptrarray;
01388 
01389                                 // Construct an array of four CElement objects each
01390                                 // containing the text "First", "Second" etc
01391                                 // Uses the AppendL() function to add the members
01392                                 // and the [] operator to access elements.
01393                                 // It leaves with one of the system wide error codes, if the operation fails.
01394     ptrarray = new (ELeave) RPointerArray<CElement>(16); // Initialise the array granularity to 16
01395     CleanupStack::PushL(array);
01396                                 // Demonstrate the use of RPointerArray.
01397     DemoRPointerArrayL(*ptrarray);
01398         CleanupStack::PopAndDestroy(ptrarray);
01399                                 // Demonstrates sorting of the RPointerArray.
01400         DemoSortRPointerArray();
01401         }
01402 

Generated by  doxygen 1.6.2