?? fibonacci3.cpp
字號(hào):
// Fibonacci3.CPP
//
// Copyright (C) Symbian Software Ltd 1997-2005. All rights reserved.
//
// Example wraps a console in an active object and performs the
// Fibonacci calculation as a background active object.
//
// The technique used is similar to, but not identical, to that used by CIdle, where
// the background active object is repeatedly called provided the other (higher priority)
// active objects are not ready to run.
//
//
// Fibonacci Active Objects example
//
#include <e32cons.h>
#include <e32base.h>
#include <e32std.h>
////testing only - can remove this & ModifyLedMask calls
//nclude <e32hal.h>
LOCAL_D CConsoleBase* console;
_LIT(KTxtMainInstructions,"\n\nPress 'F' to start\n 'ESC' to exit\n 'C' to cancel, anytime\n");
//////////////////////////////////////////////////////////////////////////////
//
// -----> CActiveConsole (definition)
//
// An abstract class which provides the facility to issue key requests.
//
//////////////////////////////////////////////////////////////////////////////
class CActiveConsole : public CActive
{
public:
// Construction
CActiveConsole(CConsoleBase* aConsole);
void ConstructL();
// Destruction
~CActiveConsole();
// Issue request
void RequestCharacter();
// Cancel request.
// Defined as pure virtual by CActive;
// implementation provided by this class.
void DoCancel();
// Service completed request.
// Defined as pure virtual by CActive;
// implementation provided by this class,
void RunL();
// Called from RunL() - an implementation must be provided
// by derived classes to handle the completed request
virtual void ProcessKeyPress(TChar aChar) = 0;
protected:
// Data members defined by this class
CConsoleBase* iConsole; // A console for reading from
};
//////////////////////////////////////////////////////////////////////////////
//
// -----> CExampleScheduler (definition)
//
//////////////////////////////////////////////////////////////////////////////
class CActiveConsole;
class CExampleScheduler : public CActiveScheduler
{
public:
void Error (TInt aError) const;
void WaitForAnyRequest();
void SetActiveObject(CActiveConsole* aActiveConsole);
private:
// data members defined for this class
CActiveConsole* iActiveConsole;
};
//////////////////////////////////////////////////////////////////////////////
//
// -----> CFibonacciEngine (definition)
//
// This class provides the fibonacci calculation engine
//
//////////////////////////////////////////////////////////////////////////////
class CFibonacciEngine : public CBase
{
public:
CFibonacciEngine() ;
void StartCalculate (TInt aTerms) ;
void StepCalculate () ;
TUint iResult ;
enum TEngineState {eInactive, eCalculating, eCompleted} ;
TEngineState iState ;
private:
TUint iCurrentTotal ;
TUint iPreviousTotal ;
TInt iTermsLeftToDo ;
} ;
//////////////////////////////////////////////////////////////////////////////
//
// -----> CFibonacciApplication (definition)
//
// This class encapsulates the fibonacci thread that runs the fibonacci engine
//
//////////////////////////////////////////////////////////////////////////////
class CFibonacciApplication : public CActive
{
public:
CFibonacciApplication(CConsoleBase* aConsole, CFibonacciEngine* aFibonacciEngine) ;
~CFibonacciApplication() ;
void CalculateFibonacci(TInt aIterations) ;
private:
void DoCancel() ;
void RunL() ;
private:
CConsoleBase* iConsole ;
CFibonacciEngine* iFibonacciEngine ;
} ;
//////////////////////////////////////////////////////////////////////////////
//
// -----> CFibonacciKeyHandler (definition)
//
// This class encapsulates the fibonacci keyboard handler
//
//////////////////////////////////////////////////////////////////////////////
class CFibonacciKeyHandler : public CActiveConsole
{
public:
CFibonacciKeyHandler( CConsoleBase* aConsole,
CFibonacciApplication* aApplication) ;
void ConstructL();
// Static construction
static CFibonacciKeyHandler* NewLC(CConsoleBase* aConsole, CFibonacciApplication* aHandler) ;
// service request
void ProcessKeyPress(TChar aChar) ;
private:
CConsoleBase* iConsole ;
CFibonacciApplication* iApplication ;
};
//////////////////////////////////////////////////////////////////////////////
//
// -----> CActiveConsole (implementation)
//
//////////////////////////////////////////////////////////////////////////////
CActiveConsole::CActiveConsole( CConsoleBase* aConsole)
: CActive(CActive::EPriorityUserInput)
// Construct high-priority active object
{
iConsole = aConsole;
__DECLARE_NAME(_S("CActiveConsole"));
}
void CActiveConsole::ConstructL()
{
// Add to active scheduler
CActiveScheduler::Add(this);
}
CActiveConsole::~CActiveConsole()
{
// Make sure we're cancelled
Cancel();
}
void CActiveConsole::DoCancel()
{
iConsole->ReadCancel();
}
void CActiveConsole::RunL()
{
// Handle completed request
ProcessKeyPress(TChar(iConsole->KeyCode()));
}
void CActiveConsole::RequestCharacter()
{
// A request is issued to the CConsoleBase to accept a
// character from the keyboard.
iConsole->Read(iStatus);
SetActive();
}
//////////////////////////////////////////////////////////////////////////////
//
// -----> CExampleScheduler (implementation)
//
//////////////////////////////////////////////////////////////////////////////
void CExampleScheduler::Error(TInt aError) const
{
_LIT(KTxtSchedulerError,"CExampleScheduler - error");
User::Panic(KTxtSchedulerError,aError);
}
void CExampleScheduler::WaitForAnyRequest()
{
if (!(iActiveConsole->IsActive()))
iActiveConsole->RequestCharacter();
CActiveScheduler::WaitForAnyRequest();
}
void CExampleScheduler::SetActiveObject(CActiveConsole* aActiveConsole)
{
iActiveConsole = aActiveConsole;
}
/////////////////////////////////////////////////////////////////////////////////
// CFibonacciKeyHandler support routine
// uses up arrow & down arrow to change number, Enter to select
/////////////////////////////////////////////////////////////////////////////////
TInt GetValueFromKeyboard (TInt aInitial, TInt aStep, TInt lowerLimit, TInt upperLimit, const TDesC& aPrompt, CConsoleBase* aConsole)
{
TChar input ;
TInt value = aInitial ;
aConsole->Printf(aPrompt) ;
do
{
aConsole->SetPos(0);
_LIT(KFormat1,"%d ");
aConsole->Printf(KFormat1, value);
input = aConsole->Getch() ;
if (input == EKeyUpArrow && value < upperLimit) value = value + aStep ;
if (input == EKeyDownArrow && value > lowerLimit) value = value - aStep ;
}
while (input != EKeyEnter) ;
return value ;
}
//////////////////////////////////////////////////////////////////////////////
//
// -----> CFibonacciKeyHandler (implementation)
//
//////////////////////////////////////////////////////////////////////////////
CFibonacciKeyHandler::CFibonacciKeyHandler(CConsoleBase* aConsole, CFibonacciApplication* aApplication)
: CActiveConsole(aConsole)
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -