ActiveObject 活动对像练习

ActiveObject 活动对像练习

活动对像都继承自 CActive 类,则活动对像调度器管理,对这个活动对像并不太了解,下面说说主要的:

  1. 活动对像从 CActive 类继承,CActive 已有 iStatus 及 SetActive()
  2. 一定要将活动对像添加到活动对像调度器中才可以得到执行 CActiveScheduler::Add()  方法添加
  3. 一般都是调用Start() 去执行这个活动对像,在 Start() 中一定要调用 SetActive 方法
  4. 重写 RunL 方法及DoCancel方法,并且在 RunL 方法中代码要少同时执行简洁
  5. IsActive 是判断一个活动对像是否处在活动状态

目前对活动对像并没有深入了解, DoCancel只知道是用到取消的,但还没有实际去操作,下面只是把练习的代码放出来,到时
再回头深入了解活动对像

代码
//  Include Files  
#include "ActiveLx.h"
#include 
<e32base.h>
#include 
<e32std.h>
#include 
<e32cons.h>            // Console

//  Constants
_LIT(KTextConsoleTitle, "Console");
_LIT(KTextFailed, 
" failed, leave code = %d");
_LIT(KTextPressAnyKey, 
" [press any key]\n");

//  Global Variables
LOCAL_D CConsoleBase* console;  // write all messages to this
static TInt word;
static TInt num;
class CActiveTimer;
class CWord:public CBase
{
public:
 
static CWord* NewL(TInt aMaxLength);
 
static CWord* NewLC(TInt aMaxLength);
private:
 CWord(TInt aMaxLength);
 
void ConstructL(TInt aMaxLength);
public:
 TInt WordNumCountL(TChar aChar);
 
void OutputWord();
 TInt WordNum();
 TInt Length();
 TInt MaxLength();
 
~CWord();
private:
 TInt iLenght;
 TInt iMaxLength;
 HBufC
* iHBuffC;
};

class CAOExample:public CActive
{
public:
 
static CAOExample* NewL(CConsoleBase* aConsole);
 
static CAOExample* NewLC(CConsoleBase* aConsole);
private:
 CAOExample();
 
void ConstructL(CConsoleBase* aConsole);
public:
 
void Start();
 
void PrintWord();
 
void PrintWordNum();
 
void KeyPressInput(TChar aChar);
 
~CAOExample();
private:
 
void RunL();
 
void Cancel();
 
void DoCancel();
private:
 CConsoleBase
* iConsole;
 CWord
* iCWord;
};
CAOExample
* CAOExample::NewL( CConsoleBase* aConsole )
{
 CAOExample
* self = CAOExample::NewLC(aConsole);
 CleanupStack::Pop();
 
return self;
 
}
CAOExample
* CAOExample::NewLC( CConsoleBase* aConsole )
{
 CAOExample
* self = new(ELeave)CAOExample();
 CleanupStack::PushL(self);
 self
->ConstructL(aConsole);
 
return self;
}
CAOExample::CAOExample():CActive(EPriorityStandard)
{
 
}
void CAOExample::ConstructL( CConsoleBase* aConsole )
{
 iConsole 
= aConsole;
 
this->iCWord = CWord::NewL(2);
 CActiveScheduler::Add(
this);
}
void CAOExample::Start()
{
 iConsole
->Read(iStatus);
 SetActive();
}
void CAOExample::PrintWord()
{
 iCWord
->OutputWord();
}
void CAOExample::PrintWordNum()
{
 _LIT(KFormat1,
"You input word num:%d\n");
 iConsole
->Printf(KFormat1,iCWord->WordNum());
 iConsole
->Printf(_L("pleae input new word"));
}
void CAOExample::KeyPressInput( TChar aChar )
{
 iCWord
->WordNumCountL(aChar);
 
if (iCWord->Length()==iCWord->MaxLength())
 {
  PrintWord();
  
 }
 
if (aChar == EKeyEnter)
 {
  PrintWordNum();
  word
= 0;
  num 
= 0;
 }
 Start();
}
CAOExample::
~CAOExample()
{
 delete iCWord;
 iCWord 
= NULL;
 iConsole 
= NULL;
}
void CAOExample::RunL()
{
 KeyPressInput(TChar(iConsole
->KeyCode()));
}
void CAOExample::Cancel()
{
 DoCancel();
}
void CAOExample::DoCancel()
{
 iConsole
->ReadCancel();
}
 

CWord
* CWord::NewL( TInt aMaxLength )
{
 CWord
* self = CWord::NewLC(aMaxLength);
 CleanupStack::Pop();
 
return self;
}
CWord
* CWord::NewLC( TInt aMaxLength )
{
 CWord
* self=new (ELeave)CWord(aMaxLength);
 CleanupStack::PushL(self);
 self
->ConstructL(aMaxLength);
 
return self;
}
CWord::CWord( TInt aMaxLength )
{
 iMaxLength  
= aMaxLength;
 iLenght 
= 0;
}
void CWord::ConstructL( TInt aMaxLength )
{
 iHBuffC 
= HBufC::NewL(aMaxLength);
}
void CWord::OutputWord()
{
 console
->Printf(iHBuffC->Des());
 iHBuffC
->Des().Zero();
 iLenght 
= 0;
}
TInt CWord::WordNum()
{
 
return num;
}
TInt CWord::Length()
{
 
return iLenght;
}

TInt CWord::MaxLength()
{
 
return iMaxLength;
}
CWord::
~CWord()
{
 delete iHBuffC;
 iHBuffC 
=NULL;
 iLenght 
= 0;
 iMaxLength 
= 0;
}
TInt CWord::WordNumCountL( TChar aChar )
{
 TPtr ptr 
= iHBuffC->Des();
 
if (iLenght<iMaxLength)
 {
  ptr.Append(aChar);
  
if (!('A'<=aChar && aChar<='Z' || 'a'<=aChar && aChar<='z'))
  {
   word 
= 0;
  } 
else
  {
   
if (word==0)
   {
    word
=1;
    
++num;
   }
  }
  iLenght
++;
  
return 1;
 } 
else
  
return 0;
}
 
class CActiveTimer:public CTimer
{
public:
 
static CActiveTimer* NewL(CConsoleBase* aConsole);
 
static CActiveTimer* NewLC(CConsoleBase* aConsole);
 
~CActiveTimer();
private:
 CActiveTimer();
 
void ConstructL(CConsoleBase* aConsole);
public:
 
void Start();
private:
 
void RunL();
 
void Cancel();
 
void DoCancel();
 
void Stop();
private:
 CConsoleBase
* iConsole;
 TTime iTime;
 RTimer iTimer;
 TBool iStop;
};
 
CActiveTimer
* CActiveTimer::NewL( CConsoleBase* aConsole )
{
 CActiveTimer
* self = CActiveTimer::NewLC(aConsole);
 CleanupStack::Pop();
 
return self;
}
CActiveTimer
* CActiveTimer::NewLC( CConsoleBase* aConsole )
{
 CActiveTimer
* self= new(ELeave)CActiveTimer();
 CleanupStack::PushL(self);
 self
->ConstructL(aConsole);
 
return self;
 
}
CActiveTimer::
~CActiveTimer()
{
 iConsole  
= NULL;
 iTimer.Close();
}
CActiveTimer::CActiveTimer():CTimer(CActive::EPriorityStandard)
{
 iStop 
= FALSE;
}
 
void CActiveTimer::Start()
{
 
if (IsActive())
  
return;
 iTimer.After(iStatus,
1000);
 SetActive();
}
void CActiveTimer::Stop()
{
 iStop 
= TRUE;
}
void CActiveTimer::DoCancel()
{
 CTimer::DoCancel();
}
void CActiveTimer::Cancel()
{
 DoCancel();
}
void CActiveTimer::RunL()
{
 
if(iStop==FALSE)
 {
  iTime.HomeTime();
  iConsole
->ClearScreen();
  TDay day 
= iTime.DayNoInWeek();
  TDayName DName;
  DName.Set(day);
  DName.ZeroTerminate();
  TDateTime dt;
  dt 
= iTime.DateTime();
  _LIT(KTime,
"today is:%d-%d-%d\n%s\n%d:%d:%d time \n");
  iConsole
->Printf(KTime,dt.Year(),dt.Month()+1,dt.Day()+1,DName.Ptr(),
   dt.Hour(),dt.Minute(),dt.Second());
  Start();
 } 
else
  CActiveScheduler::Stop();
}
void CActiveTimer::ConstructL( CConsoleBase* aConsole )
{
 CTimer::ConstructL();
 iConsole 
= aConsole;
 iTimer.CreateLocal();
 CActiveScheduler::Add(
this);
}

//  Local Functions
LOCAL_C void MainL(const TDesC& aArgs)
    {
    
//
    
// add your program code here, example code below
    
//
    
//console->Write(_L("Hello, world!\n"));
/*

 _LIT(KFormat1,"length=%d\nsize=%d\ncontent=%S");
 HBufC* aTest = HBufC::NewL(12);
 
 CleanupStack::PushL(aTest);
 _LIT(KTest,"qijianzhou");
 TPtr ptr = aTest->Des();
 TBuf<12> aBuf(KTest);
 // 测试刚创建完后长度及大小
 console->Printf(KFormat1,aTest->Length(),aTest->Size(),&ptr);
 console->Getch();
 ptr.Append(aBuf);
 
 console->Printf(KFormat1,aTest->Length(),aTest->Size(),&ptr);
 console->Getch();
 ptr.Zero();
    console->Printf(KFormat1,aTest->Length(),aTest->Size(),&ptr);
 CleanupStack::PopAndDestroy();
*/
    console
->Printf(_L("\nCommand line args: \"%S\"\n"), &aArgs);
    }

LOCAL_C 
void DoStartL()
    {
    
// Create active scheduler (to run active objects)
    CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
    CleanupStack::PushL(scheduler);
    CActiveScheduler::Install(scheduler);
    
// Call main function with command line
   
// TBuf<256> cmdLine;
   
// RProcess().CommandLine(cmdLine);
   
// MainL(cmdLine);
 CAOExample* ao = CAOExample::NewLC(console);
 ao
->Start();
 CActiveTimer
* at = CActiveTimer::NewLC(console);
// at->Start();
 scheduler->Start();
 CleanupStack::PopAndDestroy(
2);
    
// Delete active scheduler
    CleanupStack::PopAndDestroy(scheduler);
    }

//  Global Functions
GLDEF_C TInt E32Main()
    {
    
// Create cleanup stack
    __UHEAP_MARK;
    CTrapCleanup
* cleanup = CTrapCleanup::New();
    
// Create output console
    TRAPD(createError, console = Console::NewL(KTextConsoleTitle, TSize(KConsFullScreen,KConsFullScreen)));
    
if (createError)
        
return createError;
    
// Run application code inside TRAP harness, wait keypress when terminated
    TRAPD(mainError, DoStartL());
    
if (mainError)
        console
->Printf(KTextFailed, mainError);
    console
->Printf(KTextPressAnyKey);
    console
->Getch();
    
    delete console;
    delete cleanup;
    __UHEAP_MARKEND;
    
return KErrNone;
    }

// End of file

 

 

原文地址:https://www.cnblogs.com/zziss/p/1650232.html