00001 /**************************************************************************** 00002 ** 00003 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). 00004 ** All rights reserved. 00005 ** Contact: Nokia Corporation 00006 ** 00007 ** 00008 ** $QT_BEGIN_LICENSE:BSD$ 00009 ** You may use this file under the terms of the BSD license as follows: 00010 ** 00011 ** "Redistribution and use in source and binary forms, with or without 00012 ** modification, are permitted provided that the following conditions are 00013 ** met: 00014 ** * Redistributions of source code must retain the above copyright 00015 ** notice, this list of conditions and the following disclaimer. 00016 ** * Redistributions in binary form must reproduce the above copyright 00017 ** notice, this list of conditions and the following disclaimer in 00018 ** the documentation and/or other materials provided with the 00019 ** distribution. 00020 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor 00021 ** the names of its contributors may be used to endorse or promote 00022 ** products derived from this software without specific prior written 00023 ** permission. 00024 ** 00025 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00026 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00027 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00028 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00029 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00030 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00031 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00032 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00033 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00034 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00035 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." 00036 ** $QT_END_LICENSE$ 00037 ** 00038 ****************************************************************************/ 00039 00040 00041 00042 // System Includes. 00043 #include <e32std.h> 00044 #include <e32base.h> 00045 00046 // User Includes. 00047 #include "stringrenderer_symbian.h" 00048 #include "descriptorexamples_symbian.h" 00049 00055 void CDescriptorExamples::OnStack() 00056 { 00057 TPtr output( iViewer->GetViewBuffer() ); 00058 00059 //Note, You should use the _LIT macro instead of _L 00060 _LIT(KStack, "OnStack"); 00061 RenderHeader(KStack, output ); 00062 00063 // -------------------- 00064 // declare constant buffers to stack as automatic variable: 00065 // length=0, maxsize=20, data="". 00066 // Note that these automatic variables consumes stack until the 00067 // method ends... 00068 _LIT(KString1, "Hey!"); 00069 TBufC<20> tbufc20(KString1); 00070 RenderVariableFormatted( tbufc20.Des(), output, KRenderCharacteristics ); 00071 00072 // -------------------- 00073 // declare modifiable buffers to stack as automatic variable: 00074 // length=0, maxsize=20, data="" 00075 TBuf<20> tbuf20; 00076 RenderVariableFormatted( tbuf20, output, KRenderCharacteristics ); 00077 _LIT(KString2, "HelloWorld"); 00078 00079 // -------------------- 00080 // declare a non modifying descriptor pointer that points to array in 00081 // the middle of a temporary buffer: length=3, data="loWo" 00082 // Example is declared to own code block so automatic variables are 00083 // deleted when block ends. 00084 { 00085 TBuf<20> tmpBuf(KString2); 00086 const TText* arrayInBuf = tmpBuf.Ptr(); // point to first char in buffer 00087 TPtrC partOfBuffer( arrayInBuf + 3, 4 ); 00088 RenderVariableFormatted( tmpBuf, output, 00089 KRenderCharacteristics ); 00090 RenderVariableFormatted( partOfBuffer, output, 00091 KRenderCharacteristics ); 00092 } // tmpBuf, arrayInBuf and partOfBuffer do no more exist in stack 00093 00094 // -------------------- 00095 // declare modifying buffer pointer pointing to memory 00096 // in another buffer: length=4, maxSize=5, data=loWo 00097 { 00098 // tmpBuf below can't be edited with its methods but we can alter data 00099 // through modifiable buffer pointer created few lines later 00100 TBufC<20> tmpBuf(KString2); 00101 // Set arrayInBuf to point to first character in buffer. Has to be 00102 // cast to non const pointer since TPtr expects such (of course) 00103 TText* arrayInBuf = (TText*)tmpBuf.Ptr(); 00104 TPtr modifyingPointer( arrayInBuf + 3, 4, 5 ); 00105 RenderVariableFormatted( modifyingPointer, output, 00106 KRenderCharacteristics ); 00107 } 00108 00109 // -------------------- 00110 // declare modifying buffer pointing to a buffer descriptor - instead of 00111 // of its buffer directrly. The only way to do a such is to call method 00112 // Des() of non modifiable descriptors buffers TBufC and HBuf. 00113 // contents of tmpBuf after modification: length=6, maxlength=20, 00114 // data="HWorld" 00115 { 00116 TBufC<20> originalBuf(KString2); 00117 TPtr tmpBufPtr = originalBuf.Des(); 00118 // modify the original buffer. Length in both descriptors 00119 // is updated! 00120 tmpBufPtr.Delete(1, 4); 00121 RenderVariableFormatted( originalBuf.Des(), output, 00122 KRenderCharacteristics ); 00123 } 00124 iViewer->UpdateView(); 00125 } 00126 00132 void CDescriptorExamples::OnHeapL() 00133 { 00134 _LIT(KHeap, "OnHeap"); 00135 TPtr output( iViewer->GetViewBuffer() ); 00136 RenderHeader(KHeap, output ); 00137 00138 // -------------------- 00139 // Allocate heap buffer with buffer size of 512. Factory 00140 // method LC allocates the object, pushes cleanup item to 00141 // cleanup stack and returns pointer to allocated object. 00142 // 00143 HBufC *hbufc = HBufC::NewLC( 512 ); 00144 RenderVariableFormatted( hbufc->Des(), output, KRenderCharacteristics ); 00145 00146 // -------------------- 00147 // Allocate TBuf descriptor with the same characteristics 00148 // like example above. Let also the constructor to initialize 00149 // the buffer with text. 00150 _LIT(KString3,"Hello World!"); 00151 TBuf<512> *tbuf = new (ELeave) TBuf<512>(KString3); 00152 // Objects allocated from heap have to be deleted explicitly. Quite good 00153 // practise is to push every item allocated from heap to cleanup stack and 00154 // let the last line in method delete all allocated objects with one method 00155 // call (CleanupStack::PopAndDestroy(count)). If method leaves at the middle 00156 // of execution, the trap harness deletes automatically all objects pushed 00157 // to cleanup stack and no memory leaks occur. 00158 CleanupStack::PushL( tbuf ); 00159 RenderVariableFormatted( *tbuf, output, KRenderCharacteristics ); 00160 00161 // we do no more need allocated objects, so lets remove them from cleanup 00162 // stack with one method call. 00163 CleanupStack::PopAndDestroy(2); // hbufc & tbuf 00164 iViewer->UpdateView(); 00165 } 00166 00172 void CDescriptorExamples::Literals() 00173 { 00174 _LIT(KLiteral,"Literals"); 00175 TPtr output( iViewer->GetViewBuffer() ); 00176 RenderHeader(KLiteral, output); 00177 00178 // -------------------- 00179 // Declare variable KLit1 and assing some text to it. _LIT macro 00180 // evaluates so that static constant variable is declared and 00181 // initialized with given string data. Since static and constant, 00182 // it is compiled as part of program binary. 00183 _LIT( KLit1, "String declared with macro _LIT" ); 00184 RenderVariableFormatted( KLit1, output, KRenderCharacteristics ); 00185 00186 // -------------------- 00187 // Literals can be also created with macro _L. However, it isn't 00188 // so efficient (refer book "Symbian OS C++ for Mobile Phones" 00189 // for details). Using it in test code is acceptable. 00190 TPtrC L1 = _L( "String declared with macro _L" ); 00191 RenderVariableFormatted( L1, output, KRenderCharacteristics ); 00192 00193 // -------------------- 00194 // Let's declare a literal that contains euro sign (0x20AC).Since it 00195 // doesn't exist in many 8 bit encodings , we declare it as 'e' when 00196 // building non-unicode build 00197 #ifdef _UNICODE 00198 // note that in unicode literal the unicode chars can be declared 00199 // by passing the unicode number of the character as hexadecimal number 00200 _LIT( KLitEuro, "I won 166\x20AC from lottery!" ); 00201 #else 00202 _LIT( KLitEuro, "I won 166e from lottery!" ); 00203 #endif 00204 RenderVariableFormatted( KLitEuro, output, KRenderCharacteristics ); 00205 iViewer->UpdateView(); 00206 }