|
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-BD649ABC-4DBC-5E37-B80D-2BB86F94F26A" xml:lang="en"><title>How to |
|
13 use the non-modifiable buffer descriptor — TBufC<TInt></title><shortdesc>Non-modifiable buffer descriptors are useful for holding constant |
|
14 strings or data and providing safe ways to access that data.</shortdesc><prolog><metadata><keywords/></metadata></prolog><conbody> |
|
15 <ul> |
|
16 <li id="GUID-74A075D5-195D-5BB7-BC40-D683635BE6F7"><p>For text data, it is |
|
17 usual to construct a <codeph>TBufC<TInt></codeph> type and allow the appropriate |
|
18 variant, either a <codeph>TBufC8<TInt></codeph> or a <codeph>TBufC16<TInt></codeph> to |
|
19 be selected at build time.</p> </li> |
|
20 <li id="GUID-0AA8DD41-55DF-5448-A5D5-DC403E60C3A4"><p>For binary data, an |
|
21 explicit <codeph>TBufC8<TInt></codeph> is used.</p> </li> |
|
22 <li id="GUID-441D5170-8C00-5BE6-AF0C-44DB149E87F1"><p>It is rare to use an |
|
23 explicit <codeph>TBufC16<TInt></codeph>.</p> </li> |
|
24 </ul> |
|
25 <p>Data cannot be changed through this descriptor although it can be replaced |
|
26 using the assignment operators.</p> |
|
27 <p>By using the <codeph>Des()</codeph> function to construct a <codeph>TPtr/TPtr8/TPtr16</codeph> modifiable |
|
28 pointer descriptor for the buffer's data, it becomes possible to modify that |
|
29 data.</p> |
|
30 <p>Although, the following notes refer to the build independent types; they |
|
31 are equally valid for the explicit 8 bit and 16 bit types.</p> |
|
32 <section id="GUID-5591AC50-485E-413D-B3A7-BCE59F3B634D"><title>Constructing a TBufC<TInt></title> <p>A non-modifiable |
|
33 buffer descriptor can be constructed in a number of ways:</p> <ul> |
|
34 <li id="GUID-8328A45B-EB36-5C80-9B3D-969DA033B71E"><p>as an empty buffer descriptor.</p> </li> |
|
35 <li id="GUID-D0CDC7BA-A2A2-5953-8700-F43FF5FD2AE2"><p>by copying data from |
|
36 any other type of descriptor.</p> </li> |
|
37 <li id="GUID-A7CC00D3-16AB-5D2F-B25E-011D689C3BA9"><p>by copying data from |
|
38 another non-modifiable buffer descriptor of the same size.</p> </li> |
|
39 </ul> <p>The following code fragment constructs a <codeph>TBufC<16></codeph> object. |
|
40 The buffer descriptor is uninitialised, i.e. it contains no data. The assignment |
|
41 operator can be used to put data into the buffer descriptor after construction:</p> <codeblock id="GUID-BED8AC0B-4F0B-5859-994B-723F4B8F84A1" xml:space="preserve">_LIT(KText,"Hello World!"); |
|
42 ... |
|
43 TBufC<16> buf1; // length of buf1 is 0 |
|
44 ... |
|
45 buf1 = KText; // data assigned</codeblock> <p>The following code fragment |
|
46 constructs a <codeph>TBufC<16></codeph> object, initialised with the 12 |
|
47 characters making up the English language phrase "Hello World!".</p> <p>The |
|
48 source descriptor is a literal which is converted to descriptor type.</p> <codeblock id="GUID-737E0C5F-AE11-586D-B81C-0E3D1AD96414" xml:space="preserve">_LIT(KText,"Hello World!"); |
|
49 ... |
|
50 TBufC<16> buf1(KText); // length of buf1 is 12</codeblock> <p>The following |
|
51 code fragment constructs a <codeph>TBufC<16></codeph> object, initialised |
|
52 with the content of another <codeph>TBufC<16></codeph> object.</p> <codeblock id="GUID-A8A21448-DBE4-5F68-8CA0-57298E04036C" xml:space="preserve">_LIT(KText,"Hello World!"); |
|
53 ... |
|
54 TBufC<16> buf1(KText); |
|
55 TBufC<16> buf2(buf1); // data copied from descriptor buf1 |
|
56 // length of buf2 is 12</codeblock> </section> |
|
57 <section id="GUID-1C814D9C-5F53-4885-90B9-B83480CF623E"><title>Replacing data</title> <p>Data within a non-modifiable buffer |
|
58 descriptor can be completely replaced by using the assignment operator:</p> <codeblock id="GUID-C3F90B27-EB1C-5864-A80A-0D0C77BB67FC" xml:space="preserve">_LIT(KText,"Hello World!"); |
|
59 _LIT(KNewText,"New text"); |
|
60 ... |
|
61 TBufC<16> buf1(KText); |
|
62 TBufC<16> buf2; |
|
63 ... |
|
64 buf2 = buf1; // buf2 now contains "Hello World!" |
|
65 ... |
|
66 buf2 = KNewText; // buf2 now contains "New text". |
|
67 // the literal is converted to a descriptor |
|
68 // type.</codeblock> <p>To prevent data replacement, |
|
69 declare <codeph>buf2</codeph> as const.</p> </section> |
|
70 <section id="GUID-5F3BEAB3-B469-4673-A3D8-04DB697B8D0F"><title>Constructing a modifiable pointer descriptor to change the |
|
71 data</title> <p>The data contained in non-modifiable buffer descriptor <codeph>TBufC<TInt></codeph> <i>can</i> be |
|
72 changed by constructing a <codeph>TPtr</codeph> modifiable pointer descriptor |
|
73 using the<codeph>Des()</codeph> member function and then changing the data |
|
74 through that <codeph>TPtr</codeph>.</p> <p>The maximum length of the <codeph>TPtr</codeph> is |
|
75 the value of the <codeph>TBufC<TInt></codeph> template parameter.</p> <p>The |
|
76 following code fragment shows data being changed:</p> <codeblock id="GUID-02B09EBC-3E58-51B1-8D14-E388250A674A" xml:space="preserve">_LIT(KText,"Hello World!"); |
|
77 _LIT(KExtraText," & Hi"); |
|
78 ... |
|
79 TBufC<16> buf1(KText); |
|
80 ... |
|
81 TPtr ptr = buf1.Des(); |
|
82 ... |
|
83 ptr.Delete((ptr.Length()-1),1); |
|
84 ptr.Append(KExtraText); |
|
85 ...</codeblock> <p>This deletes the last character in <codeph>buf1</codeph> and |
|
86 adds the characters " & Hi" so that <codeph>buf1</codeph> now contains |
|
87 the text "Hello World & Hi" and its length is 16. Note that the length |
|
88 of both <codeph>buf1</codeph> and <codeph>ptr</codeph> change to reflect the |
|
89 data that they now both represent.</p> <p>Note that any attempt to append |
|
90 more data raises a panic.</p> </section> |
|
91 <section id="GUID-9DD4E5D1-B0A0-41BC-AE54-FC062D21173C"><title>Accessing data</title> <p>Once a non-modifiable buffer descriptor |
|
92 has been constructed, the functions in the base class, <codeph>TDesC</codeph>, |
|
93 are available to access the data.</p> <codeblock id="GUID-CE288FF9-26F7-533D-ADC4-E535035039B2" xml:space="preserve">_LIT(KText,"Hello World!"); |
|
94 ... |
|
95 TBufC<16> buf1(KText); |
|
96 ...</codeblock> <codeblock id="GUID-656150B0-C7DF-54B7-9127-6BE685EEEA83" xml:space="preserve">buf1.Length();</codeblock> </section> |
|
97 </conbody><related-links> |
|
98 <link> |
|
99 <desc><xref href="GUID-7CB11EAD-260E-551A-85F1-FEAC975FE722.dita">Literal Descriptors</xref></desc> |
|
100 </link> |
|
101 </related-links></concept> |