⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 active2.cpp

📁 如题 就是 这东西 为什么非要我 说到 20个 字 呢 看看这回 够 不
💻 CPP
字号:
// active2.cpp
//
// Copyright (c) 1999-2007 Symbian Software Ltd.  All rights reserved.
//
// $Change: 937687 $

// Asynchronous keyboard processing with messenger program.
// A single CKeyMessengerProcessor active object (derived from
// class CActiveConsole) which accepts input from keyboard, but does not
// print it.
// This object contains a CMessageTimer object which it activates if the
// user inputs the character "m" and cancelled if the user inputs "c".

// PROJECT HEADERS
#include "eustd.h"
#include "active2.h"

//////////////////////////////////////////////////////////////////////////////
//
// -----> CExampleScheduler (implementation)
//
//////////////////////////////////////////////////////////////////////////////
void CExampleScheduler::Error(TInt aError) const
    {
    _LIT(KMsgSchedErr, "CExampleScheduler-error");
    User::Panic(KMsgSchedErr, aError);
    }

//////////////////////////////////////////////////////////////////////////////
//
// -----> 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();
    }

//////////////////////////////////////////////////////////////////////////////
//
// -----> CMessageKeyProcessor (implementation)
//
//////////////////////////////////////////////////////////////////////////////

//TODO Add Timed Messenger object to parameter list
CMessageKeyProcessor::CMessageKeyProcessor(CConsoleBase* aConsole,
                                            CTimedMessenger* aMessenger)
    : CActiveConsole(aConsole)
    {
    // construct zero-priority active object
    iMessenger = aMessenger;
    } 

//TODO Add Timed Messenger object to parameter list
CMessageKeyProcessor* CMessageKeyProcessor::NewLC(CConsoleBase* aConsole,
        CTimedMessenger* aMessenger)
    {
    //TODO Add Timed Messenger object to list of parameters passed in
    CMessageKeyProcessor* self=new (ELeave) CMessageKeyProcessor(aConsole, aMessenger);
    CleanupStack::PushL(self);
    self->ConstructL();
    return self;
    }

//TODO Add Timed Messenger object to parameter list
CMessageKeyProcessor* CMessageKeyProcessor::NewL(CConsoleBase* aConsole,
                                                CTimedMessenger* aMessenger)
    {
    //TODO Add Timed Messenger object to list of parameters passed in
    CMessageKeyProcessor* self = NewLC(aConsole, aMessenger);
    CleanupStack::Pop(self);
    return self;
    }

void CMessageKeyProcessor::ConstructL()
    {
    // Add to active scheduler
    CActiveScheduler::Add(this);
    }

void CMessageKeyProcessor::ProcessKeyPress(TChar aChar)
    {
    //TODO
    // Change this member function so that keys are processed as follows:

    // if key is ESC 
    //   cancel any outstanding request
    //   stop the scheduler
    // If key is "m" or "M"
    //   cancel any outstanding request
    //   issue a message timer request.
    // If key is "c" or "C" 
    //    cancel any outstanding request
    
    if (aChar == EKeyEscape)
        {
        iMessenger->Cancel();
        CActiveScheduler::Stop();
        return;
        }

    if (aChar == 'm' || aChar == 'M') 
        {
        _LIT(KMsgStarting, "Starting Messenger....  \n");
        iConsole->Printf(KMsgStarting);
        iMessenger->Cancel();
        iMessenger->IssueRequest();
        }

    if (aChar == 'c' || aChar == 'C')
        {
        iMessenger->Cancel();
        }
    
    // Ask for another character.
    RequestCharacter();
    }

//////////////////////////////////////////////////////////////////////////////
//
// -----> CTimedMessenger (implementation)
//
//////////////////////////////////////////////////////////////////////////////
//TODO Uncomment class functions
CTimedMessenger::CTimedMessenger() : CTimer(CActive::EPriorityStandard)
    // Construct  zero-priority active object
    {};

CTimedMessenger* CTimedMessenger::NewLC(const TDesC& aGreeting,
                                        TInt aTicksRequested,
                                        TInt aTicksInterval)
    {
    CTimedMessenger* self=new(ELeave) CTimedMessenger;
    CleanupStack::PushL(self);
    self->ConstructL(aGreeting, aTicksRequested, aTicksInterval);
    return self;
    }

CTimedMessenger* CTimedMessenger::NewL(const TDesC& aGreeting,
                                        TInt aTicksRequested,
                                        TInt aTicksInterval)
    {
    CTimedMessenger* self = NewLC(aGreeting, aTicksRequested, aTicksInterval);
    CleanupStack::Pop(self);
    return self;
    }

