Symbian3/SDK/Source/GUID-E81D72B2-BA77-5F4E-8742-3812A60A4DAC.dita
changeset 0 89d6a7a84779
child 13 48780e181b38
equal deleted inserted replaced
-1:000000000000 0:89d6a7a84779
       
     1 <?xml version="1.0" encoding="utf-8"?>
       
     2 <!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
       
     3 <!-- This component and the accompanying materials are made available under the terms of the License 
       
     4 "Eclipse Public License v1.0" which accompanies this distribution, 
       
     5 and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
       
     6 <!-- Initial Contributors:
       
     7     Nokia Corporation - initial contribution.
       
     8 Contributors: 
       
     9 -->
       
    10 <!DOCTYPE concept
       
    11   PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
       
    12 <concept id="GUID-E81D72B2-BA77-5F4E-8742-3812A60A4DAC" xml:lang="en"><title>How to
       
    13 use modifiable buffer descriptor — TBuf&lt;TInt&gt;</title><shortdesc>Modifiable buffer descriptors are useful for holding strings or
       
    14 data and providing safe ways to access and modify that data.</shortdesc><prolog><metadata><keywords/></metadata></prolog><conbody>
       
    15 <ul>
       
    16 <li id="GUID-F276E9FE-B839-54ED-9F50-8E45CB094922"><p>For text data, it is
       
    17 usual to construct a <codeph>TBuf&lt;TInt&gt;</codeph> type and allow the appropriate
       
    18 variant, either a <codeph>TBuf8&lt;TInt&gt;</codeph> or a <codeph>TBuf16&lt;TInt&gt;</codeph> to
       
    19 be selected at build time.</p> </li>
       
    20 <li id="GUID-9860DB57-62C3-5295-B00E-5F629771FED9"><p>For binary data, an
       
    21 explicit <codeph>TBuf8&lt;TInt&gt;</codeph> is used.</p> </li>
       
    22 <li id="GUID-34C95FAB-0F58-5B61-B9FC-A31588855F96"><p>It is rare to use an
       
    23 explicit <codeph>TBuf16&lt;TInt&gt;</codeph>.</p> </li>
       
    24 </ul>
       
    25 <p>Although, the following notes refer to the build independent types; they
       
    26 are equally valid for the explicit 8 bit and 16 bit types.</p>
       
    27 <section id="GUID-824524FB-98B7-4CDA-A3FD-233C7C0DDD5F"><title>Constructing
       
    28 a TBuf&lt;TInt&gt;</title> <p>A modifiable buffer descriptor can be constructed
       
    29 in a number of ways:</p> <ul>
       
    30 <li id="GUID-BCFAA769-D038-5A09-B6BF-586C93F08B0B"><p>as an empty buffer descriptor.</p> </li>
       
    31 <li id="GUID-9DE61358-D6B9-54CA-9FED-11AC29443093"><p>as an empty buffer descriptor
       
    32 but giving it a length.</p> </li>
       
    33 <li id="GUID-50FB7967-79C8-5F8C-8569-36B40D14894E"><p>by copying data from
       
    34 any other type of descriptor.</p> </li>
       
    35 <li id="GUID-26E1FDDC-F0BB-5441-8F81-EF2CFA61787A"><p>by copying data from
       
    36 another modifiable buffer descriptor of the same size.</p> </li>
       
    37 </ul> <p>The following code fragment constructs a <codeph>TBuf&lt;16&gt;</codeph> object.
       
    38 The buffer descriptor is uninitialised, i.e. it contains no data. The assignment
       
    39 operator or the <codeph>Copy()</codeph> function can be used to put data into
       
    40 the buffer descriptor after construction:</p> <codeblock id="GUID-01CA390C-ECC4-56C2-A1F6-F62C2AA75E57" xml:space="preserve">_LIT(KText,"Hello World!");
       
    41 ...
       
    42 TBuf&lt;16&gt; buf1; // length of buf1 is 0
       
    43 ...
       
    44 buf1 = KText;  // data assigned</codeblock> <p>The source descriptor is a
       
    45 literal which is converted to descriptor type.</p> <p>The following code fragment
       
    46 constructs a <codeph>TBuf&lt;16&gt;</codeph> object and sets it length to 12.
       
    47 No data is assigned into the descriptor.</p> <codeblock id="GUID-FA8A1FE8-7809-5E28-B3EB-420B405DDC32" xml:space="preserve">...
       
    48 TBuf&lt;16&gt; buf1(12); // length of buf1 is 12
       
    49 ...</codeblock> <p>The following code fragment constructs a <codeph>TBuf&lt;16&gt;</codeph> object,
       
    50 initialised with the 12 characters making up the English language phrase "Hello
       
    51 World!".</p> <codeblock id="GUID-74B4614D-4273-5F9E-8A2D-1E9A8A3960D8" xml:space="preserve">_LIT(KText,"Hello World!");
       
    52 ...
       
    53 TBuf&lt;16&gt; buf1(KText);</codeblock> <p>The following code fragment constructs
       
    54 a <codeph>TBuf&lt;16&gt;</codeph> object from another <codeph>TBuf&lt;16&gt;</codeph> object.
       
    55 This is, in effect, copy construction.</p> <codeblock id="GUID-BE55436F-FDB8-5768-947F-801D02FDA631" xml:space="preserve">_LIT(KText,"Hello World!");
       
    56 ...
       
    57 TBuf&lt;16&gt; buf1(KText);
       
    58 TBuf&lt;16&gt; buf2(buf1);   // buf2 constructed from the data in buf1</codeblock> <p>In
       
    59 both of these cases, the resulting length of the descriptor is 12.</p> <p>A
       
    60 non-modifiable buffer descriptor can also be constructed from 'C' style zero
       
    61 terminated string. However, this is rarely necessary but may make it easier
       
    62 to port legacy 'C' code.</p> </section>
       
    63 <section id="GUID-8A534041-1730-43FB-8FCD-4C5BFDAAE730"><title>Replacing data</title> <p>Data
       
    64 within a modifiable buffer descriptor can be completely replaced through the
       
    65 assignment operator or by using the <codeph>Copy()</codeph> function.</p> <codeblock id="GUID-D990C115-DD84-5E04-89DA-F483A63D03FA" xml:space="preserve">_LIT(KText,"Hello World!");
       
    66 _LIT(KNewText,"New text");
       
    67 _LIT(KReplaced,"Replaced");
       
    68 ...
       
    69 TBuf&lt;16&gt; buf1(KText);
       
    70 TBuf&lt;16&gt; buf2;
       
    71 ...
       
    72 buf2 = buf1;               // buf2 now contains "Hello World!"
       
    73 ...
       
    74 buf2 = KNewText;           // buf2 now contains "New text".
       
    75                            // the literal is converted to a descriptor
       
    76                            // type.
       
    77 buf2.Copy(KReplaced);      // buf2 content replaced using Copy()</codeblock> </section>
       
    78 <section id="GUID-E49444E0-457E-4645-A1D5-C350FF998F9A"><title>Accessing and
       
    79 changing data</title> <p>Once a modifiable buffer descriptor has been constructed,
       
    80 the functions in the base classes, <codeph>TDesC</codeph> and <codeph>TDes</codeph>,
       
    81 are available to be access and change the data.</p> <codeblock id="GUID-386035F7-1C0F-5CB3-BA5B-5E0EF18DF6F8" xml:space="preserve">_LIT(KText,"Hello World!");
       
    82 ...
       
    83 TBufC&lt;16&gt; buf1(KText);
       
    84 ...
       
    85 buf1.Length();</codeblock> <p>and</p> <codeblock id="GUID-7BCC4118-4294-5607-A92B-5DBA9E68CBEE" xml:space="preserve">_LIT(KText,"Hello World!");
       
    86 ...
       
    87 TBufC&lt;16&gt; buf1(KText);   // length is 12 
       
    88 ...
       
    89 buf1.Delete(6,6);        // length is now 6, leaving "Hello" in
       
    90                          // the buffer</codeblock> </section>
       
    91 <section id="GUID-970BCE5F-CC4D-4F60-91A9-6107975E9C3F"><title>Illegal access
       
    92 causing an exception</title> <p>The following code fragment raises a panic
       
    93 because of an attempt to assign too much data. The maximum length of the buffer
       
    94 descriptor is 16 but the length of the data to be copied is 31:</p> <codeblock id="GUID-38DC46A1-E2A0-52DD-A821-F0D6B2C45430" xml:space="preserve">_LIT(KText,"Hello World! The sun is shining");
       
    95 ...
       
    96 TBufC&lt;16&gt; buf1(KText);</codeblock> <p>The following code fragment raises
       
    97 a panic because of an attempt to delete data outside the boundary defined
       
    98 by the descriptor:</p> <codeblock id="GUID-71FB3EFC-BFBA-5072-ADD2-3471E2A08D0C" xml:space="preserve">_LIT(KText,"Hello World!");
       
    99 ...
       
   100 TBufC&lt;16&gt; buf1(KText);
       
   101 buf1.Delete(99,1);</codeblock> </section>
       
   102 </conbody><related-links>
       
   103 <link>
       
   104 <desc><xref href="GUID-7CB11EAD-260E-551A-85F1-FEAC975FE722.dita">Literal Descriptors</xref></desc>
       
   105 </link>
       
   106 </related-links></concept>