03.01.2013 Views

Chapter 1

Chapter 1

Chapter 1

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

some function that will later generate an event. Then call SetActive() to indicate that the<br />

request has been issued.<br />

You can deduce from this that an active object can be responsible for only one outstanding<br />

request at a time. You can also deduce that all request functions take a TRequestStatus&<br />

parameter – or, put the other way round, any function you see with a TRequestStatus&<br />

parameter is a request function, which will complete asynchronously and generate an event.<br />

Our GUI program calls this function from HandleCommandL() using<br />

iDelayedHello->Cancel(); // Just in case<br />

iDelayedHello->SetHello(3000000); // 3-second delay<br />

In other words, it cancels any request so that the assertion in SetHello() is guaranteed to<br />

succeed and then requests a delayed info-message to appear after 3 s.<br />

When the timer event occurs, it is handled by the active object framework, as we'll describe<br />

below, and results in RunL() being called:<br />

void CDelayedHello::RunL()<br />

{<br />

iEnv->InfoMsg(R_ACTIVEHELLO_TEXT_HELLO);<br />

}<br />

Clearly, this code is very simple: it's a one-line function that produces an info-message with<br />

the usual greeting text.<br />

Important<br />

The degree of sophistication in an active object's RunL() function can<br />

vary enormously from one active object to another. CONE's<br />

CCoeEnv::RunL() function initiates an extremely sophisticated chain<br />

of processing; we have plenty of evidence for that from the debug<br />

session in <strong>Chapter</strong> 4. In contrast, the function above was a simple oneliner.<br />

17.1.3 Canceling a Request<br />

If an active object's RunL() calls a leaving function, then that active<br />

object should provide an override of the RunError() function.<br />

If your active object can issue requests, it must also be able to cancel them. CActive<br />

provides a Cancel() function that checks whether a request is active and if so calls<br />

DoCancel() to cancel it. As the implementer of the active object, you have to implement<br />

DoCancel():<br />

void CDelayedHello::DoCancel()<br />

{<br />

iTimer.Cancel();<br />

}<br />

There is no need for any checking here. Because CActive has already checked that a<br />

request is active, there is no need for you to check this or to reset the active flag.<br />

Important<br />

There is an obligation on any class with request functions, to provide

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!