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