SlideShare une entreprise Scribd logo
1  sur  77
Symbian OS Descriptors v2.0a – 21 May 2008 1 Andreas Jakl, 2008
Disclaimer These slides are provided free of charge at http://www.symbianresources.com and are used during Symbian OS courses at the University of Applied Sciences in Hagenberg, Austria ( http://www.fh-hagenberg.at/ ) Respecting the copyright laws, you are allowed to use them: for your own, personal, non-commercial use in the academic environment In all other cases (e.g. for commercial training), please contact andreas.jakl@fh-hagenberg.at The correctness of the contents of these materials cannot be guaranteed. Andreas Jakl is not liable for incorrect information or damage that may arise from using the materials. Parts of these materials are based on information from Symbian Press-books published by John Wiley & Sons, Ltd. This document contains copyright materials which are proprietary to Symbian, UIQ, Nokia and SonyEricsson. “S60™” is a trademark of Nokia. “UIQ™” is a trademark of UIQ Technology. Pictures of mobile phones or applications are copyright their respective manufacturers / developers. “Symbian ™”, “Symbian OS ™” and all other Symbian-based marks and logos are trademarks of Symbian Software Limited and are used under license. © Symbian Software Limited 2006.  Andreas Jakl, 2008 2
Contents Literals Descriptors TDes / TDesC TBuf / TBufC TPtr / TPtrC HBufC RBuf Descriptorsasfunctionparameters / returnvalues Convertingto/from Unicode String Numberconversion Andreas Jakl, 2008 3
Introduction Why are they different to Strings? Andreas Jakl, 2008 4
Andreas Jakl, 2008 5 h e l l o  Strings in C char* hello = "hello"; Memory view: Function strlen() Reads from the beginning to '' and counts number of chars char* 0x64243184
Descriptors – Introduction Descriptor = Symbian OS string “self-describing” Holds length as well as type (= memory layout) Different to: CString (C++), Java Strings, ...  for descriptors, allocation and cleanup is managed by programmer C strings  Descriptors protect against buffer overrun and don’t use  termination Andreas Jakl, 2008 6
Descriptors – Motivation Why no normal strings? Minimal memory usage Required efficiency ROM (Literals: _LIT!) – Stack – Heap? Constant – modifiable? Unicode(TBuf16 – TBuf8) Take a long time to get used to  Andreas Jakl, 2008 7
Unicode Symbian OS uses 16-bit text since Symbian OS v5u+ All descriptors available twice 8 bit (e.g. TBuf8) Used for binary data and ASCII text 16 bit (e.g. TBuf16) Normally not used explicitly, instead: Build independent (e.g. TBuf) Currently always Unicode typedef’d to 16 bit version Use this for normal strings Andreas Jakl, 2008 8
Literals _L, _LIT Andreas Jakl, 2008 9
Literals In reality _LIT is a macro, expands to create a: Constant descriptor, compiled to program binary Not localisable  only use for testing, very simple applications or fixed strings (e.g. for protocols)! _LIT(KHello, “Hello”); Builds a named object called KHello of type TLitC16 Stores the string Hello into the object The string is written to the program binary Andreas Jakl, 2008 10
_LIT-Macro TLitChas the same memory layout like a descriptor  can be used as a constant descriptor for function parameters (const TDesC&) _LIT(KHello, "Hello!");console->Printf(KHello);iLabel->SetTextL(KHello);  can be casted to a descriptor through ()-operator TInt length = KHello().Length();		// == 6 Andreas Jakl, 2008 11
_L-Macro _L(“Hello”) Can be used directly as parameter console->Printf(_L(“Hello”)); User::Panic(_L(“example.dll”), KErrNotSupported); Creates temporary TPtrC inefficient, deprecated! Both macros defined in e32def.h Andreas Jakl, 2008 12
Comparison Andreas Jakl, 2008 13 _L(“Hello”) (through macro)  TPtrC hello(_L(“Hello”)) _LIT(KHello, “Hello”); ROM ROM Stack (temporary) ‘H’ 5 ‘e’ ‘l’ ‘l’ ‘o’ ‘’ ‘H’ 5 ‘e’ ‘l’ ‘l’ ‘o’ ‘’ <ptr> For completeness: Literals are actually ‘’-terminated because of the standard C++ compiler, but the additional ‘’-char not reflected in the saved length All other descriptors are not ‘’-terminated!
Descriptors TDes, TDesC Andreas Jakl, 2008 14
Modifiable? Do these methods modify the data? Andreas Jakl, 2008 15
Modifiable – Solution Do these methods modify the data? Andreas Jakl, 2008 16          TDesC  TDes defined in ...
Constant – Modifiable? Modifiable (derived from TDes) Allow to modify data (replace chars, appending, ...) Non-modifiable (derived from TDesC) Constant data (read-only) Modify it anyway? Possible, if data is not in ROM: Get modifiable pointer descriptor to data using myText.Des(); Andreas Jakl, 2008 17
Inheritance Hierarchy Abstract base class because of: Generalisation(use base type for parameters!) Provide basic functions shared by all types (e.g. Compare(), Find(), Mid(), ...) Andreas Jakl, 2008 18 constant modifiable
TDesC / TDes Base class of all other descriptors (except Literals) Provide basic functionality TDesC: Read-only access (compare, search, ...) TDes: Inherits from TDesC and adds modification functions.Saves maximum length to prevent overflow. Cannot be instantiated Allow polymorphic use of descriptors  Commonly used in function parameters Don’t care where data is stored Andreas Jakl, 2008 19
Storing the Length Constant (TDesC) Modifiable (TDes) Andreas Jakl, 2008 20 <length> <string data> Depending on descriptor type:can directly contain the data or  be a pointer to data on the stack, heap or ROM 4 bytes <length> <max.length> <string data> 4 bytes 4 bytes
Type For Completeness: All Descriptors and Literals also store the type of the Descriptor (Stack, Heap, ROM, …) First 4 bits of iLength reduces max. length to 28 bits Used in common methods of TDesand TDesCto execute correct code depending on type using switch()-statement (Alternative: virtual functions, but that’d cause overhead) Andreas Jakl, 2008 21 The type is normally not relevantfor the developer, therefore it is omitted in following slides for clarity TDesC <length> <string data> <type> 4 bit 28 bit 4 bytes (32 bit)
Stack-Based Buffer Descriptors TBuf, TBufC Andreas Jakl, 2008 22
Buffer Descriptors Comparable to (const) char[]of C Directly contain the string Use C++ templates to specify length (parameter) Andreas Jakl, 2008 23 TBufC<5> Constant: ‘H’ 5 ‘e’ ‘l’ ‘l’ ‘o’ iLength(TDesC) Modifiable: TBuf<9> 9 ‘H’ 5 ‘e’ ‘l’ ‘l’ ‘o’ iLength(TDesC) iMaxLength(TDes)
Size, Length, MaxLength TBuf<9> with text “Hello” Andreas Jakl, 2008 24 9 ‘H’ 5 ‘e’ ‘l’ ‘l’ ‘o’ ... 4 bytes 4 bytes 2 2 Unicode characters! Length()  5 (characters) MaxLength()  9 (characters) Size()  10 (bytes) MaxSize()  18 (bytes)
Initializing the TBuf Debugging in Carbide.c++: Assigning Strings Andreas Jakl, 2008 25 // Define constant string literal _LIT(KString1, "Hello "); _LIT(KString2, "World"); // Copy KString1 into new TBuf with max. length of 15 TBuf<15> buf1(KString1); // Same as above, this time using Copy() and KString2 TBuf<15> buf2;		// Initialize empty TBuf with max. length of 15 buf2.Copy(KString2); // Append contents of buf2 to buf1 buf1.Append(buf2); // Create constant descriptor based on KString1 TBufC<15> cBuf1(KString1); // Replace contents of cBuf1 with contents of buf1 cBuf1 = buf1;
Using the TBuf Andreas Jakl, 2008 26 _LIT(KHelloWorld, "Hello World");	// Defines constant string literal constTIntmaxLength = 15; // Create a modifiable buffer TBuf<maxLength> buf;	 TIntcurLength = buf.Length();		// ...... ? TInt maxLength2 = buf.MaxLength();	// ...... ? // Set the contents of the buffer buf = KHelloWorld; curLength = buf.Length();			// ...... ? TIntcurSize = buf.Size();				// ...... ? TTextch = buf[1];					// ...... ?
Using the TBuf Andreas Jakl, 2008 27 _LIT(KHelloWorld, "Hello World");	// Defines constant string literal constTIntmaxLength = 15; // Create a modifiable buffer TBuf<maxLength> buf;	 TIntcurLength = buf.Length();		// == 0 TInt maxLength2 = buf.MaxLength();	// == 20 // Set the contents of the buffer buf = KHelloWorld; curLength = buf.Length();			// == 11 TIntcurSize = buf.Size();				// == 22 TTextch = buf[1];					// == ‘e’
Maximum Length Andreas Jakl, 2008 28 _LIT(KHello, "Hello ");			// Defines constant string literal TBuf<15> buf(KHello);			// buf == “Hello ” buf.Append(KHello);			// buf == “Hello Hello “ buf.Append(KHello);			// Exceeds maximum length  Panic! SDK-Doc for USER 11-panic: “This panic is raised when any operation that moves or copies data to a 16-bit variant descriptor, causes the length of that descriptor to exceed its maximum length. […]”
Pointer Descriptors TPtr, TPtrC Andreas Jakl, 2008 29
Pointer Descriptors Comparable to (const) char* of C Can point to text on the heap, stack or ROM Do NOT own the data they point to! Andreas Jakl, 2008 30 ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ Constant: TPtrC 0x64243184 5 ROM, heaporstack iLength(TDesC) iPtr(TPtrC) ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ TPtr Modifiable: 0x64243184 5 9 ROM, heaporstack iLength(TDesC) iPtr(TPtrC) iMaxLength(TDes)
TPtr – Example Andreas Jakl, 2008 31 _LIT(KHelloWorld, "Hello World");	// Defines constant string literal // Create a constant buffer with contents of string literal TBufC<13> buf (KHelloWorld); // Get a pointer descriptor for the text TPtrptr = buf.Des(); // Replace the 6th char with an '!', even though TBufC is actually constant! ptr[5] = '!';						// buf: "Hello!World" TBufC<13> (Stack) ‘H’ 11 ‘e’ ‘l’ ‘l’ ‘o’ ‘!’ ‘W’ ‘o’ ‘r’ ‘l’ ‘d’ TPtr(Stack) iLength(TDesC) 11 13 0x0303FE5C Data iscopiedto thestack KHelloWorld(ROM) iLength(TDesC) iPtr(TPtrC) iMaxLength(TDes) ‘H’ 11 ‘e’ ‘l’ ‘l’ ‘o’ ‘ ’ ‘W’ ‘o’ ‘r’ ‘l’ ‘d’ ‘’ iLength
TPtrC: “=” versus “Set“ Andreas Jakl, 2008 32 // Create two literals _LIT(KHi, "Hi"); _LIT(KSymbian, "Symbian"); // Constant buffer descriptors, copy data from ROM to Stack TBufC<10> bufHi(KHi); TBufC<10> bufSymbian(KSymbian); // ptr1 and ptr2 point to string data owned by TBufC's TPtr ptr1(bufHi.Des()); TPtr ptr2(bufSymbian.Des()); ptr1 = ptr2; ptr1.Set(ptr2); ptr1  bufHi: “Symbian” ptr2  bufSymbian: “Symbian” bufHi: “Symbian” bufSymbian: “Symbian” … copies Data from buffer pointed toptr2 to buffer pointed to by ptr1 ptr1  bufSymbian: “Symbian” ptr2  bufSymbian: “Symbian” bufHi: “Hi” bufSymbian: “Symbian” … sets ptr1 to point to same bufferas ptr2
Heap-Based Buffer Descriptors HBufC, RBuf Andreas Jakl, 2008 33
Use Heap-Based Buffer when… String data is not in ROM and not on stack Stack size is a lot more limited than the heap! Length of buffer is not known at compile time, e.g. Loading strings from resource file Getting strings from UI (query dialogs, ...) Receiving response from the web Longer lifetime than creator e.g. passing to an asynchronous function Andreas Jakl, 2008 34
Constant Heap Descriptor Comparable to (char*) malloc(length+1) of C Data is stored on the heap Andreas Jakl, 2008 35 Heap ‘H’ 5 ‘e’ ‘l’ ‘l’ ‘o’ HBufC* 0x64243184 Same layout as TBufC
HBufC Example Andreas Jakl, 2008 36 Shorter variant: HBufC* hBuf = KHello().AllocLC(); (Function of TDesC, creates and returns heap buffer based on a copy of its own data) _LIT(KHello, "Hello"); // Create new HBufC with length of 5 HBufC* hBuf = HBufC::NewLC(KHello().Length()); // Copy data of KHello to heap allocated by hBuf *hBuf = KHello; […] // Do cleanup CleanupStack::PopAndDestroy(1); Heap ‘H’ 5 ‘e’ ‘l’ ‘l’ ‘o’ HBufC* 0x64243184 KHello (ROM) Data is copiedto the heap ‘H’ 5 ‘e’ ‘l’ ‘l’ ‘o’ ‘’ iLength
Modifying HBufC Modify HBufC through apointer descriptor: Andreas Jakl, 2008 37 _LIT(KHello, "Hello!"); _LIT(KWorld, "World!"); HBufC* buf = HBufC::NewLC(KHello().Length()); *buf = KHello;						// buf holds "Hello!", Length = 6 // Increase heap buffer size - still holds "Hello!" buf = buf->ReAllocL(KHello().Length() + KWorld().Length()); // New memory area has been reserved – update reference on CleanupStack! CleanupStack::Pop(buf); CleanupStack::PushL(buf); // Create pointer descriptor based on heap descriptor TPtrptr(buf->Des());				// Length = 6, MaxLength = 12 ptr[KHello().Length() - 1] = ' ';		// buf holds "Hello " ptr.Append(KWorld);				// buf holds "Hello World!" CleanupStack::PopAndDestroy(buf); To access the data of an HBufC* (e.g. to use it for a TDesC&-parameter), you can write *hptr instead of creating a TPtrC with hptr->Des();
HBufC – Memory View Andreas Jakl, 2008 38 TPtr 12 12 0x02704174 iLength(TDesC) iPtr(TPtrC) iMaxLength(TDes) Heap HBufC* 0x02704174 ‘H’ 12 ‘e’ ‘l’ ‘l’ ‘o’ ‘ ’ ‘W’ ‘o’ ‘r’ ‘l’ ‘d’ ‘!’
RBuf Available since Symbian OS 8 Owns and points to modifiable string data on the heap Behaves like a handle to a resource (Close() to free) Resize possible, but has to be done manually Similar to: TPtr, but owns data it points to HBufC, but easier to use Can be wrapper for HBufC-Object Andreas Jakl, 2008 39
RBuf – Memory Layout Andreas Jakl, 2008 40 Heap RBuf points directly to its owned data: ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ RBuf 0x64243184 5 9 iLength(TDesC) *iEPtrType(RBuf) iMaxLength(TDes) C++ Union (only one of both variables are used, depending on type) Heap RBuf points to owned HBufC: ‘H’ 5 ‘e’ ‘l’ ‘l’ ‘o’ RBuf 5 9 0x64243184 = contents of old HBufC iLength(TDesC) *iEBufCPtrType(RBuf) iMaxLength(TDes)
RBuf – Construction Creating an RBuf: Allocate own memoryRBufbuf;buf.CreateL(TIntaMaxLength);	// Create RBuf with max. lengthor: buf.CreateL(constTDesC &aDes);	// Copy data to own memory Transfer ownership of HBufC(wrapper for HBufC)RBufbuf(HBufC* aHBuf);		// Transfer ownership of HBufC-dataor: buf.Assign(HBufC* aHBuf); Transfer ownership of existing memory areabuf.Assign(TUint16 *aHeapCell, TIntaMaxLength); Andreas Jakl, 2008 41
Using the RBuf Andreas Jakl, 2008 42 _LIT(KHelloWorld, "Hello World!"); RBufheapBuf; // Create RBuf, copy contents of KHelloWorld heapBuf.CreateL(KHelloWorld); // Make sure Close() is called on RBuf in case of Leave heapBuf.CleanupClosePushL(); […] // Calls Close() on the RBuf CleanupStack::PopAndDestroy(); Directly points to string data(ASCII 72 = “H” of “Hello World”)
RBuf – Memory view Andreas Jakl, 2008 43 Heap 0x027d4174 Hello World Stack Data iscopiedto theheap *iEPtrType ROM 11 iMaxLength RBufheapBuf Hello World 11 11 iLength
RBuf and HBufC Andreas Jakl, 2008 44 _LIT(KHelloWorld, "Hello World!"); HBufC* hptr; // Create heap descriptor which can hold up to 15 items. Current lenght = 0 hptr = HBufC::NewL(15); // Assigns data to heap descriptor. Current length = 12 *hptr = KHelloWorld; // Ownership of heap descriptor passed to RBuf RBufbuf(hptr); // Close buffer and free resources - do not delete HBuf! buf.Close();
RBuf & HBufC – Memory View Andreas Jakl, 2008 45 Heap 0x027d4174 Hello World 11 Stack Data is copiedto the heap *iEBufCPtrType 11 iMaxLength RBuf buf ROM 11 iLength Hello World 11 HBufC* hptr ... don’t delete twice, now the RBuf owns the memory!
One more Example It’s recommended to use RBuf instead of HBufC* Andreas Jakl, 2008 46 // Function returns HBufC*, we use RBuf to wrap it  simpler handling for us! RBufresString (iEikonEnv->AllocReadResourceL(someResourceId)); // Leaving functions ahead, so push the RBuf on the CleanupStack resString.CleanupClosePushL(); // Use modifiable descriptor to append new text... resString.ReAllocL(resString.Length() + 4); resString.Append(_L("-Foo")); […] // Assign text to a label, resString is a TDesC as well, so it works without changes! SetLabelL(ELabel, resString); CleanupStack::PopAndDestroy(); 		// calls resString.Close();
RBuf vs. HBufC? Theoretically: Use HBufC for constant data Use RBuf for modifiable data However: As RBuf is a lot easier to use, consider always using RBuf Most Symbian OS APIs operate using HBufC Either also use HBufC for these cases Or wrap HBufC with RBuf Andreas Jakl, 2008 47
Summary Abstract (TDes, TDesC) Base class of other descriptors Cannot be instantiated Used for function parameters Literal (TLitC _LIT()) Used to store non-modifiable, literal strings in code Buffer (TBuf, TBufC) Data is stored on the stack Size specified at compile time Heap (HBufC, RBuf) Data stored on the heap Size specified at run-time Pointer (TPtr, TPtrC) References data stored outside the class Andreas Jakl, 2008 48
Lots of types? Decide: Andreas Jakl, 2008 49 Is the descriptor modifiable? YES NO Has the memory for the descriptor data already been allocated elsewhere (heap or stack)? Has the memory for the descriptor data already been allocated elsewhere (heap or stack)? YES YES TPtr TPtrC Will the memory allocated be stack- or heap-based? Will the memory allocated be stack- or heap-based? HEAP HEAP RBuf HBufC STACK STACK TBuf TBufC
Quiz Andreas Jakl, 2008 50 TDesC::AllocLC()“Creates a new 16-bit heap descriptor, initializes it with a copy of this descriptor's data, and puts a pointer to the descriptor onto the cleanup stack.” Assign variables to the memory view: _LIT(KHello1, “Hello”); TPtrChello2 (KHello1); TBufC<5> hello3(KHello1); HBufC* hello4 = KHello1().AllocLC(); Heap hello 5 Stack …? hello …? 5 ROM …? …? hello 5 5
Quiz – Solution Assign variables to the memory view: Andreas Jakl, 2008 51 _LIT(KHello1, “Hello”); TPtrChello2 (KHello1); TBufC<5> hello3(KHello1); HBufC* hello4 = KHello1().AllocLC(); Heap hello 5 Stack hello4 hello hello3 5 ROM KHello1 hello2 hello 5 5
Using Descriptors in Functions The daily life of a Symbian OS developer … Andreas Jakl, 2008 52
Function Arguments void MyFunction(TBuf<8>aText) What if caller uses an RBuf, TPtr or TBuf<9>?  References for base classes (TDes&, const TDesC&) should always be used void SomeFunction(const TDesC& aReadOnlyDescriptor, TDes& aReadWriteDescriptor); Andreas Jakl, 2008 53
Example TInterrorCode = RFile.Read(TDes8& aDes); Function can find out max. length (aDes.MaxLength()) Caller can get number of bytes read: des.Length() Compare that to the Win32-API … BOOL ReadFile(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped);  Andreas Jakl, 2008 54
Return HBufC* Make sure calling function knows it takes ownership! Don’t forget the cleanup stack if it is necessary! Andreas Jakl, 2008 55 HBufC* CreateSomeDescriptorL() {	_LIT(KBert, "bert"); // Createheap-baseddescriptorHBufC* newBert = KBert().AllocL(); // Return ourHBufC*return (newBert);} HBufC* CreateSomeDescriptorLC() {	_LIT(KBert, "bert"); HBufC* newBert = KBert().AllocLC(); return (newBert);}
Better: RBuf Creating a specific RBuf in a function: Andreas Jakl, 2008 56 RBufmyBuf; myBuf.CleanupClosePushL(); GetSomeDataL(myBuf); console->Printf(myBuf); CleanupStack::PopAndDestroy(myBuf); voidGetSomeDataL(RBuf&aBuf) {	_LIT(KHello, "Hello"); // Allocatememory and copy „Hello“ to RBufaBuf.CreateL(KHello); }
Returning a TPtrC What’s wrong with the following code? Andreas Jakl, 2008 57 TPtrCGetText() {  // CreateTBufCbased on literaldescriptor 	_LIT(KMyText, "My Text");TBufC<7> buf(KMyText()); // Create a TPtrCbased on bufTPtrCbufPtr(buf); // Return theTPtrCreturn (bufPtr);}
Returning a TPtrC – Solution bufPtr does not own the data buf is a local variable and ceases to exist when function is left  Target of bufPtris no longer valid! Andreas Jakl, 2008 58 TPtrCGetText() {  // CreateTBufCbased on literaldescriptor 	_LIT(KMyText, "My Text");TBufC<7> buf(KMyText()); // Create a TPtrCbased on bufTPtrCbufPtr(buf); // Return theTPtrCreturn (bufPtr);}
Unicode and Binary Painless conversion Andreas Jakl, 2008 59 Conversion is more important than you might think: e.g. socket communication is 8 bit!
Converting from Unicode Simple conversion by stripping alternate characters: Andreas Jakl, 2008 60 TBuf8<3> cat(_L8("cat"));		// _L used for simplicity TBuf16<3> dog(_L16("dog")); cat.Copy(dog);				// cat now contains "dog" TDes8 cat TDes16 dog cat.Copy(dog); Strips -padding TDes8 cat
Converting to Unicode Simple conversion by padding each character with trailing zero: Andreas Jakl, 2008 61 TBuf8<5> small(_L8("small")); TBuf16<5> large(_L16("large")); large.Copy(small);				// large now contains "small" TDes8 small TDes16 large large.Copy(small); Adds -padding TDes8 large
Proper Conversion Use conversion library (charconv.lib) Requires: #include <charconv.h> #include <utf.h> Library: charconv.lib Andreas Jakl, 2008 62 // Russian text saved as UTF8 _LIT8(KString8, "Телефон"); TBufC8<20> source8(KString8); // Create target 16 bit buffer for Unicode string RBuf target16; target16.CreateL(source8.Length()); target16.CleanupClosePushL(); // Use conversion library to convert from UTF8 to Unicode CnvUtfConverter::ConvertToUnicodeFromUtf8(target16, source8); // Print results and do cleanup console->Printf(target16); CleanupStack::PopAndDestroy(1);
More about Descriptor Usage Other things you should know Andreas Jakl, 2008 63
TLex General string-parsing functions suitable for numeric format conversions and syntactical-element parsing e.g. parse GPS data (NMEA) Powerful, but very complex More information at: http://descriptors.blogspot.com/2005/08/35-how-do-i-use-tlex.html Andreas Jakl, 2008 64
Conversion String  Number String  Number: Provided by TDes Number  String: Simple use of TLex Andreas Jakl, 2008 65 _LIT(KTestString1, "54321"); // Convertstring to number TLexlex(KTestString1()); TIntvalue = 0; User::LeaveIfError(lex.Val(value)); ASSERT(value==54321); // Convertnumber to string TBuf<8> buf; buf.Num(value);
Formatting Text Integrate variables into text Appending text or numbers Simplest way: use the AppendNum()-function defined in TDes Make sure the target descriptor is large enough! Andreas Jakl, 2008 66 _LIT(KText, "Number "); TBuf<15> myText(KText); myText.AppendNum(1); console->Printf(myText);
Formatting Text Format strings Use placeholders for elements Full syntax in the SDK help: » Symbian OS vXX » Symbian OS guide » Base » Using User Library (E32) » Buffers and Strings » Using Descriptors » How to Use Descriptors » Format string syntax Console (format syntax directly integrated) Andreas Jakl, 2008 67 _LIT(KName, "Joe Bloggs"); _LIT(KText, "Name: %S, Age: %d"); TBuf<30> myText; myText.Format(KText, &KName, 27); console->Printf(myText); TInt result = 0; _LIT(KFormatCompare1, "Compare() using str1 and str2 = %d"); console->Printf(KFormatCompare1, result);
TFileName Definition of TFileName(e32const.h / e32cmn.h): const TIntKMaxFileName=0x100;		// = Decimal 256typedefTBuf<KMaxFileName> TFileName; Required size on the stack: 520 bytes 2 x 256 data bytes (Unicode), 8 bytes for descriptor obj. Standard stack size in Symbian OS: 8kB Therefore: Do not allocate TFileName on the stack (however: instance var. of CBase-class is on the heap!) Do not pass it by value! … use RBuf instead! Andreas Jakl, 2008 68
Test your Knowledge Did you understand everything? Andreas Jakl, 2008 69
ASD-like Question – Easy  Which of the following statements about descriptors are correct? A. All descriptor classes, except RBuf, derive from TDesC. B. The first four bytes of a descriptor store the descriptor’s length and type. C. The descriptor classes do not use virtual functions to avoid the overhead of a virtual function pointer in every descriptor object. D. Modifiable descriptors use the NULL terminator to indicate the end of the descriptor data. E. All descriptors have “wide”, 16-bit characters. Andreas Jakl, 2008 70 Copyright Meme Education, 2006 http://www.meme-education.com/
Solution A.Incorrect. RBuf derives from TDesC as well. B. Correct. C. Correct. D. Incorrect. The NULL terminator is not used by descriptors. Only Literals are -terminated because of the C++-compiler, but this is hidden from the developers. E. Incorrect. Descriptors are available as 8 bit and 16 bit variants. Andreas Jakl, 2008 71
ASD-like Question – Medium Andreas Jakl, 2008 72 _LIT(KHello, "Hello!"); TBufC<6> hello(KHello); _LIT(KBye, "Goodbye!"); TBufC<8> bye(KBye); TPtrfoo(hello.Des()); TIntlen = foo.Length(); TIntmaxLen = foo.MaxLength(); TPtr bar(bye.Des()); foo.Set(bar); len = foo.Length(); maxLen = foo.MaxLength(); foo.Copy(KHello); len = foo.Length(); maxLen = foo.MaxLength(); Which of the following statements are correct? A. After executing line 2, a call to hello.MaxLength() returns 6. B. After executing line 6, len = 6; After line 7, maxLen = 6. C. After executing line 9, hello contains “Goodbye!”. D. After executing line 10, len = 8; After line 11, maxLen = 8. E. After executing line 13, len = 6; After line 14, maxLen = 6. Copyright Meme Education, 2006 http://www.meme-education.com/
Solution A.Incorrect. TBufC does not have a MaxLength()-function. This is defined in TDes base class. B. Correct. C. Incorrect. After the Set()-function, the foo-TPtr points to the same memory as the bar-TPtr. The buffers are not altered however. This would be the case with =. D. Correct.  E. Incorrect. MaxLength = 8, as foo is now pointing to the buffer of the bar-TPtr, which is the bye-TBuf with a max. length of 8. Andreas Jakl, 2008 73
ASD-like Question – Hard Andreas Jakl, 2008 74 Which of the following uses of a descriptor should be reconsidered? A.	USE 1 B.	USE 2 C.	USE 3 D.	USE 4 E.	USE 5 classCTestClass : publicCBase { public: staticCTestClass* NewL(constTFileNameaFilename); // USE 1 public: inlineconstTDesC& FileName() {return (iFileName);}; // USE 2 voidSendDataL(constTBufC<20>& aData); // USE 3 voidReceiveDataL(TDes& aData); // USE 4 private: 	// Construction code omitted for clarity private: TFileNameiFileName; // USE 5 }; Copyright Meme Education, 2006 http://www.meme-education.com/
Solution A.Reconsider. TFileName should be passed by reference, not by value.  B. OK. C. Reconsider. Use generic parameter instead (const TDesC& aData). D. OK.  E. OK.  Andreas Jakl, 2008 75
Resources Famous blogs with answers to common problems: http://descriptors.blogspot.com/ http://descriptor-tips.blogspot.com/ Detailed information in the books: Symbian OS C++ for Mobile Phones, Vol. 3 Symbian OS Explained Andreas Jakl, 2008 76 Unfortunately this book do not cover the most recent addition to Symbian OS, the RBuf. Apart from this minor disadvantage, it is very useful.
Thanks for your attention That’s it! Andreas Jakl, 2008 77

Contenu connexe

Tendances

JIT compilation for CPython
JIT compilation for CPythonJIT compilation for CPython
JIT compilation for CPythondelimitry
 
Coding in Disaster Relief - Worksheet (Advanced)
Coding in Disaster Relief - Worksheet (Advanced)Coding in Disaster Relief - Worksheet (Advanced)
Coding in Disaster Relief - Worksheet (Advanced)Charling Li
 
Productive OpenCL Programming An Introduction to OpenCL Libraries with Array...
Productive OpenCL Programming An Introduction to OpenCL Libraries  with Array...Productive OpenCL Programming An Introduction to OpenCL Libraries  with Array...
Productive OpenCL Programming An Introduction to OpenCL Libraries with Array...AMD Developer Central
 
Hooking signals and dumping the callstack
Hooking signals and dumping the callstackHooking signals and dumping the callstack
Hooking signals and dumping the callstackThierry Gayet
 
Suyash Thesis Presentation
Suyash Thesis PresentationSuyash Thesis Presentation
Suyash Thesis PresentationTanvee Katyal
 
2.Format Strings
2.Format Strings2.Format Strings
2.Format Stringsphanleson
 
Analyzing the Dolphin-emu project
Analyzing the Dolphin-emu projectAnalyzing the Dolphin-emu project
Analyzing the Dolphin-emu projectPVS-Studio
 
C for Java programmers (part 3)
C for Java programmers (part 3)C for Java programmers (part 3)
C for Java programmers (part 3)Dmitry Zinoviev
 
C for Java programmers (part 1)
C for Java programmers (part 1)C for Java programmers (part 1)
C for Java programmers (part 1)Dmitry Zinoviev
 
C for Java programmers (part 2)
C for Java programmers (part 2)C for Java programmers (part 2)
C for Java programmers (part 2)Dmitry Zinoviev
 
Smalltalk implementation of EXIL, a Component-based Programming Language
 Smalltalk implementation of EXIL, a Component-based Programming Language Smalltalk implementation of EXIL, a Component-based Programming Language
Smalltalk implementation of EXIL, a Component-based Programming LanguageESUG
 
Optimising code using Span<T>
Optimising code using Span<T>Optimising code using Span<T>
Optimising code using Span<T>Mirco Vanini
 
Presentation on C++ programming
Presentation on C++ programming Presentation on C++ programming
Presentation on C++ programming AditiTibile
 

Tendances (20)

First session quiz
First session quizFirst session quiz
First session quiz
 
JIT compilation for CPython
JIT compilation for CPythonJIT compilation for CPython
JIT compilation for CPython
 
Coding in Disaster Relief - Worksheet (Advanced)
Coding in Disaster Relief - Worksheet (Advanced)Coding in Disaster Relief - Worksheet (Advanced)
Coding in Disaster Relief - Worksheet (Advanced)
 
Productive OpenCL Programming An Introduction to OpenCL Libraries with Array...
Productive OpenCL Programming An Introduction to OpenCL Libraries  with Array...Productive OpenCL Programming An Introduction to OpenCL Libraries  with Array...
Productive OpenCL Programming An Introduction to OpenCL Libraries with Array...
 
C language
C languageC language
C language
 
Hooking signals and dumping the callstack
Hooking signals and dumping the callstackHooking signals and dumping the callstack
Hooking signals and dumping the callstack
 
Suyash Thesis Presentation
Suyash Thesis PresentationSuyash Thesis Presentation
Suyash Thesis Presentation
 
Mpi
Mpi Mpi
Mpi
 
2.Format Strings
2.Format Strings2.Format Strings
2.Format Strings
 
C
CC
C
 
Analyzing the Dolphin-emu project
Analyzing the Dolphin-emu projectAnalyzing the Dolphin-emu project
Analyzing the Dolphin-emu project
 
MPI Tutorial
MPI TutorialMPI Tutorial
MPI Tutorial
 
C tutorial
C tutorialC tutorial
C tutorial
 
C for Java programmers (part 3)
C for Java programmers (part 3)C for Java programmers (part 3)
C for Java programmers (part 3)
 
C for Java programmers (part 1)
C for Java programmers (part 1)C for Java programmers (part 1)
C for Java programmers (part 1)
 
C for Java programmers (part 2)
C for Java programmers (part 2)C for Java programmers (part 2)
C for Java programmers (part 2)
 
Smalltalk implementation of EXIL, a Component-based Programming Language
 Smalltalk implementation of EXIL, a Component-based Programming Language Smalltalk implementation of EXIL, a Component-based Programming Language
Smalltalk implementation of EXIL, a Component-based Programming Language
 
Optimising code using Span<T>
Optimising code using Span<T>Optimising code using Span<T>
Optimising code using Span<T>
 
Parallel computing(2)
Parallel computing(2)Parallel computing(2)
Parallel computing(2)
 
Presentation on C++ programming
Presentation on C++ programming Presentation on C++ programming
Presentation on C++ programming
 

Similaire à Symbian OS - Descriptors

Symbian OS - Memory Management
Symbian OS - Memory ManagementSymbian OS - Memory Management
Symbian OS - Memory ManagementAndreas Jakl
 
Symbian OS - Dynamic Arrays
Symbian OS - Dynamic ArraysSymbian OS - Dynamic Arrays
Symbian OS - Dynamic ArraysAndreas Jakl
 
Symbian OS - Types And Declarations
Symbian OS - Types And DeclarationsSymbian OS - Types And Declarations
Symbian OS - Types And DeclarationsAndreas Jakl
 
0100_Embeded_C_CompilationProcess.pdf
0100_Embeded_C_CompilationProcess.pdf0100_Embeded_C_CompilationProcess.pdf
0100_Embeded_C_CompilationProcess.pdfKhaledIbrahim10923
 
#include avrinterrupt.h The global interrupt flag is maintained.pdf
#include avrinterrupt.h The global interrupt flag is maintained.pdf#include avrinterrupt.h The global interrupt flag is maintained.pdf
#include avrinterrupt.h The global interrupt flag is maintained.pdfarasanlethers
 
Java ME - 06 - Record Stores, Distribution and Localization
Java ME - 06 - Record Stores, Distribution and LocalizationJava ME - 06 - Record Stores, Distribution and Localization
Java ME - 06 - Record Stores, Distribution and LocalizationAndreas Jakl
 
Den-long-men of void-jar(hardware and Software)
Den-long-men of void-jar(hardware and Software)Den-long-men of void-jar(hardware and Software)
Den-long-men of void-jar(hardware and Software)HU-man
 
0-Slot14-15-16-Libraries.pdf
0-Slot14-15-16-Libraries.pdf0-Slot14-15-16-Libraries.pdf
0-Slot14-15-16-Libraries.pdfssusere19c741
 
Writing c code for the 8051
Writing c code for the 8051Writing c code for the 8051
Writing c code for the 8051Quản Minh Tú
 
Rust and Eclipse
Rust and EclipseRust and Eclipse
Rust and EclipseMax Bureck
 
Keyboard interrupt
Keyboard interruptKeyboard interrupt
Keyboard interruptTech_MX
 
An Introduction To C++Templates
An Introduction To C++TemplatesAn Introduction To C++Templates
An Introduction To C++TemplatesGanesh Samarthyam
 
C101 – Intro to Programming with C
C101 – Intro to Programming with CC101 – Intro to Programming with C
C101 – Intro to Programming with Cgpsoft_sk
 

Similaire à Symbian OS - Descriptors (20)

Symbian OS - Memory Management
Symbian OS - Memory ManagementSymbian OS - Memory Management
Symbian OS - Memory Management
 
Symbian OS - Dynamic Arrays
Symbian OS - Dynamic ArraysSymbian OS - Dynamic Arrays
Symbian OS - Dynamic Arrays
 
C programming language
C programming languageC programming language
C programming language
 
Symbian OS - Types And Declarations
Symbian OS - Types And DeclarationsSymbian OS - Types And Declarations
Symbian OS - Types And Declarations
 
0100_Embeded_C_CompilationProcess.pdf
0100_Embeded_C_CompilationProcess.pdf0100_Embeded_C_CompilationProcess.pdf
0100_Embeded_C_CompilationProcess.pdf
 
#include avrinterrupt.h The global interrupt flag is maintained.pdf
#include avrinterrupt.h The global interrupt flag is maintained.pdf#include avrinterrupt.h The global interrupt flag is maintained.pdf
#include avrinterrupt.h The global interrupt flag is maintained.pdf
 
Java ME - 06 - Record Stores, Distribution and Localization
Java ME - 06 - Record Stores, Distribution and LocalizationJava ME - 06 - Record Stores, Distribution and Localization
Java ME - 06 - Record Stores, Distribution and Localization
 
Den-long-men of void-jar(hardware and Software)
Den-long-men of void-jar(hardware and Software)Den-long-men of void-jar(hardware and Software)
Den-long-men of void-jar(hardware and Software)
 
C programming session10
C programming  session10C programming  session10
C programming session10
 
Embedded C programming session10
Embedded C programming  session10Embedded C programming  session10
Embedded C programming session10
 
0-Slot14-15-16-Libraries.pdf
0-Slot14-15-16-Libraries.pdf0-Slot14-15-16-Libraries.pdf
0-Slot14-15-16-Libraries.pdf
 
Writing c code for the 8051
Writing c code for the 8051Writing c code for the 8051
Writing c code for the 8051
 
8051
80518051
8051
 
Rust and Eclipse
Rust and EclipseRust and Eclipse
Rust and Eclipse
 
Keyboard interrupt
Keyboard interruptKeyboard interrupt
Keyboard interrupt
 
Embedded system classes in mumbai
Embedded system classes in mumbaiEmbedded system classes in mumbai
Embedded system classes in mumbai
 
C language tutorial
C language tutorialC language tutorial
C language tutorial
 
An Introduction To C++Templates
An Introduction To C++TemplatesAn Introduction To C++Templates
An Introduction To C++Templates
 
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
 
C101 – Intro to Programming with C
C101 – Intro to Programming with CC101 – Intro to Programming with C
C101 – Intro to Programming with C
 

Plus de Andreas Jakl

Create Engaging Healthcare Experiences with Augmented Reality
Create Engaging Healthcare Experiences with Augmented RealityCreate Engaging Healthcare Experiences with Augmented Reality
Create Engaging Healthcare Experiences with Augmented RealityAndreas Jakl
 
AR / VR Interaction Development with Unity
AR / VR Interaction Development with UnityAR / VR Interaction Development with Unity
AR / VR Interaction Development with UnityAndreas Jakl
 
Android Development with Kotlin, Part 3 - Code and App Management
Android Development with Kotlin, Part 3 - Code and App ManagementAndroid Development with Kotlin, Part 3 - Code and App Management
Android Development with Kotlin, Part 3 - Code and App ManagementAndreas Jakl
 
Android Development with Kotlin, Part 2 - Internet Services and JSON
Android Development with Kotlin, Part 2 - Internet Services and JSONAndroid Development with Kotlin, Part 2 - Internet Services and JSON
Android Development with Kotlin, Part 2 - Internet Services and JSONAndreas Jakl
 
Android Development with Kotlin, Part 1 - Introduction
Android Development with Kotlin, Part 1 - IntroductionAndroid Development with Kotlin, Part 1 - Introduction
Android Development with Kotlin, Part 1 - IntroductionAndreas Jakl
 
Android and NFC / NDEF (with Kotlin)
Android and NFC / NDEF (with Kotlin)Android and NFC / NDEF (with Kotlin)
Android and NFC / NDEF (with Kotlin)Andreas Jakl
 
Basics of Web Technologies
Basics of Web TechnologiesBasics of Web Technologies
Basics of Web TechnologiesAndreas Jakl
 
Bluetooth Beacons - Bluetooth 5, iBeacon, Eddystone, Arduino, Windows 10 & More
Bluetooth Beacons - Bluetooth 5, iBeacon, Eddystone, Arduino, Windows 10 & MoreBluetooth Beacons - Bluetooth 5, iBeacon, Eddystone, Arduino, Windows 10 & More
Bluetooth Beacons - Bluetooth 5, iBeacon, Eddystone, Arduino, Windows 10 & MoreAndreas Jakl
 
Which new scenarios are enabled by Windows 10 for NFC, Bluetooth LE & Beacons?
Which new scenarios are enabled by Windows 10 for NFC, Bluetooth LE & Beacons?Which new scenarios are enabled by Windows 10 for NFC, Bluetooth LE & Beacons?
Which new scenarios are enabled by Windows 10 for NFC, Bluetooth LE & Beacons?Andreas Jakl
 
Mobile Test Automation
Mobile Test AutomationMobile Test Automation
Mobile Test AutomationAndreas Jakl
 
Qt App Development - Cross-Platform Development for Android, iOS, Windows Pho...
Qt App Development - Cross-Platform Development for Android, iOS, Windows Pho...Qt App Development - Cross-Platform Development for Android, iOS, Windows Pho...
Qt App Development - Cross-Platform Development for Android, iOS, Windows Pho...Andreas Jakl
 
WinJS, Apache Cordova & NFC - HTML5 apps for Android and Windows Phone
WinJS, Apache Cordova & NFC - HTML5 apps for Android and Windows PhoneWinJS, Apache Cordova & NFC - HTML5 apps for Android and Windows Phone
WinJS, Apache Cordova & NFC - HTML5 apps for Android and Windows PhoneAndreas Jakl
 
Nokia New Asha Platform Developer Training
Nokia New Asha Platform Developer TrainingNokia New Asha Platform Developer Training
Nokia New Asha Platform Developer TrainingAndreas Jakl
 
Windows Phone 8 NFC Quickstart
Windows Phone 8 NFC QuickstartWindows Phone 8 NFC Quickstart
Windows Phone 8 NFC QuickstartAndreas Jakl
 
Windows (Phone) 8 NFC App Scenarios
Windows (Phone) 8 NFC App ScenariosWindows (Phone) 8 NFC App Scenarios
Windows (Phone) 8 NFC App ScenariosAndreas Jakl
 
Windows 8 Platform NFC Development
Windows 8 Platform NFC DevelopmentWindows 8 Platform NFC Development
Windows 8 Platform NFC DevelopmentAndreas Jakl
 
NFC Development with Qt - v2.2.0 (5. November 2012)
NFC Development with Qt - v2.2.0 (5. November 2012)NFC Development with Qt - v2.2.0 (5. November 2012)
NFC Development with Qt - v2.2.0 (5. November 2012)Andreas Jakl
 
06 - Qt Communication
06 - Qt Communication06 - Qt Communication
06 - Qt CommunicationAndreas Jakl
 
05 - Qt External Interaction and Graphics
05 - Qt External Interaction and Graphics05 - Qt External Interaction and Graphics
05 - Qt External Interaction and GraphicsAndreas Jakl
 

Plus de Andreas Jakl (20)

Create Engaging Healthcare Experiences with Augmented Reality
Create Engaging Healthcare Experiences with Augmented RealityCreate Engaging Healthcare Experiences with Augmented Reality
Create Engaging Healthcare Experiences with Augmented Reality
 
AR / VR Interaction Development with Unity
AR / VR Interaction Development with UnityAR / VR Interaction Development with Unity
AR / VR Interaction Development with Unity
 
Android Development with Kotlin, Part 3 - Code and App Management
Android Development with Kotlin, Part 3 - Code and App ManagementAndroid Development with Kotlin, Part 3 - Code and App Management
Android Development with Kotlin, Part 3 - Code and App Management
 
Android Development with Kotlin, Part 2 - Internet Services and JSON
Android Development with Kotlin, Part 2 - Internet Services and JSONAndroid Development with Kotlin, Part 2 - Internet Services and JSON
Android Development with Kotlin, Part 2 - Internet Services and JSON
 
Android Development with Kotlin, Part 1 - Introduction
Android Development with Kotlin, Part 1 - IntroductionAndroid Development with Kotlin, Part 1 - Introduction
Android Development with Kotlin, Part 1 - Introduction
 
Android and NFC / NDEF (with Kotlin)
Android and NFC / NDEF (with Kotlin)Android and NFC / NDEF (with Kotlin)
Android and NFC / NDEF (with Kotlin)
 
Basics of Web Technologies
Basics of Web TechnologiesBasics of Web Technologies
Basics of Web Technologies
 
Bluetooth Beacons - Bluetooth 5, iBeacon, Eddystone, Arduino, Windows 10 & More
Bluetooth Beacons - Bluetooth 5, iBeacon, Eddystone, Arduino, Windows 10 & MoreBluetooth Beacons - Bluetooth 5, iBeacon, Eddystone, Arduino, Windows 10 & More
Bluetooth Beacons - Bluetooth 5, iBeacon, Eddystone, Arduino, Windows 10 & More
 
Which new scenarios are enabled by Windows 10 for NFC, Bluetooth LE & Beacons?
Which new scenarios are enabled by Windows 10 for NFC, Bluetooth LE & Beacons?Which new scenarios are enabled by Windows 10 for NFC, Bluetooth LE & Beacons?
Which new scenarios are enabled by Windows 10 for NFC, Bluetooth LE & Beacons?
 
Mobile Test Automation
Mobile Test AutomationMobile Test Automation
Mobile Test Automation
 
Qt App Development - Cross-Platform Development for Android, iOS, Windows Pho...
Qt App Development - Cross-Platform Development for Android, iOS, Windows Pho...Qt App Development - Cross-Platform Development for Android, iOS, Windows Pho...
Qt App Development - Cross-Platform Development for Android, iOS, Windows Pho...
 
WinJS, Apache Cordova & NFC - HTML5 apps for Android and Windows Phone
WinJS, Apache Cordova & NFC - HTML5 apps for Android and Windows PhoneWinJS, Apache Cordova & NFC - HTML5 apps for Android and Windows Phone
WinJS, Apache Cordova & NFC - HTML5 apps for Android and Windows Phone
 
Nokia New Asha Platform Developer Training
Nokia New Asha Platform Developer TrainingNokia New Asha Platform Developer Training
Nokia New Asha Platform Developer Training
 
Windows Phone 8 NFC Quickstart
Windows Phone 8 NFC QuickstartWindows Phone 8 NFC Quickstart
Windows Phone 8 NFC Quickstart
 
Windows (Phone) 8 NFC App Scenarios
Windows (Phone) 8 NFC App ScenariosWindows (Phone) 8 NFC App Scenarios
Windows (Phone) 8 NFC App Scenarios
 
Windows 8 Platform NFC Development
Windows 8 Platform NFC DevelopmentWindows 8 Platform NFC Development
Windows 8 Platform NFC Development
 
NFC Development with Qt - v2.2.0 (5. November 2012)
NFC Development with Qt - v2.2.0 (5. November 2012)NFC Development with Qt - v2.2.0 (5. November 2012)
NFC Development with Qt - v2.2.0 (5. November 2012)
 
06 - Qt Communication
06 - Qt Communication06 - Qt Communication
06 - Qt Communication
 
05 - Qt External Interaction and Graphics
05 - Qt External Interaction and Graphics05 - Qt External Interaction and Graphics
05 - Qt External Interaction and Graphics
 
04 - Qt Data
04 - Qt Data04 - Qt Data
04 - Qt Data
 

Dernier

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 

Dernier (20)

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 

Symbian OS - Descriptors

  • 1. Symbian OS Descriptors v2.0a – 21 May 2008 1 Andreas Jakl, 2008
  • 2. Disclaimer These slides are provided free of charge at http://www.symbianresources.com and are used during Symbian OS courses at the University of Applied Sciences in Hagenberg, Austria ( http://www.fh-hagenberg.at/ ) Respecting the copyright laws, you are allowed to use them: for your own, personal, non-commercial use in the academic environment In all other cases (e.g. for commercial training), please contact andreas.jakl@fh-hagenberg.at The correctness of the contents of these materials cannot be guaranteed. Andreas Jakl is not liable for incorrect information or damage that may arise from using the materials. Parts of these materials are based on information from Symbian Press-books published by John Wiley & Sons, Ltd. This document contains copyright materials which are proprietary to Symbian, UIQ, Nokia and SonyEricsson. “S60™” is a trademark of Nokia. “UIQ™” is a trademark of UIQ Technology. Pictures of mobile phones or applications are copyright their respective manufacturers / developers. “Symbian ™”, “Symbian OS ™” and all other Symbian-based marks and logos are trademarks of Symbian Software Limited and are used under license. © Symbian Software Limited 2006. Andreas Jakl, 2008 2
  • 3. Contents Literals Descriptors TDes / TDesC TBuf / TBufC TPtr / TPtrC HBufC RBuf Descriptorsasfunctionparameters / returnvalues Convertingto/from Unicode String Numberconversion Andreas Jakl, 2008 3
  • 4. Introduction Why are they different to Strings? Andreas Jakl, 2008 4
  • 5. Andreas Jakl, 2008 5 h e l l o Strings in C char* hello = &quot;hello&quot;; Memory view: Function strlen() Reads from the beginning to &apos;&apos; and counts number of chars char* 0x64243184
  • 6. Descriptors – Introduction Descriptor = Symbian OS string “self-describing” Holds length as well as type (= memory layout) Different to: CString (C++), Java Strings, ...  for descriptors, allocation and cleanup is managed by programmer C strings  Descriptors protect against buffer overrun and don’t use termination Andreas Jakl, 2008 6
  • 7. Descriptors – Motivation Why no normal strings? Minimal memory usage Required efficiency ROM (Literals: _LIT!) – Stack – Heap? Constant – modifiable? Unicode(TBuf16 – TBuf8) Take a long time to get used to  Andreas Jakl, 2008 7
  • 8. Unicode Symbian OS uses 16-bit text since Symbian OS v5u+ All descriptors available twice 8 bit (e.g. TBuf8) Used for binary data and ASCII text 16 bit (e.g. TBuf16) Normally not used explicitly, instead: Build independent (e.g. TBuf) Currently always Unicode typedef’d to 16 bit version Use this for normal strings Andreas Jakl, 2008 8
  • 9. Literals _L, _LIT Andreas Jakl, 2008 9
  • 10. Literals In reality _LIT is a macro, expands to create a: Constant descriptor, compiled to program binary Not localisable  only use for testing, very simple applications or fixed strings (e.g. for protocols)! _LIT(KHello, “Hello”); Builds a named object called KHello of type TLitC16 Stores the string Hello into the object The string is written to the program binary Andreas Jakl, 2008 10
  • 11. _LIT-Macro TLitChas the same memory layout like a descriptor  can be used as a constant descriptor for function parameters (const TDesC&) _LIT(KHello, &quot;Hello!&quot;);console-&gt;Printf(KHello);iLabel-&gt;SetTextL(KHello);  can be casted to a descriptor through ()-operator TInt length = KHello().Length(); // == 6 Andreas Jakl, 2008 11
  • 12. _L-Macro _L(“Hello”) Can be used directly as parameter console-&gt;Printf(_L(“Hello”)); User::Panic(_L(“example.dll”), KErrNotSupported); Creates temporary TPtrC inefficient, deprecated! Both macros defined in e32def.h Andreas Jakl, 2008 12
  • 13. Comparison Andreas Jakl, 2008 13 _L(“Hello”) (through macro)  TPtrC hello(_L(“Hello”)) _LIT(KHello, “Hello”); ROM ROM Stack (temporary) ‘H’ 5 ‘e’ ‘l’ ‘l’ ‘o’ ‘’ ‘H’ 5 ‘e’ ‘l’ ‘l’ ‘o’ ‘’ &lt;ptr&gt; For completeness: Literals are actually ‘’-terminated because of the standard C++ compiler, but the additional ‘’-char not reflected in the saved length All other descriptors are not ‘’-terminated!
  • 14. Descriptors TDes, TDesC Andreas Jakl, 2008 14
  • 15. Modifiable? Do these methods modify the data? Andreas Jakl, 2008 15
  • 16. Modifiable – Solution Do these methods modify the data? Andreas Jakl, 2008 16          TDesC  TDes defined in ...
  • 17. Constant – Modifiable? Modifiable (derived from TDes) Allow to modify data (replace chars, appending, ...) Non-modifiable (derived from TDesC) Constant data (read-only) Modify it anyway? Possible, if data is not in ROM: Get modifiable pointer descriptor to data using myText.Des(); Andreas Jakl, 2008 17
  • 18. Inheritance Hierarchy Abstract base class because of: Generalisation(use base type for parameters!) Provide basic functions shared by all types (e.g. Compare(), Find(), Mid(), ...) Andreas Jakl, 2008 18 constant modifiable
  • 19. TDesC / TDes Base class of all other descriptors (except Literals) Provide basic functionality TDesC: Read-only access (compare, search, ...) TDes: Inherits from TDesC and adds modification functions.Saves maximum length to prevent overflow. Cannot be instantiated Allow polymorphic use of descriptors  Commonly used in function parameters Don’t care where data is stored Andreas Jakl, 2008 19
  • 20. Storing the Length Constant (TDesC) Modifiable (TDes) Andreas Jakl, 2008 20 &lt;length&gt; &lt;string data&gt; Depending on descriptor type:can directly contain the data or be a pointer to data on the stack, heap or ROM 4 bytes &lt;length&gt; &lt;max.length&gt; &lt;string data&gt; 4 bytes 4 bytes
  • 21. Type For Completeness: All Descriptors and Literals also store the type of the Descriptor (Stack, Heap, ROM, …) First 4 bits of iLength reduces max. length to 28 bits Used in common methods of TDesand TDesCto execute correct code depending on type using switch()-statement (Alternative: virtual functions, but that’d cause overhead) Andreas Jakl, 2008 21 The type is normally not relevantfor the developer, therefore it is omitted in following slides for clarity TDesC &lt;length&gt; &lt;string data&gt; &lt;type&gt; 4 bit 28 bit 4 bytes (32 bit)
  • 22. Stack-Based Buffer Descriptors TBuf, TBufC Andreas Jakl, 2008 22
  • 23. Buffer Descriptors Comparable to (const) char[]of C Directly contain the string Use C++ templates to specify length (parameter) Andreas Jakl, 2008 23 TBufC&lt;5&gt; Constant: ‘H’ 5 ‘e’ ‘l’ ‘l’ ‘o’ iLength(TDesC) Modifiable: TBuf&lt;9&gt; 9 ‘H’ 5 ‘e’ ‘l’ ‘l’ ‘o’ iLength(TDesC) iMaxLength(TDes)
  • 24. Size, Length, MaxLength TBuf&lt;9&gt; with text “Hello” Andreas Jakl, 2008 24 9 ‘H’ 5 ‘e’ ‘l’ ‘l’ ‘o’ ... 4 bytes 4 bytes 2 2 Unicode characters! Length()  5 (characters) MaxLength()  9 (characters) Size()  10 (bytes) MaxSize()  18 (bytes)
  • 25. Initializing the TBuf Debugging in Carbide.c++: Assigning Strings Andreas Jakl, 2008 25 // Define constant string literal _LIT(KString1, &quot;Hello &quot;); _LIT(KString2, &quot;World&quot;); // Copy KString1 into new TBuf with max. length of 15 TBuf&lt;15&gt; buf1(KString1); // Same as above, this time using Copy() and KString2 TBuf&lt;15&gt; buf2; // Initialize empty TBuf with max. length of 15 buf2.Copy(KString2); // Append contents of buf2 to buf1 buf1.Append(buf2); // Create constant descriptor based on KString1 TBufC&lt;15&gt; cBuf1(KString1); // Replace contents of cBuf1 with contents of buf1 cBuf1 = buf1;
  • 26. Using the TBuf Andreas Jakl, 2008 26 _LIT(KHelloWorld, &quot;Hello World&quot;); // Defines constant string literal constTIntmaxLength = 15; // Create a modifiable buffer TBuf&lt;maxLength&gt; buf; TIntcurLength = buf.Length(); // ...... ? TInt maxLength2 = buf.MaxLength(); // ...... ? // Set the contents of the buffer buf = KHelloWorld; curLength = buf.Length(); // ...... ? TIntcurSize = buf.Size(); // ...... ? TTextch = buf[1]; // ...... ?
  • 27. Using the TBuf Andreas Jakl, 2008 27 _LIT(KHelloWorld, &quot;Hello World&quot;); // Defines constant string literal constTIntmaxLength = 15; // Create a modifiable buffer TBuf&lt;maxLength&gt; buf; TIntcurLength = buf.Length(); // == 0 TInt maxLength2 = buf.MaxLength(); // == 20 // Set the contents of the buffer buf = KHelloWorld; curLength = buf.Length(); // == 11 TIntcurSize = buf.Size(); // == 22 TTextch = buf[1]; // == ‘e’
  • 28. Maximum Length Andreas Jakl, 2008 28 _LIT(KHello, &quot;Hello &quot;); // Defines constant string literal TBuf&lt;15&gt; buf(KHello); // buf == “Hello ” buf.Append(KHello); // buf == “Hello Hello “ buf.Append(KHello); // Exceeds maximum length  Panic! SDK-Doc for USER 11-panic: “This panic is raised when any operation that moves or copies data to a 16-bit variant descriptor, causes the length of that descriptor to exceed its maximum length. […]”
  • 29. Pointer Descriptors TPtr, TPtrC Andreas Jakl, 2008 29
  • 30. Pointer Descriptors Comparable to (const) char* of C Can point to text on the heap, stack or ROM Do NOT own the data they point to! Andreas Jakl, 2008 30 ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ Constant: TPtrC 0x64243184 5 ROM, heaporstack iLength(TDesC) iPtr(TPtrC) ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ TPtr Modifiable: 0x64243184 5 9 ROM, heaporstack iLength(TDesC) iPtr(TPtrC) iMaxLength(TDes)
  • 31. TPtr – Example Andreas Jakl, 2008 31 _LIT(KHelloWorld, &quot;Hello World&quot;); // Defines constant string literal // Create a constant buffer with contents of string literal TBufC&lt;13&gt; buf (KHelloWorld); // Get a pointer descriptor for the text TPtrptr = buf.Des(); // Replace the 6th char with an &apos;!&apos;, even though TBufC is actually constant! ptr[5] = &apos;!&apos;; // buf: &quot;Hello!World&quot; TBufC&lt;13&gt; (Stack) ‘H’ 11 ‘e’ ‘l’ ‘l’ ‘o’ ‘!’ ‘W’ ‘o’ ‘r’ ‘l’ ‘d’ TPtr(Stack) iLength(TDesC) 11 13 0x0303FE5C Data iscopiedto thestack KHelloWorld(ROM) iLength(TDesC) iPtr(TPtrC) iMaxLength(TDes) ‘H’ 11 ‘e’ ‘l’ ‘l’ ‘o’ ‘ ’ ‘W’ ‘o’ ‘r’ ‘l’ ‘d’ ‘’ iLength
  • 32. TPtrC: “=” versus “Set“ Andreas Jakl, 2008 32 // Create two literals _LIT(KHi, &quot;Hi&quot;); _LIT(KSymbian, &quot;Symbian&quot;); // Constant buffer descriptors, copy data from ROM to Stack TBufC&lt;10&gt; bufHi(KHi); TBufC&lt;10&gt; bufSymbian(KSymbian); // ptr1 and ptr2 point to string data owned by TBufC&apos;s TPtr ptr1(bufHi.Des()); TPtr ptr2(bufSymbian.Des()); ptr1 = ptr2; ptr1.Set(ptr2); ptr1  bufHi: “Symbian” ptr2  bufSymbian: “Symbian” bufHi: “Symbian” bufSymbian: “Symbian” … copies Data from buffer pointed toptr2 to buffer pointed to by ptr1 ptr1  bufSymbian: “Symbian” ptr2  bufSymbian: “Symbian” bufHi: “Hi” bufSymbian: “Symbian” … sets ptr1 to point to same bufferas ptr2
  • 33. Heap-Based Buffer Descriptors HBufC, RBuf Andreas Jakl, 2008 33
  • 34. Use Heap-Based Buffer when… String data is not in ROM and not on stack Stack size is a lot more limited than the heap! Length of buffer is not known at compile time, e.g. Loading strings from resource file Getting strings from UI (query dialogs, ...) Receiving response from the web Longer lifetime than creator e.g. passing to an asynchronous function Andreas Jakl, 2008 34
  • 35. Constant Heap Descriptor Comparable to (char*) malloc(length+1) of C Data is stored on the heap Andreas Jakl, 2008 35 Heap ‘H’ 5 ‘e’ ‘l’ ‘l’ ‘o’ HBufC* 0x64243184 Same layout as TBufC
  • 36. HBufC Example Andreas Jakl, 2008 36 Shorter variant: HBufC* hBuf = KHello().AllocLC(); (Function of TDesC, creates and returns heap buffer based on a copy of its own data) _LIT(KHello, &quot;Hello&quot;); // Create new HBufC with length of 5 HBufC* hBuf = HBufC::NewLC(KHello().Length()); // Copy data of KHello to heap allocated by hBuf *hBuf = KHello; […] // Do cleanup CleanupStack::PopAndDestroy(1); Heap ‘H’ 5 ‘e’ ‘l’ ‘l’ ‘o’ HBufC* 0x64243184 KHello (ROM) Data is copiedto the heap ‘H’ 5 ‘e’ ‘l’ ‘l’ ‘o’ ‘’ iLength
  • 37. Modifying HBufC Modify HBufC through apointer descriptor: Andreas Jakl, 2008 37 _LIT(KHello, &quot;Hello!&quot;); _LIT(KWorld, &quot;World!&quot;); HBufC* buf = HBufC::NewLC(KHello().Length()); *buf = KHello; // buf holds &quot;Hello!&quot;, Length = 6 // Increase heap buffer size - still holds &quot;Hello!&quot; buf = buf-&gt;ReAllocL(KHello().Length() + KWorld().Length()); // New memory area has been reserved – update reference on CleanupStack! CleanupStack::Pop(buf); CleanupStack::PushL(buf); // Create pointer descriptor based on heap descriptor TPtrptr(buf-&gt;Des()); // Length = 6, MaxLength = 12 ptr[KHello().Length() - 1] = &apos; &apos;; // buf holds &quot;Hello &quot; ptr.Append(KWorld); // buf holds &quot;Hello World!&quot; CleanupStack::PopAndDestroy(buf); To access the data of an HBufC* (e.g. to use it for a TDesC&-parameter), you can write *hptr instead of creating a TPtrC with hptr-&gt;Des();
  • 38. HBufC – Memory View Andreas Jakl, 2008 38 TPtr 12 12 0x02704174 iLength(TDesC) iPtr(TPtrC) iMaxLength(TDes) Heap HBufC* 0x02704174 ‘H’ 12 ‘e’ ‘l’ ‘l’ ‘o’ ‘ ’ ‘W’ ‘o’ ‘r’ ‘l’ ‘d’ ‘!’
  • 39. RBuf Available since Symbian OS 8 Owns and points to modifiable string data on the heap Behaves like a handle to a resource (Close() to free) Resize possible, but has to be done manually Similar to: TPtr, but owns data it points to HBufC, but easier to use Can be wrapper for HBufC-Object Andreas Jakl, 2008 39
  • 40. RBuf – Memory Layout Andreas Jakl, 2008 40 Heap RBuf points directly to its owned data: ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ RBuf 0x64243184 5 9 iLength(TDesC) *iEPtrType(RBuf) iMaxLength(TDes) C++ Union (only one of both variables are used, depending on type) Heap RBuf points to owned HBufC: ‘H’ 5 ‘e’ ‘l’ ‘l’ ‘o’ RBuf 5 9 0x64243184 = contents of old HBufC iLength(TDesC) *iEBufCPtrType(RBuf) iMaxLength(TDes)
  • 41. RBuf – Construction Creating an RBuf: Allocate own memoryRBufbuf;buf.CreateL(TIntaMaxLength); // Create RBuf with max. lengthor: buf.CreateL(constTDesC &aDes); // Copy data to own memory Transfer ownership of HBufC(wrapper for HBufC)RBufbuf(HBufC* aHBuf); // Transfer ownership of HBufC-dataor: buf.Assign(HBufC* aHBuf); Transfer ownership of existing memory areabuf.Assign(TUint16 *aHeapCell, TIntaMaxLength); Andreas Jakl, 2008 41
  • 42. Using the RBuf Andreas Jakl, 2008 42 _LIT(KHelloWorld, &quot;Hello World!&quot;); RBufheapBuf; // Create RBuf, copy contents of KHelloWorld heapBuf.CreateL(KHelloWorld); // Make sure Close() is called on RBuf in case of Leave heapBuf.CleanupClosePushL(); […] // Calls Close() on the RBuf CleanupStack::PopAndDestroy(); Directly points to string data(ASCII 72 = “H” of “Hello World”)
  • 43. RBuf – Memory view Andreas Jakl, 2008 43 Heap 0x027d4174 Hello World Stack Data iscopiedto theheap *iEPtrType ROM 11 iMaxLength RBufheapBuf Hello World 11 11 iLength
  • 44. RBuf and HBufC Andreas Jakl, 2008 44 _LIT(KHelloWorld, &quot;Hello World!&quot;); HBufC* hptr; // Create heap descriptor which can hold up to 15 items. Current lenght = 0 hptr = HBufC::NewL(15); // Assigns data to heap descriptor. Current length = 12 *hptr = KHelloWorld; // Ownership of heap descriptor passed to RBuf RBufbuf(hptr); // Close buffer and free resources - do not delete HBuf! buf.Close();
  • 45. RBuf & HBufC – Memory View Andreas Jakl, 2008 45 Heap 0x027d4174 Hello World 11 Stack Data is copiedto the heap *iEBufCPtrType 11 iMaxLength RBuf buf ROM 11 iLength Hello World 11 HBufC* hptr ... don’t delete twice, now the RBuf owns the memory!
  • 46. One more Example It’s recommended to use RBuf instead of HBufC* Andreas Jakl, 2008 46 // Function returns HBufC*, we use RBuf to wrap it  simpler handling for us! RBufresString (iEikonEnv-&gt;AllocReadResourceL(someResourceId)); // Leaving functions ahead, so push the RBuf on the CleanupStack resString.CleanupClosePushL(); // Use modifiable descriptor to append new text... resString.ReAllocL(resString.Length() + 4); resString.Append(_L(&quot;-Foo&quot;)); […] // Assign text to a label, resString is a TDesC as well, so it works without changes! SetLabelL(ELabel, resString); CleanupStack::PopAndDestroy(); // calls resString.Close();
  • 47. RBuf vs. HBufC? Theoretically: Use HBufC for constant data Use RBuf for modifiable data However: As RBuf is a lot easier to use, consider always using RBuf Most Symbian OS APIs operate using HBufC Either also use HBufC for these cases Or wrap HBufC with RBuf Andreas Jakl, 2008 47
  • 48. Summary Abstract (TDes, TDesC) Base class of other descriptors Cannot be instantiated Used for function parameters Literal (TLitC _LIT()) Used to store non-modifiable, literal strings in code Buffer (TBuf, TBufC) Data is stored on the stack Size specified at compile time Heap (HBufC, RBuf) Data stored on the heap Size specified at run-time Pointer (TPtr, TPtrC) References data stored outside the class Andreas Jakl, 2008 48
  • 49. Lots of types? Decide: Andreas Jakl, 2008 49 Is the descriptor modifiable? YES NO Has the memory for the descriptor data already been allocated elsewhere (heap or stack)? Has the memory for the descriptor data already been allocated elsewhere (heap or stack)? YES YES TPtr TPtrC Will the memory allocated be stack- or heap-based? Will the memory allocated be stack- or heap-based? HEAP HEAP RBuf HBufC STACK STACK TBuf TBufC
  • 50. Quiz Andreas Jakl, 2008 50 TDesC::AllocLC()“Creates a new 16-bit heap descriptor, initializes it with a copy of this descriptor&apos;s data, and puts a pointer to the descriptor onto the cleanup stack.” Assign variables to the memory view: _LIT(KHello1, “Hello”); TPtrChello2 (KHello1); TBufC&lt;5&gt; hello3(KHello1); HBufC* hello4 = KHello1().AllocLC(); Heap hello 5 Stack …? hello …? 5 ROM …? …? hello 5 5
  • 51. Quiz – Solution Assign variables to the memory view: Andreas Jakl, 2008 51 _LIT(KHello1, “Hello”); TPtrChello2 (KHello1); TBufC&lt;5&gt; hello3(KHello1); HBufC* hello4 = KHello1().AllocLC(); Heap hello 5 Stack hello4 hello hello3 5 ROM KHello1 hello2 hello 5 5
  • 52. Using Descriptors in Functions The daily life of a Symbian OS developer … Andreas Jakl, 2008 52
  • 53. Function Arguments void MyFunction(TBuf&lt;8&gt;aText) What if caller uses an RBuf, TPtr or TBuf&lt;9&gt;?  References for base classes (TDes&, const TDesC&) should always be used void SomeFunction(const TDesC& aReadOnlyDescriptor, TDes& aReadWriteDescriptor); Andreas Jakl, 2008 53
  • 54. Example TInterrorCode = RFile.Read(TDes8& aDes); Function can find out max. length (aDes.MaxLength()) Caller can get number of bytes read: des.Length() Compare that to the Win32-API … BOOL ReadFile(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped); Andreas Jakl, 2008 54
  • 55. Return HBufC* Make sure calling function knows it takes ownership! Don’t forget the cleanup stack if it is necessary! Andreas Jakl, 2008 55 HBufC* CreateSomeDescriptorL() { _LIT(KBert, &quot;bert&quot;); // Createheap-baseddescriptorHBufC* newBert = KBert().AllocL(); // Return ourHBufC*return (newBert);} HBufC* CreateSomeDescriptorLC() { _LIT(KBert, &quot;bert&quot;); HBufC* newBert = KBert().AllocLC(); return (newBert);}
  • 56. Better: RBuf Creating a specific RBuf in a function: Andreas Jakl, 2008 56 RBufmyBuf; myBuf.CleanupClosePushL(); GetSomeDataL(myBuf); console-&gt;Printf(myBuf); CleanupStack::PopAndDestroy(myBuf); voidGetSomeDataL(RBuf&aBuf) { _LIT(KHello, &quot;Hello&quot;); // Allocatememory and copy „Hello“ to RBufaBuf.CreateL(KHello); }
  • 57. Returning a TPtrC What’s wrong with the following code? Andreas Jakl, 2008 57 TPtrCGetText() { // CreateTBufCbased on literaldescriptor _LIT(KMyText, &quot;My Text&quot;);TBufC&lt;7&gt; buf(KMyText()); // Create a TPtrCbased on bufTPtrCbufPtr(buf); // Return theTPtrCreturn (bufPtr);}
  • 58. Returning a TPtrC – Solution bufPtr does not own the data buf is a local variable and ceases to exist when function is left  Target of bufPtris no longer valid! Andreas Jakl, 2008 58 TPtrCGetText() { // CreateTBufCbased on literaldescriptor _LIT(KMyText, &quot;My Text&quot;);TBufC&lt;7&gt; buf(KMyText()); // Create a TPtrCbased on bufTPtrCbufPtr(buf); // Return theTPtrCreturn (bufPtr);}
  • 59. Unicode and Binary Painless conversion Andreas Jakl, 2008 59 Conversion is more important than you might think: e.g. socket communication is 8 bit!
  • 60. Converting from Unicode Simple conversion by stripping alternate characters: Andreas Jakl, 2008 60 TBuf8&lt;3&gt; cat(_L8(&quot;cat&quot;)); // _L used for simplicity TBuf16&lt;3&gt; dog(_L16(&quot;dog&quot;)); cat.Copy(dog); // cat now contains &quot;dog&quot; TDes8 cat TDes16 dog cat.Copy(dog); Strips -padding TDes8 cat
  • 61. Converting to Unicode Simple conversion by padding each character with trailing zero: Andreas Jakl, 2008 61 TBuf8&lt;5&gt; small(_L8(&quot;small&quot;)); TBuf16&lt;5&gt; large(_L16(&quot;large&quot;)); large.Copy(small); // large now contains &quot;small&quot; TDes8 small TDes16 large large.Copy(small); Adds -padding TDes8 large
  • 62. Proper Conversion Use conversion library (charconv.lib) Requires: #include &lt;charconv.h&gt; #include &lt;utf.h&gt; Library: charconv.lib Andreas Jakl, 2008 62 // Russian text saved as UTF8 _LIT8(KString8, &quot;Телефон&quot;); TBufC8&lt;20&gt; source8(KString8); // Create target 16 bit buffer for Unicode string RBuf target16; target16.CreateL(source8.Length()); target16.CleanupClosePushL(); // Use conversion library to convert from UTF8 to Unicode CnvUtfConverter::ConvertToUnicodeFromUtf8(target16, source8); // Print results and do cleanup console-&gt;Printf(target16); CleanupStack::PopAndDestroy(1);
  • 63. More about Descriptor Usage Other things you should know Andreas Jakl, 2008 63
  • 64. TLex General string-parsing functions suitable for numeric format conversions and syntactical-element parsing e.g. parse GPS data (NMEA) Powerful, but very complex More information at: http://descriptors.blogspot.com/2005/08/35-how-do-i-use-tlex.html Andreas Jakl, 2008 64
  • 65. Conversion String  Number String  Number: Provided by TDes Number  String: Simple use of TLex Andreas Jakl, 2008 65 _LIT(KTestString1, &quot;54321&quot;); // Convertstring to number TLexlex(KTestString1()); TIntvalue = 0; User::LeaveIfError(lex.Val(value)); ASSERT(value==54321); // Convertnumber to string TBuf&lt;8&gt; buf; buf.Num(value);
  • 66. Formatting Text Integrate variables into text Appending text or numbers Simplest way: use the AppendNum()-function defined in TDes Make sure the target descriptor is large enough! Andreas Jakl, 2008 66 _LIT(KText, &quot;Number &quot;); TBuf&lt;15&gt; myText(KText); myText.AppendNum(1); console-&gt;Printf(myText);
  • 67. Formatting Text Format strings Use placeholders for elements Full syntax in the SDK help: » Symbian OS vXX » Symbian OS guide » Base » Using User Library (E32) » Buffers and Strings » Using Descriptors » How to Use Descriptors » Format string syntax Console (format syntax directly integrated) Andreas Jakl, 2008 67 _LIT(KName, &quot;Joe Bloggs&quot;); _LIT(KText, &quot;Name: %S, Age: %d&quot;); TBuf&lt;30&gt; myText; myText.Format(KText, &KName, 27); console-&gt;Printf(myText); TInt result = 0; _LIT(KFormatCompare1, &quot;Compare() using str1 and str2 = %d&quot;); console-&gt;Printf(KFormatCompare1, result);
  • 68. TFileName Definition of TFileName(e32const.h / e32cmn.h): const TIntKMaxFileName=0x100; // = Decimal 256typedefTBuf&lt;KMaxFileName&gt; TFileName; Required size on the stack: 520 bytes 2 x 256 data bytes (Unicode), 8 bytes for descriptor obj. Standard stack size in Symbian OS: 8kB Therefore: Do not allocate TFileName on the stack (however: instance var. of CBase-class is on the heap!) Do not pass it by value! … use RBuf instead! Andreas Jakl, 2008 68
  • 69. Test your Knowledge Did you understand everything? Andreas Jakl, 2008 69
  • 70. ASD-like Question – Easy Which of the following statements about descriptors are correct? A. All descriptor classes, except RBuf, derive from TDesC. B. The first four bytes of a descriptor store the descriptor’s length and type. C. The descriptor classes do not use virtual functions to avoid the overhead of a virtual function pointer in every descriptor object. D. Modifiable descriptors use the NULL terminator to indicate the end of the descriptor data. E. All descriptors have “wide”, 16-bit characters. Andreas Jakl, 2008 70 Copyright Meme Education, 2006 http://www.meme-education.com/
  • 71. Solution A.Incorrect. RBuf derives from TDesC as well. B. Correct. C. Correct. D. Incorrect. The NULL terminator is not used by descriptors. Only Literals are -terminated because of the C++-compiler, but this is hidden from the developers. E. Incorrect. Descriptors are available as 8 bit and 16 bit variants. Andreas Jakl, 2008 71
  • 72. ASD-like Question – Medium Andreas Jakl, 2008 72 _LIT(KHello, &quot;Hello!&quot;); TBufC&lt;6&gt; hello(KHello); _LIT(KBye, &quot;Goodbye!&quot;); TBufC&lt;8&gt; bye(KBye); TPtrfoo(hello.Des()); TIntlen = foo.Length(); TIntmaxLen = foo.MaxLength(); TPtr bar(bye.Des()); foo.Set(bar); len = foo.Length(); maxLen = foo.MaxLength(); foo.Copy(KHello); len = foo.Length(); maxLen = foo.MaxLength(); Which of the following statements are correct? A. After executing line 2, a call to hello.MaxLength() returns 6. B. After executing line 6, len = 6; After line 7, maxLen = 6. C. After executing line 9, hello contains “Goodbye!”. D. After executing line 10, len = 8; After line 11, maxLen = 8. E. After executing line 13, len = 6; After line 14, maxLen = 6. Copyright Meme Education, 2006 http://www.meme-education.com/
  • 73. Solution A.Incorrect. TBufC does not have a MaxLength()-function. This is defined in TDes base class. B. Correct. C. Incorrect. After the Set()-function, the foo-TPtr points to the same memory as the bar-TPtr. The buffers are not altered however. This would be the case with =. D. Correct. E. Incorrect. MaxLength = 8, as foo is now pointing to the buffer of the bar-TPtr, which is the bye-TBuf with a max. length of 8. Andreas Jakl, 2008 73
  • 74. ASD-like Question – Hard Andreas Jakl, 2008 74 Which of the following uses of a descriptor should be reconsidered? A. USE 1 B. USE 2 C. USE 3 D. USE 4 E. USE 5 classCTestClass : publicCBase { public: staticCTestClass* NewL(constTFileNameaFilename); // USE 1 public: inlineconstTDesC& FileName() {return (iFileName);}; // USE 2 voidSendDataL(constTBufC&lt;20&gt;& aData); // USE 3 voidReceiveDataL(TDes& aData); // USE 4 private: // Construction code omitted for clarity private: TFileNameiFileName; // USE 5 }; Copyright Meme Education, 2006 http://www.meme-education.com/
  • 75. Solution A.Reconsider. TFileName should be passed by reference, not by value. B. OK. C. Reconsider. Use generic parameter instead (const TDesC& aData). D. OK. E. OK. Andreas Jakl, 2008 75
  • 76. Resources Famous blogs with answers to common problems: http://descriptors.blogspot.com/ http://descriptor-tips.blogspot.com/ Detailed information in the books: Symbian OS C++ for Mobile Phones, Vol. 3 Symbian OS Explained Andreas Jakl, 2008 76 Unfortunately this book do not cover the most recent addition to Symbian OS, the RBuf. Apart from this minor disadvantage, it is very useful.
  • 77. Thanks for your attention That’s it! Andreas Jakl, 2008 77