Saturday, May 28, 2005
22. What are literal descriptors?
Literal descriptors are constant descriptors which are compiled into ROM because their contents does not ever change. They are equivalent to static char[] in C.
There are two types of literal descriptor, _LIT which is the most efficient, and preferred, Symbian OS literal and _L, which is now deprecated in all but test code.
In the examples I've included in this blog, I've used _LIT throughout, so you've seen it in action already, for example:
_LIT(KFred, "Fred");
On the wide builds of Symbian OS created today, this is equivalent to the following:
_LIT16(KFred, "Fred");
If an explicitly narrow literal descriptor is required, this can be created using the _LIT8 macro:
_LIT8(KFred, "Fred");
KFred can then be used as a constant descriptor, either directly or to initialise a TPtrC/TPtr or TBufC/TBuf.
The macros are defined in the Symbian OS header file e32def.h and create an object of type TLitC16 or TLitC8 (defined in e32des.h). These classes do not derive from TDesC8 or TDesC16 but have the same binary layout as TBufC8 or TBufC16. This means that the literal object can be used wherever TDesC is used. The literal classes provide operator()() which is very useful to cast the literal object to a TDesC. For example:
_LIT(KFred, "Fred");
HBufC8* theHeapBuffer = KFred().AllocL();
To save you additional code, Symbian OS already defines a literal to represent a blank string, defined as follows:
Build independent: _LIT(KNullDesC,"");
8-bit for non-Unicode strings: _LIT8(KNullDesC8,"");
16-bit for Unicode strings: _LIT16(KNullDesC16,"");
There are two types of literal descriptor, _LIT which is the most efficient, and preferred, Symbian OS literal and _L, which is now deprecated in all but test code.
In the examples I've included in this blog, I've used _LIT throughout, so you've seen it in action already, for example:
_LIT(KFred, "Fred");
On the wide builds of Symbian OS created today, this is equivalent to the following:
_LIT16(KFred, "Fred");
If an explicitly narrow literal descriptor is required, this can be created using the _LIT8 macro:
_LIT8(KFred, "Fred");
KFred can then be used as a constant descriptor, either directly or to initialise a TPtrC/TPtr or TBufC/TBuf.
The macros are defined in the Symbian OS header file e32def.h and create an object of type TLitC16 or TLitC8 (defined in e32des.h). These classes do not derive from TDesC8 or TDesC16 but have the same binary layout as TBufC8 or TBufC16. This means that the literal object can be used wherever TDesC is used. The literal classes provide operator()() which is very useful to cast the literal object to a TDesC. For example:
_LIT(KFred, "Fred");
HBufC8* theHeapBuffer = KFred().AllocL();
To save you additional code, Symbian OS already defines a literal to represent a blank string, defined as follows:
Build independent: _LIT(KNullDesC,"");
8-bit for non-Unicode strings: _LIT8(KNullDesC8,"");
16-bit for Unicode strings: _LIT16(KNullDesC16,"");
Comments:
<< Home
"These classes do not derive from TDesC8 or TDesC16 but have the same binary layout as TBufC8 or TBufC16."
What do we mean by binary layout?
Post a Comment
What do we mean by binary layout?
<< Home