examples/Base/BufsAndStrings/Desc/HeapBuffer/HeapBuffer.cpp

00001 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // This component and the accompanying materials are made available
00004 // under the terms of "Eclipse Public License v1.0"
00005 // which accompanies this distribution, and is available
00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 // Examples to demonstrate the basic ideas of heap buffer descriptors.
00015 //
00016 
00017 #include "CommonFramework.h"
00018 
00019 //
00020 // Common literal text
00021 //
00022 _LIT(KTxtHelloWorld,"Hello World!");
00023 _LIT(KTxtHelloWorldMorning,"Hello World! Morning");
00024 _LIT(KTxtAndHi," & Hi");
00025 
00026 //
00027 // Common Format strings
00028 //
00029 _LIT(KCommonFormat1,"Size()=%d;\nMaxLength()=%d\n");
00030 _LIT(KCommonFormat2,"Ptr()=%x; Length()=%d; ");
00031 _LIT(KCommonFormat5,"Ptr()=%x; Length()=%d; Size()=%d\n");
00032 
00033 
00034 // Do the example
00035 LOCAL_C void doExampleL()
00036     {
00037         
00038                                 // An HBufC is always constructed on the heap
00039                                 // using the static New(), NewL() or NewLC()
00040         HBufC* buf;
00041 
00042                                 // Construct an HBufC.
00043                                 // This descriptor can hold up to 15 data 
00044                                 // items.
00045                                 // The current length is zero
00046         buf  = HBufC::NewL(15);
00047         CleanupStack::PushL(buf);
00048 
00049                                 // Note, we could replace the above two lines of code
00050                                 // with the single line: buf  = HBufC::NewLC(15);
00051 
00052 
00053                                 // Show 1. Content
00054                                 //      2. address of descriptor
00055                                 //      3. address of descriptor data area
00056                                 //      4. length of descriptor
00057                                 //      5. size of descriptor
00058                                 // The address of the descriptor data area
00059                                 // is offset 4 from the start of the 
00060                                 // descriptor itself.
00061         _LIT(KFormat10,"\"%S\"; descriptor at %x; ");
00062         console->Printf(KFormat10,buf,buf);
00063         console->Printf(KCommonFormat5,buf->Ptr(),buf->Length(),buf->Size());
00064                                                  
00065                                 // Set up some data into the HBufC 
00066         *buf = KTxtHelloWorld;          // "Hello World!"
00067 
00068                                 // Show 1. Content
00069                                 //      2. length of descriptor
00070                                 //      3. size of descriptor
00071         _LIT(KFormat9,"\"%S\"; ");
00072         console->Printf(KFormat9,buf);
00073         _LIT(KFormat8,"Length()=%d; Size()=%d\n");
00074         console->Printf(KFormat8,buf->Length(),buf->Size());
00075 
00076                                 // Now want to replace the text with:
00077                                 // "Hello World! Morning"
00078                                 // Resulting length would be > 15
00079                                 // So, reallocate the HBufc first
00080                                 // to make it bigger
00081 
00082         buf = buf->ReAllocL(20);
00083 
00084                                 // Assign the new text.
00085         *buf = KTxtHelloWorldMorning;   // "Hello World! Morning"
00086 
00087                                 // Show it
00088         _LIT(KFormat7,"\n\"%S\"; \n(1st realloc') desc'at %x; ");
00089         console->Printf(KFormat7,buf,buf);
00090         console->Printf(KCommonFormat5,buf->Ptr(),buf->Length(),buf->Size());                   
00091 
00092                                 // buf may point to the same area as before.
00093                                 // In general, this is not the case so DO 
00094                                 // NOT ASSUME. 
00095         
00096         buf = buf->ReAllocL(22);
00097         _LIT(KFormat6,"\n\"%S\"; \n(2nd realloc') desc'at %x; ");
00098         console->Printf(KFormat6,buf,buf);
00099         console->Printf(KCommonFormat5,buf->Ptr(),buf->Length(),buf->Size());
00100         
00101                                 // The Des() function returns a TPtr to the
00102                                 // HBufC.
00103                                 // The HBufC data can be changed through 
00104                                 // the TPtr.
00105                                 // The maximum length of the TPtr is 
00106                                 // determined from the size of the cell 
00107                                 // allocated to the data area of the HBufC.
00108                                 // In this example, the value has been rounded 
00109                                 // up to 24.
00110         TPtr ptr = buf->Des();
00111 
00112         _LIT(KFormat11,"TPtr descriptor at %x; ");
00113         console->Printf(KFormat11,&ptr);
00114         console->Printf(KCommonFormat2,ptr.Ptr(),ptr.Length());
00115         console->Printf(KCommonFormat1,ptr.Size(),ptr.MaxLength());
00116 
00117                                 // Now change the HBufC data through
00118                                 // the TPtr. This is OK provided the length
00119                                 // of the changed data does not exceed the 
00120                                 // maximum length.
00121                                 //
00122                                 // The following change deletes the last 
00123                                 // nine characters and then appends 
00124                                 // the characters " & Hi".
00125                                 //
00126                                 // Note that the length of both the HBufC 
00127                                 // and the TPtr reflect the changed data.
00128         
00129         ptr.Delete((ptr.Length()-9),9);
00130     ptr.Append(KTxtAndHi); // " & Hi"
00131         
00132                                 // Look at it from HBufC's viewpoint
00133         _LIT(KFormat4,"\n\"%S\";\nHBufC descriptor at %x; ");
00134         console->Printf(KFormat4,buf,buf);
00135 
00136         console->Printf(KCommonFormat5,buf->Ptr(),buf->Length(),buf->Size());
00137 
00138                                 // Look at it from TPtr's viewpoint
00139         _LIT(KFormat3,"\"%S\"; \nTPtr  descriptor at %x; ");
00140         console->Printf(KFormat3,&ptr,&ptr);
00141         console->Printf(KCommonFormat2,ptr.Ptr(),ptr.Length());
00142         console->Printf(KCommonFormat1,ptr.Size(),ptr.MaxLength());
00143                                 // Pop the HBufC off the cleanup stack
00144                                 // and destroy it (i.e. the HBufC)
00145         CleanupStack::PopAndDestroy();
00146     }
00147 
00148 
00149         
00150         

Generated on Thu Jan 21 10:32:55 2010 for TB10.1 Example Applications by  doxygen 1.5.3