📄 fibonacci2.cpp
字号:
// Fibonacci2.cpp
//
// Copyright (c) 2000 Symbian Ltd. All rights reserved.
// Copyright (C) Symbian Software Ltd 2000-2005. All rights reserved.
// Example wraps a console in an active object and performs the
// Fibonacci calculation in a separate thread.
//
// This is NOT the recommended way and serves only to compare with the
// Fibonacci3 example which performs the calculation as
// a background active object.
//
#include <e32std.h>
#include <e32cons.h>
#include <e32base.h>
//
// Common literals
//
_LIT(KTxtFibThread,"FibThread");
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 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:
void Calculate (TInt aTerms) ;
TUint iResult ;
} ;
//////////////////////////////////////////////////////////////////////////////
//
// -----> TFibonacciParameters (definition)
//
// This class provides for passing parameters to the thread
//
//////////////////////////////////////////////////////////////////////////////
class TFibonacciParameters
{
public:
TInt iVar1 ;
TAny* iVar2 ;
TUint iResult ;
} ;
//////////////////////////////////////////////////////////////////////////////
//
// -----> CFibonacciThreadHandler (definition)
//
// This class encapsulates the fibonacci thread that runs the fibonacci engine
//
//////////////////////////////////////////////////////////////////////////////
class CFibonacciThreadHandler : public CActive
{
public:
CFibonacciThreadHandler(CConsoleBase* aConsole, CFibonacciEngine* aFibonacciEngine) ;
~CFibonacciThreadHandler() ;
void CalculateFibonacci(TInt aIterations) ;
private:
void DoCancel() ;
void RunL() ;
static TInt FibonacciThread(TAny* aParameters) ;
private:
CConsoleBase* iConsole ;
TFibonacciParameters iFibonacciParameters ;
CFibonacciEngine* iFibonacciEngine ;
};
//////////////////////////////////////////////////////////////////////////////
//
// -----> CFibonacciKeyHandler (definition)
//
// This class encapsulates the fibonacci keyboard handler
//
//////////////////////////////////////////////////////////////////////////////
class CFibonacciKeyHandler : public CActiveConsole
{
public:
CFibonacciKeyHandler( CConsoleBase* aConsole,
CFibonacciThreadHandler* iThreadHandler) ;
void ConstructL();
// Static construction
static CFibonacciKeyHandler* NewLC(CConsoleBase* aConsole, CFibonacciThreadHandler* aHandler) ;
// service request
void ProcessKeyPress(TChar aChar) ;
private:
CConsoleBase* iConsole ;
CFibonacciThreadHandler* iThreadHandler ;
};
//////////////////////////////////////////////////////////////////////////////
//
// -----> CActiveConsole (implementation)
//
//////////////////////////////////////////////////////////////////////////////
CActiveConsole::CActiveConsole( CConsoleBase* aConsole)
: CActive(CActive::EPriorityUserInput)
// Construct high-priority active object
{
iConsole = aConsole;
}
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)
//
//////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -