How to use the non-modifiable buffer descriptor — TBufC

原创 2007年10月09日 11:09:00

下面的内容摘自Symbian S60 3rd Edition SDK,先把英文原版贴在这里,等时间充足的时候我再把它翻译出来。How to use the non-modifiable buffer descriptor — TBufC<TInt>《如何使用不可修改的缓冲描述符TBufC<TInt>》

How to use the non-modifiable buffer descriptor — TBufC<TInt>

Non-modifiable buffer descriptors are useful for holding constant strings or data and providing safe ways to access that data.

  • For text data, it is usual to construct a TBufC<TInt> type and allow the appropriate variant, either a TBufC8<TInt> or a TBufC16<TInt> to be selected at build time.

  • For binary data, an explicit TBufC8<TInt> is used.

  • It is rare to use an explicit TBufC16<TInt>.

Data cannot be changed through this descriptor although it can be replaced using the assignment operators.

By using the Des() function to construct a TPtr/TPtr8/TPtr16 modifiable pointer descriptor for the buffer's data, it becomes possible to modify that data.

Although, the following notes refer to the build independent types; they are equally valid for the explicit 8 bit and 16 bit types.


Constructing a TBufC<TInt>

A non-modifiable buffer descriptor can be constructed in a number of ways:

  • as an empty buffer descriptor.

  • by copying data from any other type of descriptor.

  • by copying data from another non-modifiable buffer descriptor of the same size.

The following code fragment constructs a TBufC<16> object. The buffer descriptor is uninitialised, i.e. it contains no data. The assignment operator can be used to put data into the buffer descriptor after construction:

_LIT(KText,"Hello World!");
...
TBufC<16> buf1; // length of buf1 is 0
...
buf1 = KText; // data assigned

The following code fragment constructs a TBufC<16> object, initialised with the 12 characters making up the English language phrase "Hello World!".

The source descriptor is a literal which is converted to descriptor type.

_LIT(KText,"Hello World!");
...
TBufC<16> buf1(KText); // length of buf1 is 12

The following code fragment constructs a TBufC<16> object, initialised with the content of another TBufC<16> object.

_LIT(KText,"Hello World!");
...
TBufC<16> buf1(KText);
TBufC<16> buf2(buf1); // data copied from descriptor buf1
                       // length of buf2 is 12


Replacing data

Data within a non-modifiable buffer descriptor can be completely replaced by using the assignment operator:

_LIT(KText,"Hello World!");
_LIT(KNewText,"New text");
...
TBufC<16> buf1(KText);
TBufC<16> buf2;
...
buf2 = buf1; // buf2 now contains "Hello World!"
...
buf2 = KNewText; // buf2 now contains "New text".
                           // the literal is converted to a descriptor
                           // type.

To prevent data replacement, declare buf2 as const.


Constructing a modifiable pointer descriptor to change the data

The data contained in non-modifiable buffer descriptor TBufC<TInt> can be changed by constructing a TPtr modifiable pointer descriptor using the Des() member function and then changing the data through that TPtr.

The maximum length of the TPtr is the value of the TBufC<TInt> template parameter.

The following code fragment shows data being changed:

_LIT(KText,"Hello World!");
_LIT(KExtraText," & Hi");
...
TBufC<16> buf1(KText);
...
TPtr ptr = buf1.Des();
...
ptr.Delete((ptr.Length()-1),1);
ptr.Append(KExtraText);
...

This deletes the last character in buf1 and adds the characters " & Hi" so that buf1 now contains the text "Hello World & Hi" and its length is 16. Note that the length of both buf1 and ptr change to reflect the data that they now both represent.

Note that any attempt to append more data raises a panic.


Accessing data

Once a non-modifiable buffer descriptor has been constructed, the functions in the base class, TDesC, are available to access the data.

_LIT(KText,"Hello World!");
...
TBufC<16> buf1(KText);
...

buf1.Length();


How to use the non-modifiable pointer descriptor — TPtrC

下面的内容摘自Symbian S60 3rd Edition SDK,先把英文原版贴在这里,等时间充足的时候我再把它翻译出来。How to use the non-modifiable pointer...
  • wave_1102
  • wave_1102
  • 2007-10-09 11:22:00
  • 648

RxJava的学习之变换操作符—buffer

基本操作格式: 1. buffer(count) 2. buffer(count, skip) 3. buffer(bufferClosingSelector和buffer(boundary) ...
  • hjjdehao
  • hjjdehao
  • 2017-04-11 09:30:04
  • 717

OpenGL Frame Buffer Object

 IntroductionIn the last OpenGL Framebuffer object article we covered the basic usage of an FBO for ...
  • yangdelong
  • yangdelong
  • 2007-06-27 14:25:00
  • 2303

How to use modifiable buffer descriptor — TBuf

下面的内容摘自Symbian S60 3rd Edition SDK,先把英文原版贴在这里,等时间充足的时候我再把它翻译出来。How to use modifiable buffer descript...
  • wave_1102
  • wave_1102
  • 2007-10-09 11:16:00
  • 728

Linux maximum number of open files or file descriptors

There are two levels of control on Linux maximum number of open files (or file descriptors), system ...
  • Shelsea_x
  • Shelsea_x
  • 2013-08-28 08:17:43
  • 2118

How to use the heap descriptor — HBufC

下面的内容摘自Symbian S60 3rd Edition SDK,先把英文原版贴在这里,等时间充足的时候我再把它翻译出来。How to use the heap descriptor — HBuf...
  • wave_1102
  • wave_1102
  • 2007-10-09 11:38:00
  • 651

How-To Guide for Descriptors

How-To Guide for Descriptors Author:  Raymond Hettinger Contact:  Copyright:  ...
  • dao123mao
  • dao123mao
  • 2011-10-20 11:07:05
  • 460

how to debug buffer overrun

最近开发一套基于InfiniBand的网络通讯组件,在使用InfiniBand通讯前,我使用socket来进行同步,在服务端开发中遇到这样的一个问题:A buffer overrun has occu...
  • hitheu
  • hitheu
  • 2013-05-12 11:18:57
  • 3784

Xshell无法链接本地虚拟机

本地cmd能够ping同虚拟机的IP地址,但是xshell链接时提示如下: Connecting to 10.110.31.151:22... Could not connect to...
  • KoalaY_Doctor
  • KoalaY_Doctor
  • 2016-01-05 10:34:34
  • 3911

NodeJs TypeError: First argument must be file descriptor解决方法

最近在学习NodeJs,在练习一个例子的时候发现老是报:TypeError: First argument must be file descriptor,代码如下: var fs=require(...
  • u012453843
  • u012453843
  • 2017-03-09 13:52:08
  • 1342
收藏助手
不良信息举报
您举报文章:How to use the non-modifiable buffer descriptor — TBufC
举报原因:
原因补充:

(最多只允许输入30个字)