examples/ForumNokia/DescriptorExample/src/DeclaringDescriptors.cpp

00001 /*
00002  * Copyright © 2008 Nokia Corporation.
00003  */
00004 
00005 #include <e32std.h>
00006 #include <e32base.h>
00007 #include "DescriptorExamples.h"
00008 #include "StringRenderer.h"
00009 
00010 // -----------------------------------------------------------------------------
00011 // This example method is documented in header file "DescriptorExamples.h"
00012 // -----------------------------------------------------------------------------
00013 void CDescriptorExamples::ToStack()
00014     {
00015     TPtr output( iViewer->GetViewBuffer() );
00016     
00017     //Note, You should use the _LIT macro instead of _L
00018     RenderHeader( _L( "ToStack" ), output );
00019 
00020     // --------------------
00021     // declare constant buffers to stack as automatic variable:
00022     // length=0, maxsize=20, data="".
00023     // Note that these automatic variables consumes stack until the
00024     // method ends...
00025     TBufC<20> tbufc20( _L("Hey!") );
00026     RenderVariableFormatted( tbufc20.Des(), output, KRenderCharacteristics );
00027 
00028     // --------------------
00029     // declare modifiable buffers to stack as automatic variable:
00030     // length=0, maxsize=20, data=""
00031     TBuf<20> tbuf20;
00032     RenderVariableFormatted( tbuf20, output, KRenderCharacteristics );
00033 
00034     // --------------------
00035     // declare a non modifying descriptor pointer that points to array in
00036     // the middle of a temporary buffer: length=3, data="loWo"
00037     // Example is declared to own code block so automatic variables are
00038     // deleted when block ends.
00039         {
00040         TBuf<20> tmpBuf(_L("HelloWorld"));
00041         const TText* arrayInBuf = tmpBuf.Ptr(); // point to first char in buffer
00042         TPtrC partOfBuffer( arrayInBuf + 3, 4 );
00043         RenderVariableFormatted( tmpBuf, output,
00044                                  KRenderCharacteristics );
00045         RenderVariableFormatted( partOfBuffer, output,
00046                                  KRenderCharacteristics );
00047         } // tmpBuf, arrayInBuf and partOfBuffer do no more exist in stack
00048 
00049     // --------------------
00050     // declare modifying buffer pointer pointing to memory
00051     // in another buffer: length=4, maxSize=5, data=loWo
00052         {
00053         // tmpBuf below can't be edited with its methods but we can alter data
00054         // through modifiable buffer pointer created few lines later
00055         TBufC<20> tmpBuf(_L("HelloWorld"));
00056         // Set arrayInBuf to point to first character in buffer. Has to be
00057         // cast to non const pointer since TPtr expects such (of course)
00058         TText* arrayInBuf = (TText*)tmpBuf.Ptr();
00059         TPtr modifyingPointer( arrayInBuf + 3, 4, 5 );
00060         RenderVariableFormatted( modifyingPointer, output,
00061                                  KRenderCharacteristics );
00062         }
00063 
00064     // --------------------
00065     // declare modifying buffer pointing to a buffer descriptor - instead of
00066     // of its buffer directrly. The only way to do a such is to call method
00067     // Des() of non modifiable descriptors buffers TBufC and HBuf.
00068     // contents of tmpBuf after modification: length=6, maxlength=20,
00069     // data="HWorld"
00070         {
00071         TBufC<20> originalBuf( _L("HelloWorld") );
00072         TPtr tmpBufPtr = originalBuf.Des();
00073         // modify the original buffer. Length in both descriptors
00074         // is updated!
00075         tmpBufPtr.Delete(1, 4);
00076         RenderVariableFormatted( originalBuf.Des(), output,
00077                                  KRenderCharacteristics );
00078         }
00079     iViewer->UpdateView();
00080     }
00081 
00082 // -----------------------------------------------------------------------------
00083 // This example method is documented in header file "DescriptorExamples.h"
00084 // -----------------------------------------------------------------------------
00085 void CDescriptorExamples::ToHeapL()
00086     {
00087     TPtr output( iViewer->GetViewBuffer() );
00088     RenderHeader( _L( "ToHeap" ), output );
00089 
00090     // --------------------
00091     // Allocate heap buffer with buffer size of 512. Factory
00092     // method LC allocates the object, pushes cleanup item to
00093     // cleanup stack and returns pointer to allocated object.
00094     //
00095     HBufC *hbufc = HBufC::NewLC( 512 );
00096     RenderVariableFormatted( hbufc->Des(), output, KRenderCharacteristics );
00097 
00098     // --------------------
00099     // Allocate TBuf descriptor with the same characteristics
00100     // like example above. Let also the constructor to initialize
00101     // the buffer with text.
00102     TBuf<512> *tbuf = new (ELeave) TBuf<512>( _L("Hello World!") );
00103     // Objects allocated from heap have to be deleted explicitly. Quite good
00104     // practise is to push every item allocated from heap to cleanup stack and
00105     // let the last line in method delete all allocated objects with one method
00106     // call (CleanupStack::PopAndDestroy(count)). If method leaves at the middle
00107     // of execution, the trap harness deletes automatically all objects pushed
00108     // to cleanup stack and no memory leaks occur.
00109     CleanupStack::PushL( tbuf );
00110     RenderVariableFormatted( *tbuf, output, KRenderCharacteristics );
00111 
00112     // we do no more need allocated objects, so lets remove them from cleanup
00113     // stack with one method call.
00114     CleanupStack::PopAndDestroy(2); // hbufc & tbuf
00115     iViewer->UpdateView();
00116     }
00117 
00118 // -----------------------------------------------------------------------------
00119 // This example method is documented in header file "DescriptorExamples.h"
00120 // -----------------------------------------------------------------------------
00121 void CDescriptorExamples::Literals()
00122     {
00123     TPtr output( iViewer->GetViewBuffer() );
00124     RenderHeader( _L( "Literals" ), output );
00125 
00126     // --------------------
00127     // Declare variable KLit1 and assing some text to it. _LIT macro
00128     // evaluates so that static constant variable is declared and
00129     // initialized with given string data. Since static and constant,
00130     // it is compiled as part of program binary.
00131     _LIT( KLit1, "String declared with macro _LIT" );
00132     RenderVariableFormatted( KLit1, output, KRenderCharacteristics );
00133 
00134     // --------------------
00135     // Literals can be also created with macro _L. However, it isn't
00136     // so efficient (refer book "Symbian OS C++ for Mobile Phones"
00137     // for details). Using it in test code is acceptable.
00138     TPtrC L1 = _L( "String declared with macro _L" );
00139     RenderVariableFormatted( L1, output, KRenderCharacteristics );
00140 
00141     // --------------------
00142     // Let's declare a literal that contains euro sign (0x20AC).Since it
00143     // doesn't exist in many 8 bit encodings , we declare it as 'e' when
00144     // building non-unicode build
00145 #ifdef _UNICODE
00146     // note that in unicode literal the unicode chars can be declared
00147     // by passing the unicode number of the character as hexadecimal number
00148     _LIT( KLitEuro, "I won 166\x20AC from lottery!" );
00149 #else
00150     _LIT( KLitEuro, "I won 166e from lottery!" );
00151 #endif
00152     RenderVariableFormatted( KLitEuro, output, KRenderCharacteristics );
00153     iViewer->UpdateView();
00154     }

Generated by  doxygen 1.6.2