📄 twophase.cpp
字号:
// TwoPhase.cpp
//
// Copyright (c) 2005 NIIT ChengDu. All rights reserved.
// Example shows two phase construction.
// version 1.0
// written by hewei
// date: 10-10-2005
// NOTE: the structure of this example is different to standard E32 examples
#include <e32cons.h>
// All messages written to this
LOCAL_D CConsoleBase* console;
// Function prototypes
void callExampleL();
void doExampleL();
//////////////////////////////////////////////////////////////////////////////
//
// -----> CDate (definition)
//
// The class is used by the example code
//
//////////////////////////////////////////////////////////////////////////////
class CDate : public CBase
{
public:
static CDate* NewLC(TInt aYear,TInt aMonth,TInt aDate,CConsoleBase* aConsole);
static CDate* NewL(TInt aYear,TInt aMonth,TInt aDate,CConsoleBase* aConsole);
public :
TInt IsLeapYear();
void Print();
private:
CDate(TInt aYear,TInt aMonth,TInt aDate,CConsoleBase* aConsole);
public :
TInt iYear;
TInt iMonth;
TInt iDate;
CConsoleBase* iConsole;
};
//////////////////////////////////////////////////////////////////////////////
//
// -----> CDate (implementation)
//
//////////////////////////////////////////////////////////////////////////////
CDate* CDate::NewLC(TInt aYear,TInt aMonth,TInt aDate,CConsoleBase* aConsole)
{
CDate* date=NewL(aYear,aMonth,aDate, aConsole);
CleanupStack::PushL(date);
return date;
}
CDate* CDate::NewL(TInt aYear,TInt aMonth,TInt aDate,CConsoleBase* aConsole)
{
CDate* self=new (ELeave)CDate(aYear,aMonth,aDate, aConsole);
return self;
}
CDate::CDate(TInt aYear,TInt aMonth,TInt aDate,CConsoleBase* aConsole)
{
iYear=aYear;
iMonth=aMonth;
iDate=aDate;
iConsole=aConsole;
}
void CDate::Print()
{
_LIT(KDateFormat,"%d/%d/%d\n");
iConsole->Printf(KDateFormat,iMonth,iDate,iYear);
}
TInt CDate::IsLeapYear()
{
_LIT(KISLEAPYEAR,"%d is a leap year\n");
_LIT(KISNOTLEAPYEAR,"%d is not a leap year\n");
if(iYear%4==0&&iYear%100!=0||iYear%400==0)
{
iConsole->Printf(KISLEAPYEAR,iYear);
return 1;
}
else
{
iConsole->Printf(KISNOTLEAPYEAR,iYear);
return 0;
}
}
//////////////////////////////////////////////////////////////////////////////
//
// -----> CFullTime (definition)
//
// The class is used by the example code
//
//////////////////////////////////////////////////////////////////////////////
class CFullTime :CBase
{
public:
static CFullTime* NewL(TInt aHour,TInt aMinute,TInt aSecond,TInt aYear,TInt aMonth,TInt aDate,CConsoleBase* aConsole);
static CFullTime* NewLC(TInt aHour,TInt aMinute,TInt aSecond,TInt aYear,TInt aMonth,TInt aDate,CConsoleBase* aConsole);
private:
CFullTime(TInt aHour,TInt aMinute,TInt aSecond);
void ConstructL(TInt aYear,TInt aMonth,TInt aDate,CConsoleBase* aConsole);
public:
void Print();
~CFullTime();
private:
TInt iHour;
TInt iSecond;
TInt iMinute;
CDate* iDate;
CConsoleBase* iConsole;
};
//////////////////////////////////////////////////////////////////////////////
//
// -----> CFullTime (implement)
//
// The class is used by the example code
//
//////////////////////////////////////////////////////////////////////////////
CFullTime* CFullTime::NewL(TInt aHour,TInt aMinute,TInt aSecond,TInt aYear,TInt aMonth,TInt aDate,CConsoleBase* aConsole)
{
CFullTime* self=new (ELeave)CFullTime(aHour,aMinute,aSecond);
self->ConstructL(aYear,aMonth,aDate,aConsole);
return self;
}
CFullTime* CFullTime::NewLC(TInt aHour,TInt aMinute,TInt aSecond,TInt aYear,TInt aMonth,TInt aDate,CConsoleBase* aConsole)
{
CFullTime* self=CFullTime::NewL(aHour,aMinute,aSecond,aYear,aMonth,aDate,aConsole);
CleanupStack::PushL(self);
return self;
}
CFullTime::CFullTime(TInt aHour,TInt aMinute,TInt aSecond)
{
iHour=aHour;
iMinute=aMinute;
iSecond=aSecond;
}
void CFullTime::ConstructL(TInt aYear,TInt aMonth,TInt aDate,CConsoleBase* aConsole)
{
iDate=CDate::NewLC(aYear,aMonth,aDate,aConsole);
iConsole=aConsole;
CleanupStack::Pop(iDate);
}
void CFullTime::Print()
{
_LIT(KTimeFormat,"%d:%d:%d\n");
iDate->Print();
iConsole->Printf(KTimeFormat,iHour,iMinute,iSecond);
iDate->IsLeapYear();
}
CFullTime::~CFullTime()
{
delete iDate;
}
//////////////////////////////////////////////////////////////////////////////
//
// 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;
}
//////////////////////////////////////////////////////////////////////////////
//
//doExampleL() and CallExampleL();
//
//////////////////////////////////////////////////////////////////////////////
void doExampleL()
{
//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->Getch();
// Remove the console object from the cleanupstack
// and destroy it.
CleanupStack::PopAndDestroy();
}
void callExampleL()
{
//pointer to CFullTime object.
CFullTime* fulltime=CFullTime::NewLC(14,10,22,2005,10,10,console) ;
//print message of CFullTime.
fulltime->Print();
CleanupStack::PopAndDestroy();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -