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.

If you're writing a function, and that function calls another one that might leave, then you<br />

should put an L in its name. This will remind you, your colleagues, or anyone else looking at<br />

your code, that this function might leave. Here are a few cases to think about:<br />

� If you're writing a function that calls another, and the other is an L function, then your<br />

function must be an L function too (unless you trap the leave).<br />

� If your function calls new(ELeave), your function must also be an L function.<br />

� If you're implementing a function provided by a framework, then it's important to know<br />

whether that framework function is an L function. If it is, you can allocate resources that<br />

can potentially leave. If not, your code must not leave – or, if it does, you must handle it<br />

privately. For example, CEikAppUi::HandleCommandL() allows you to leave, so<br />

user command handlers can allocate resources and potentially leave.<br />

CCoeControl::Draw() doesn't allow you to leave, so any drawing code you write<br />

must work with preallocated resources and not leave or, if for its own reasons it can't<br />

preallocate resources, it must trap potential leaves and handle them privately.<br />

� If you're specifying a framework function for others to implement, think very carefully<br />

about whether you will allow an implementer to leave. This is an essential aspect of<br />

your function. Don't just code it as an L function to allow the implementer to do what<br />

they want; code L if it's needed, and don't code it if it's not.<br />

The L naming convention gives the same kind of message to the programmer as the<br />

throws clause in Java or standard C++. However, L is not checked by the compiler, so if<br />

you forget to include it in the name, the compiler will not complain. However, if someone else<br />

then uses your function and doesn't realize that it might leave – they may not take<br />

appropriate precautions.<br />

Note<br />

In the C++ Knowledge Base on Symbian's website, you'll find an Lcorrectness<br />

checking script that can be quite useful.<br />

You don't often need to code your own traps, partly because the Symbian OS framework<br />

provides one for you, and partly because (as we'll see shortly) routine cleanup is handled by<br />

other means.<br />

Sometimes, though, you'll need to perform a recovery action, in addition to some nonroutine<br />

cleanup. Or you'll need to code a nonleaving function, such as Draw() that allocates<br />

resources and draws successfully if possible, but otherwise traps any leaves and handles<br />

them internally. In these cases, you must code your own trap.<br />

In a word processor, for example, processing a key press causes many things to happen –<br />

to name but two, there's the allocation of undo buffers, and the expansion of the document<br />

to take a new character. If anything like that goes wrong, you need to undo the operation<br />

completely. You could use code such as<br />

TRAPD(error, HandleKeyL());<br />

if(error)<br />

{<br />

RevertUndoBuffer();<br />

// Any other special cleanup<br />

User::Leave(error);<br />

}<br />

Nested traps

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

Saved successfully!

Ooh no, something went wrong!