Friday, May 06, 2005

7. How do I create a small, stack-based descriptor?

If it needs to be modifiable, use a TBuf. If it's constant, use a TBufC. You'll need to specify the size of stack space allocated for the descriptor's data.

Here’s an example:

_LIT(KFred, “fred”); // A string literal, these will be described later
TBufC<4> constantFred(KFred);

In reality, you probably wouldn't do this for a non-modifiable descriptor, since you can use the literal, KFred directly by calling the operator()() (as I'll describe separately in 22. What are literal descriptors?). That is, instead of creating constantFred, you could just call KFred().

However, this approach is still useful for modifiable descriptors, for example, for logging purposes:

TInt CMyActiveObject::RunError(TInt aError)
_LIT8(KError, "CMyActiveObject::RunError: %d");
TBuf8<35> errorBuf;
errorBuf.Format(KError, aError); // reference 3 has more information about TDes::Format()
iLogger.Log(errorBuf); // iLogger is a log object owned by the class
return (KErrNone);

What if I want a descriptor to hold ASCII text?

There are some cases where you do need to hold 8-bit text: MIME types, or binary data, for example. To do this, you should explicitly use the 8 bit descriptors:

_LIT8(KImageGif, “image/gif”);
TBufC8<15> mimeType(KImageGif);


I'm having problems with Format with %s and I guess I'm not alone... A quick example on that would be helpful (with normal TBuf/TDes as parameter and also when using KNullDesC as parameter). The official symbian format reference is as cryptic as usual...
Hi Toni

You're not alone, and I sympathise. I ran out of time to do a proper post today, but I hope this helps (if you've not solved it already):


(1) To display C-style strings, use %s as the formatter
(2) To display descriptors, use %S.
(3) You must also pass the address of descriptors or an exception occurs

For example:

// C string
TText array[] = L"Hello";
_LIT(KTxtWorld, " World");
TBufC<20> constantBuffer(KTxtWorld);
// assume a CConsoleBase* console
_LIT(KFormatTxt, "%s%S");
console->Printf(KFormatTxt, array, &constantBuffer);

This displays ‘Hello World’
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?

WWW Descriptors FAQ