03.01.2013 Views

Chapter 1

Chapter 1

Chapter 1

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

There is another version of Write() here, which takes a length that is used to override the<br />

length in the descriptor. This is a simple convenience function. If it had not been provided in<br />

the API, and you wanted it, then instead of using file.Write(buffer,length), you<br />

could use file.Write(buffer.Left(length)). Left() returns a TPtrC with the<br />

same address as its argument, but with its length shortened to the number of characters (or<br />

bytes) required.<br />

Likewise, the Read() functions take a descriptor in which the data to be read will be put.<br />

These parameters are specified as TDes& types. Read()-like functions coded like this use<br />

one of three conventions:<br />

� They fill the destination buffer to its MaxLength().<br />

� They fill the destination buffer with a fixed amount of data, but truncate if that would<br />

exceed MaxLength().<br />

� They fill the destination buffer with a fixed amount of data, but panic if the buffer isn't<br />

big enough.<br />

RFile usually fills the entire buffer – except when reading has reached the end of the file.<br />

Many servers and device drivers follow the third convention and expect the buffer to be big<br />

enough.<br />

Communications protocols at the stream level behave like an RFile. Communications<br />

protocols at the packet level behave like a server or an I/O device.<br />

In <strong>Chapter</strong> 16 and those that follow, we'll learn a lot more about binary descriptors, as we get<br />

to grips with packet-based communications and servers for the Battleships game.<br />

5.7 Summary<br />

In this chapter, I've introduced you to the way Symbian OS handles strings and other data,<br />

using descriptors. The main advantages of descriptors are<br />

� descriptors can handle both string and binary data<br />

� they provide a uniform API for dealing with data, whether it is part of a program binary,<br />

on the stack, or on the default heap<br />

� they prevent buffer overflow errors, but otherwise don't hide memory management<br />

issues – preserving the balance between safety and efficiency that underlies Symbian<br />

OS<br />

� descriptors encapsulate the address and length of data<br />

� 8-bit versions of descriptor classes allow binary data to be handled.<br />

In terms of the classes we've seen<br />

� for immutable strings and data, classes ending with C provide constfunctions, derived<br />

from TDesC<br />

� when modifying strings or data, use classes derived from TDes, with its nonconst<br />

functions<br />

� the _LIT and _L macros, the difference between them and why _LIT is preferred.

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

Saved successfully!

Ooh no, something went wrong!