www.pudn.com > symbianex.rar > HBufC.cpp


// HBufC.cpp 
// 
// Copyright (c) 2005 Neusoft Institute of Information Technology.Chengdu   
// All rights reserved. 
 
// written by hewei 
//   
// version 1.0 
 
// This program demostrate how to use HBufC.  
 
// NOTE: the structure of this example is different to standard E32 examples 
 
 
#include  
 
  // All messages written to this 
LOCAL_D CConsoleBase* console; 
 
// Function prototypes 
 void callExampleL(); 
 void doExampleL(); 
 
 
////////////////////////////////////////////////////////////////////////////// 
// 
// -----> CStudent (definition) 
// 
// The class is used by the example code 
// 
////////////////////////////////////////////////////////////////////////////// 
class CStudent : public CBase 
	{ 
public: 
	static CStudent* NewL(const TDesC& aName,TInt aNo,TReal aScore); 
	static CStudent* NewLC(const TDesC& aName,TInt aNo,TReal aScore); 
public : 
	HBufC* GetName(); 
	TInt GetNo(); 
	TReal GetScore(); 
	~CStudent(); 
private: 
	CStudent(TInt aNo,TReal aScore); 
	void ConstructL(const TDesC& aName); 
private : 
	TInt iNo; 
	HBufC* iName; 
	TReal iScore; 
	}; 
 
 
////////////////////////////////////////////////////////////////////////////// 
// 
// -----> CStudent (implementation) 
// 
////////////////////////////////////////////////////////////////////////////// 
CStudent* CStudent::NewL(const TDesC& aName,TInt aNo,TReal aScore)  
	{ 
	CStudent* stu=new (ELeave)CStudent(aNo,aScore); 
	stu->ConstructL(aName); 
	return stu; 
	 
} 
 
CStudent* CStudent::NewLC(const TDesC& aName,TInt aNo,TReal aScore)  
	{ 
	CStudent* stu=CStudent::NewL(aName,aNo,aScore); 
	CleanupStack::PushL(stu); 
	return stu; 
} 
CStudent::CStudent(TInt aNo,TReal aScore) 
{ 
	iNo=aNo; 
	iScore=aScore; 
} 
void CStudent::ConstructL(const TDesC& aName) 
{ 
	iName=HBufC::NewLC(aName.Length()); 
	TPtr16 ptr16=iName->Des(); 
	ptr16.Copy(aName); 
	CleanupStack::Pop(); 
	 
} 
HBufC* CStudent::GetName() 
{ 
	return iName; 
} 
TInt CStudent::GetNo() 
{ 
	return iNo; 
} 
TReal CStudent::GetScore() 
{ 
	return iScore; 
} 
CStudent::~CStudent() 
{ 
	delete iName; 
} 
////////////////////////////////////////////////////////////////////////////// 
// 
// Main function called by E32 
// 
////////////////////////////////////////////////////////////////////////////// 
GLDEF_C TInt E32Main() 
    { 
	  // Get cleanup stack 
	CTrapCleanup* cleanup=CTrapCleanup::New(); 
 
	 // Some more initialization, then do the example 
    TRAPD(error,doExampleL()); 
	 
	   
	  // callExampleL() should never leave. 
	_LIT(KMsgPanicEpoc32ex,"EPOC32EX"); 
	__ASSERT_ALWAYS(!error,User::Panic(KMsgPanicEpoc32ex,error)); 
 
  // destroy the cleanup stack 
	delete cleanup; 
	 
	  // return 
	return 0; 
    } 
 
 
////////////////////////////////////////////////////////////////////////////// 
// 
// 
// 
////////////////////////////////////////////////////////////////////////////// 
void doExampleL() 
{ 
  	_LIT(KPressAnyKey,"[Press any key...OK]"); 
	//Get Console; 
	_LIT(KMsgExampleCode,"Symbian OS Example Code"); 
	console = Console::NewL(KMsgExampleCode,TSize(KConsFullScreen,KConsFullScreen)); 
		// Put console onto the cleanup stack. 
	CleanupStack::PushL(console); 
 
	int error; 
	TRAP(error,callExampleL()); 
	//TRAPD(error,callExampleL()); 
    if(error) 
	{ 
		_LIT(KERROR,"error occured!\n"); 
		console->Printf(KERROR); 
	 
	} 
	else{ 
      _LIT(KNOLEAVE,"No Leave!\n"); 
		console->Printf(KNOLEAVE); 
	} 
	console->Printf(KPressAnyKey); 
	console->Getch(); 
		// Remove the console object from the cleanupstack 
	  // and destroy it. 	 
	CleanupStack::PopAndDestroy(); 
} 
 
void callExampleL()  
    { 
	_LIT(KName1,"hewei"); 
	_LIT(KNewLine,"\n"); 
	_LIT(KFmt1,"No=%d  "); 
	_LIT(KFmt2,"Score=%f "); 
	_LIT(KFmt3,"Name="); 
	CStudent* stu=CStudent::NewLC(KName1,1,89.8); 
	console->Printf(KFmt3); 
	console->Printf(*(stu->GetName())); 
	console->Printf(KNewLine); 
	console->Printf(KFmt1,stu->GetNo()); 
	console->Printf(KNewLine); 
	console->Printf(KFmt2,stu->GetScore()); 
	console->Printf(KNewLine); 
	CleanupStack::PopAndDestroy(); 
 
}