How to use modifiable buffer descriptor — TBuf

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

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

How to use modifiable buffer descriptor — TBuf<TInt>

Modifiable buffer descriptors are useful for holding strings or data and providing safe ways to access and modify that data.

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

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

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

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 TBuf<TInt>

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

  • as an empty buffer descriptor.

  • as an empty buffer descriptor but giving it a length.

  • by copying data from any other type of descriptor.

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

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

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

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

The following code fragment constructs a TBuf<16> object and sets it length to 12. No data is assigned into the descriptor.

...
TBuf<16> buf1(12); // length of buf1 is 12
...

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

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

The following code fragment constructs a TBuf<16> object from another TBuf<16> object. This is, in effect, copy construction.

_LIT(KText,"Hello World!");
...
TBuf<16> buf1(KText);
TBuf<16> buf2(buf1); // buf2 constructed from the data in buf1

In both of these cases, the resulting length of the descriptor is 12.

A non-modifiable buffer descriptor can also be constructed from 'C' style zero terminated string. However, this is rarely necessary but may make it easier to port legacy 'C' code.


Replacing data

Data within a modifiable buffer descriptor can be completely replaced through the assignment operator or by using the Copy() function.

_LIT(KText,"Hello World!");
_LIT(KNewText,"New text");
_LIT(KReplaced,"Replaced");
...
TBuf<16> buf1(KText);
TBuf<16> buf2;
...
buf2 = buf1; // buf2 now contains "Hello World!"
...
buf2 = KNewText; // buf2 now contains "New text".
                           // the literal is converted to a descriptor
                           // type.
buf2.Copy(KReplaced); // buf2 content replaced using Copy()


Accessing and changing data

Once a modifiable buffer descriptor has been constructed, the functions in the base classes, TDesC and TDes, are available to be access and change the data.

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

and

_LIT(KText,"Hello World!");
...
TBufC<16> buf1(KText); // length is 12
...
buf1.Delete(6,6); // length is now 6, leaving "Hello" in
                         // the buffer


Illegal access causing an exception

The following code fragment raises a panic because of an attempt to assign too much data. The maximum length of the buffer descriptor is 16 but the length of the data to be copied is 31:

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

The following code fragment raises a panic because of an attempt to delete data outside the boundary defined by the descriptor:

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


 

How to use the modifiable pointer descriptor — TPtr

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

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 the non-modifiable buffer descriptor — TBufC

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

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

vi 提示 “不能修改 因为选项modifiable”是关的

使用vi的时候不能编辑,提示 “不能修改,因为选项 "modifiable"是关的 ”,输入命令 :set modifiable 回车就可以继续编辑...
  • flexprogram
  • flexprogram
  • 2011-01-24 13:13:00
  • 6047

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
  • 3902
收藏助手
不良信息举报
您举报文章:How to use modifiable buffer descriptor — TBuf
举报原因:
原因补充:

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