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

[Top]


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.

[Top]


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.

[Top]


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();


年纪轻轻记忆力就衰退得厉害,我还有救吗?

转自:http://36kr.com/p/5097688.html 如果我有一小时的时间来背诵一段材料,我会花 55 分钟搞清楚为什么要背它、它能给我带来什么价值,然后再用 5 分钟时间去记住它。 ...
  • shellching
  • shellching
  • 2017年10月18日 17:40
  • 350

Keil C51编译及连接技术(覆盖技术OVERLAY)

http://www.cnblogs.com/heiyue/p/3271421.html Keil C51编译及连接技术
  • chungle2011
  • chungle2011
  • 2014年12月20日 21:22
  • 4872

Go实战--golang中使用号称全世界最快的JSON解析器json-iterator(json-iterator/go)

生命不止,继续go go go!!!号外号外,差不一条广告,我的博客六月到十一月UV的地域分布: golang中的使用json一点也不陌生,golang为我们提供了官方的encoding/json:...
  • wangshubo1989
  • wangshubo1989
  • 2017年12月04日 14:57
  • 11988

eclipse使用技巧

eclipse使用技巧  出处:作者:永恒の_☆ 地址:http://blog.csdn.net/chenghui0317/article/details/9417949 1、如何新建java...
  • JamesLi6
  • JamesLi6
  • 2016年08月16日 21:40
  • 778

DMA描述符及映射

1.DMA通道 DMA(Direct Memory Access)通道建立在设备和RAM之间,DMAC(DMA Controler)与设备I/O控制器相互作用共同实现数据传送。 在PC中,DMA控制...
  • phunxm
  • phunxm
  • 2013年07月24日 16:40
  • 17115

Matplotlib 中文用户指南 4.7 使用 LaTeX 渲染文本

使用 LaTeX 渲染文本 原文:Text rendering With LaTeX 译者:飞龙 协议:CC BY-NC-SA 4.0 Matplotlib 可以选择使用 La...
  • wizardforcel
  • wizardforcel
  • 2017年01月30日 11:47
  • 542

Stack_Queue 一个数组实现三个栈 @CareerCup

原文: Describe how you could use a single array to implement three stacks. 译文: 你如何只用一个数组实现三个栈...
  • hellobinfeng
  • hellobinfeng
  • 2014年03月01日 11:35
  • 1738

快来看看Google出品的Protocol Buffer,别只会用Json和XML了

前言 习惯用 Json、XML 数据存储格式的你们,相信大多都没听过Protocol BufferProtocol Buffer 其实 是 Google出品的一种轻量 & 高效的结构化数据存储格...
  • aptentity
  • aptentity
  • 2017年03月29日 23:20
  • 746

关于Swing中使用Substance皮肤和界面刷新问题一角

问题一、Swing使用Substance皮肤教程 请点链接刚开始遇到的问题基本都有http://insubstantial.github.io/insubstantial/substance/doc...
  • lidelin10
  • lidelin10
  • 2017年12月30日 14:52
  • 69

在WinForms中使用MD皮肤

Material Skin for .Net WinForms TODO: 本文由 赤石俊哉 翻译整理,您可以随意用于学习交流使用,转载之前请注明出处。 参考 GitHub - Ignac...
  • Toshiya14
  • Toshiya14
  • 2016年07月23日 15:10
  • 1005
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:How to use the non-modifiable buffer descriptor — TBufC
举报原因:
原因补充:

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