void CTimedMessenger::ConstructL(const TDesC& aGreeting,
                                    TInt aTicksRequested,
                                    TInt aTicksInterval)
    {
    // Base class second-phase construction.
    CTimer::ConstructL();
    // Set members from arguments
    iGreeting       = aGreeting;       // Set greeting text.
    iTicksRequested = aTicksRequested; // Ticks requested
    iTicksInterval  = aTicksInterval;  // Interval between ticks
    // Add active object to active scheduler
    CActiveScheduler::Add(this); 
    }

CTimedMessenger::~CTimedMessenger()
    {
    // Make sure we're cancelled
    Cancel();
    }

void CTimedMessenger::DoCancel()
    {
    // Call base class version of function
    //TODO
    // Base class
    CTimer::DoCancel(); 
    //TODO
    // Reset this variable - needed if the object is re-activated later
    iTicksDone = 0;
    // Tell user message has been cancelled
    _LIT(KMsgCancelled, "Outstanding Messenger \nrequest cancelled\n");
    console->Printf(KMsgCancelled); 
    }

void CTimedMessenger::IssueRequest()
    {
    // There should never be an outstanding request at this point.
    _LIT(KMsgAlreadyActive, "Is already Active");
    __ASSERT_ALWAYS(!IsActive(), User::Panic(KMsgAlreadyActive, EPanicAlreadyActive));
    // Request another wait
    //TODO
    CTimer::After(iTicksInterval * 1000000);
    }

void CTimedMessenger::RunL()
    {
    // Handle request completion.
    // Remember to keep track ofiTicksDone (how many times message request is issued)
    // Message must be sent iTicksRequested times
    // stop scheduler when all requested messages sent
    //TODO
  
    // One more tick done
    iTicksDone++;
    // Print greeting
    _LIT(KFormatString1, "%S \n");
    console->Printf(KFormatString1, &iGreeting);
    // Issue new request, or stop if we have reached the limit
    if (iTicksDone < iTicksRequested)
        {
        IssueRequest();
        }
    else
        {
        _LIT(KMsgFinished, "Messenger finished \n");
        console->Printf(KMsgFinished);
        // Reset this variable - needed if the object is re-activated later
        iTicksDone = 0;
        // Can now stop the active scheduler
        CActiveScheduler::Stop();
        }
    }

//////////////////////////////////////////////////////////////////////////////
//
// Do the example
//
//////////////////////////////////////////////////////////////////////////////
LOCAL_C void doExampleL()
    {
    // Construct and install the active scheduler
    CExampleScheduler*  exampleScheduler = new(ELeave) CExampleScheduler;

    // Push onto the cleanup stack
    CleanupStack::PushL(exampleScheduler);
     
    // Install as the active scheduler
    CActiveScheduler::Install(exampleScheduler);

    //TODO Use the following message
    _LIT(KMsgGoodMorning, "Good Morning!");

    //TODO Create a CTimedMessenger active object with it's NewLC which will emit
    // 3 messages with an interval of 2 seconds between messages.
    CTimedMessenger* messenger = CTimedMessenger::NewLC(KMsgGoodMorning, 3, 2);

    // Remember that the CTimedMessenger object will have to be cleaned up
    // See cleanup stack below

    //TODO Comment out the first introductory message, comment in the two below
    //_LIT(KTitleMsg, "A single CMessageKeyProcessor active object.\nIt accepts and prints keyboard input to the console.\nPress ESC to end.\n\n");
    //console->Printf(KTitleMsg);
    _LIT(KMsgTitleA, "A single \nCKeyMessengerProcessor \nactive object which contains a CMessageTimer.\n");
    console->Printf(KMsgTitleA);
    _LIT(KMsgTitleB, "Press 'm' to activate it;\nPress 'c' to cancel it.\nPress ESC to end.\n\n");
    console->Printf(KMsgTitleB);

    //TODO Add the messenger argument to the NewLC's parameter list
    CMessageKeyProcessor* keyProcesser = CMessageKeyProcessor::NewLC(console, messenger);
      
    // Issue the first request
    keyProcesser->RequestCharacter();
    
    // Main part of the program is a wait loop
    // This function completes when the scheduler stops
    CActiveScheduler::Start();

    //TODO PopAndDestroy the CTimed Messenger object we've created and pusted onto the cleanup stack
    // Remove from the cleanup stack and destroy:
    // 1. the CTimedMessenger active object
    // 2. the CMessageKeyProcessor active object.
    // 3. exampleScheduler
    CleanupStack::PopAndDestroy(3, exampleScheduler); 
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -