Symbian 学习日志(六. 实用类)

A.     尽量使用基类做为参数;

B.      使用 const TDesC& 当不要修改时,使用引用当要修改时,使用TDes&

C.从HBufCTDes

HBufC* CPoem::DoGetLineL(TInt aLineNumber)

{// Code omitted for clarity. Allocates and returns a heap buffer

      // containing the text of aLineNumber (leaves if aLineNumber is out of range)

}

 

void CPoem::GetLineL(TInt aLineNumber, TDes& aDes)

{

      HBufC* line = DoGetLineL(aLineNumber);

      CleanupStack::PushL(line);

      // Is the descriptor large enough (4 bytes or more) to return an

      // integer representing the length of data required?

      if (aDes.MaxLength() < line->Length())

      {

           if (aDes.MaxLength() >= sizeof(TInt))

           {// Writes the length required (TPckg is described later)

                 TPckg<TInt> length(line->Length());

                 aDes.Copy(length);

           }

           // Leave & indicate that the current length is too short

           User::Leave(KErrOverflow); // Leaves are described in Chapter 2

      }

      else

      {

           aDes.Copy(*line);

           CleanupStack::PopAndDestroy(line);

      }

}

 

D.和 Stream 的数据交互

 

使用函数的方法:

// Writes the contents of iHeapBuffer to a writable stream

void CSampleClass::ExternalizeL(RWriteStream& aStream) const

{

      // Write the descriptor’s length

      aStream.WriteUint32L(iHeapBuffer->Length());

      // Write the descriptor’s data

      aStream.WriteL(*iHeapBuffer, iHeapBuffer->Length());

}

// Instantiates iHeapBuffer by reading the contents of the stream

void CSomeClass::InternalizeL(RReadStream& aStream)

{

      TInt size=aStream.ReadUint32L(); // Read the descriptor’s length

      iHeapBuffer = HBufC::NewL(size); // Allocate iHeapBuffer

      // Create a modifiable descriptor over iHeapBuffer

      TPtr ptr(iHeapBuffer->Des());

      // Read the descriptor data into iHeapBuffer

      aStream.ReadL(ptr,size);

}

 

直接使用 << >>

class TSomeClass

{

      ... // Omitted for clarity

private:

      TBuf<12> iBuffer;

      ...

};

void TSomeClass::ExternalizeL(RWriteStream& aStream)

{

      aStream << iBuffer;

      ...

}

void TSomeClass::InternalizeL(RReadStream& aStream)

{

      aStream >> iBuffer;

      ...

}

 

DTFileName 的使用

 

定义

const TInt KMaxFileName=0x100; // = 256 (decimal)

typedef TBuf<KMaxFileName> TFileName;

 

使用一个TFileName可能会产生500+字节的开销,所以应该尽量不要在栈上使用;

 

使用 TParse, TParsePtr, TParsePtrC

 

E.其它实用类

 

TLex: 用来进行数据类型转换和字符串解析

TPckg<T>: 可以对一个对像进行包装, 当相 C++ 的智能指针一样,对这个对象中的内容改变会影响到对象本身;

TPckgC<T>: 和TPckg相比,不能修包装的对象;调用会引起编译错误;

TPckgBuf<T>: 和TPckg相比,会创建一个对象的拷贝,修改不会改变原对象.

 

 

参照下图

 

 TLex

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值