How to use the modifiable pointer descriptor — TPtr

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

How to use the modifiable pointer descriptor — TPtr

Modifiable pointer descriptors are useful for referencing strings or data which can be accessed and changed.

  • For text data, it is usual to construct a TPtr type and allow the appropriate variant, either a TPtr or a TPtrC to be selected at build time.

  • For binary data, an explicit TPtr8 is used.

  • It is rare to use an explicit TPtr16.


Constructing a TPtr

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

  • another modifiable pointer descriptor.

  • from a non-modifiable buffer descriptor using the Des() function

  • from an explicit pointer into memory and specifying a maximum length.

  • from an explicit pointer into memory and specifying the length of the data and a maximum length.

The following code fragment constructs a TPtr to represent the data already represented by another TPtr:

TPtr ptr1;
...
TPtr ptr2(ptr1);
...

The following code fragment constructs a TPtr for a non-modifiable buffer descriptor, a TBufC<TInt>, using the Des() function. Data that would normally be unmodifiable through the TBufC<TInt> can be changed through the TPtr.

The source are literals which are converted to descriptor type.

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

Define a TText area initialised to contain the string "Have a nice day":

The following code fragments show the construction of a TPtr using a pointer and specifying a length and a maximum length. This technique is not commonly used. Literals are a much better way of defining string constants.

TText str[16] = {'H', 'a', 'v', 'e', ' ', 'a',
    ' ', 'n', 'i', 'c', 'e',
    ' ', 'd', 'a', 'y', '/0'};

TPtr ptr(&str[0],15,16);

The descriptor ptr represents the data in str and is constructed to have a current length of 15 (the length of the text, excluding the zero terminator) and a maximum length of 16 (the actual length of str). Once the descriptor has been constructed, it has no further use for the zero terminator.

TPtr ptr(&str[0],15,16);

[Top]


Replacing data through the TPtr

Data can be completely replaced using the assignment operator or the Copy() function.:

_LIT(KText,"Hi there");
...
ptr = KText;
...
ptr.Copy(KText);

Note the use of the _LIT macro to define the source string. A literal is converted into a descriptor type.

The length of ptr is now 8 but the maximum length remains unchanged. The size depends on the build variant. In a non-Unicode build, this is 8 but in a Unicode build, this becomes 16 (two bytes for every character).

[Top]


Changing the length of data

The length of the data represented can be changed.

_LIT(KText,"Hi there");
...
ptr = KText;
ptr.SetLength(2);
ptr.Zero();

For example, after ptr.SetLength(2), the descriptor represents the string "Hi". The length can even be set to zero so that after ptr.Zero(), the descriptor represents no data. Nevertheless, the maximum length remains unchanged.


 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值