Basic concepts of Symbian Development

Source: Internet
Author: User

This article focuses on the basic concepts of Symbian in Symbian development, including data types, descriptors, and error handling, I believe that you will have a deep understanding of the concept of Symbian development through the study in this article.

Learn the basic concepts of Symbian Development

I. Basic Data Types

This is relatively simple. It is all written in e32def. h. That is to say, pay attention to the following types, and write them later. The meaning is also clear.
 

 
  1. typedefvoidTAny;  
  2.  
  3. typedefsignedcharTInt8;  
  4. typedefunsignedcharTUint8;  
  5. typedefshortintTInt16;  
  6. typedefunsignedshortintTUint16;  
  7. typedeflongintTInt32;  
  8. typedefunsignedlongintTUint32;  
  9. typedefsignedintTInt;  
  10. typedefunsignedintTUint;  
  11.  
  12. typedeffloatTReal32;  
  13. typedefdoubleTReal64;  
  14. typedefdoubleTReal;  
  15.  
  16. typedefunsignedcharTText8;  
  17. typedefunsignedshortintTText16;  
  18.  
  19. typedefintTBool;  
  20.  
  21. typedefTUint32TLinAddr;//Definesalinear(virtual)addresstype.  
  22.  

Ii. descriptor in Symbian Development

This is interesting. To put it bluntly, there are two things we used before: String and malloc. However, in symbian, descriptors are divided into three types: Buffer descriptors, pointer descriptors, and heap descriptors.

A. Buffer descriptor: TBuf and TBufC

Similar to char [], that is, it is a string representation, such as TBuf <20> str; which is basically the same as charstr [20.
However, the descriptor can contain some methods, just as we use the String mainly for its convenient String processing method.

B. pointer descriptors: TPtr and TPtrC

It is similar to char *, that is, it is a character byte.

C. Heap descriptor: HBufC

Similar to a space we use malloc to open up, such as: HBufC * buf = HBufC: NewL (128); and byte * buf = (byte *) malloc (128 ); the meaning is basically the same.

There is also an abstract descriptor TDes and TDesC, which are the base classes of other descriptors.
C after all descriptor names indicates that it is an unchangeable descriptor. In other words, all descriptors without C are added with some modification functions on the basis of C descriptors.

Remember that descriptors bring us convenience.
For example, these functions:

Length () is used to obtain the true Length (number of elements) of a string, while Size () is used to obtain the number of bytes it occupies.
Left ()/Right ()/Mid () is a function used to obtain substrings.
Compare () Comparison function.
Locate ()/LocateReverse ()/Find ()/Match () can be used to Find substrings or characters.
Copy ()/Delte ()/Insert ()/Replace ()/Trim ()/Append ()/Zero () can be used to modify the descriptor content.
Num () can convert a value into a string.
Format () is similar to sprintf and is commonly used to Format the output. However, other functions of the same system may be more convenient, such as AppendFormat ()/AppendNum.


◆ For the heap descriptor in Symbian development, note three points:

1. It is Des (). Because HBufC carries C as a non-modifiable descriptor, if we want to modify it, we need to use buf. Des () to obtain a pointer descriptor pointing to it. The following sentence:
_ Partition (KHello, "hellochina ");

HBufC * buf = HBufC: NewL (64 );
* Buf = KHello;

TPtrp = buf-> Des ();
P [0] = 'H ';

2. There is an AllocLC () in TDesC that can allocate memory to obtain the HBufC descriptor, which is the same as HBufC: NewL. In addition, ReAllocL can be used in HBufC to re-allocate memory, just like realloc.

3. Differentiate the meanings of the two sentences:
TPtrp = buf-> Des ();
TPtrp (buf-> Des ());

The first sentence is to obtain the maximum length of a pointer p based on the actual length of the buf, that is, the actual length of the buf at this time 11 ), the second sentence uses the buf information to construct p, so its maximum length should be 64, although the current actual length is also 11.

Another macro related to the descriptor is very common. Note: _ constant (constant name, string value ). For example:
_ Round (KSayHelloSTR, "Helloworld .");

The _ L macro is not recommended because of its low efficiency.

Here, KSayHelloSTR is another type of Descriptor TLitC. TLitC provides two operators:

& The operator can get its constTDesC *, while the () operator gets its constTDesC &.
KSayHelloSTR (). Length (); // obtain the Length of the string.

TBuf <256> str;
Str. Format (KFormatSTR, & KSayHelloSTR); // get the reference address of this string

For more information about the descriptor, see hfile: e32des16. h.

Iii. handle errors in Symbian Development

There are three concepts: Leave. The most common thing is to reload new (ELeave) to the new operator, indicating that the structure at this time will produce insufficient memory.
With new (ELeave), we can use it directly after a new object without having to determine whether the construction is successful, because if it fails, an error will be thrown and the upper layer will be returned.

The second concept works with Leave and has a macro TRAPD (error, Func) similar to try... catch ....
That is, if Leave occurs in the Func function, the error code is returned. Generally, the User: Leave () can be used in the program to throw an error, similar to the thrownewException operation.

The third concept is also the most commonly used, namely the use of CleanupStack.
Instead of using the TRAPD macro, we can push the pointer to cleanupstack before Leave may occur, and pop it out after correct completion. If it fails, the system will help us pin things in cleanupstack. This makes it much easier.

The general code is similar:
 

 
  1. CMyCls*mc=new(ELeave)CMyCls;  
  2. CleanupStack::PushL(mc);  
  3. me->doSth1L();  
  4. me->doSth2L();  
  5. CleanupStack::PopAndDestroy();  

We usually use cleanupstack to protect local variables, but we cannot do this for class member variables. Otherwise, they will be destroyed twice, resulting in a serious error ).
In addition, when the pop is out of the range, only a few should be played. If other content is accidentally popped up, it may also cause serious errors. Therefore, Pop has an overloaded Pop (3, pA), which indicates that three objects are popped up and the final output stack is not pA.

Iv. Two-Phase Structure

Generally, Symbian classes in Symbian development do not provide public constructor.) because it advocates the so-called "two-phase constructor" method. Defined:

A) The constructor is protected or privted and cannot contain any operation that causes Leave.
B) implement two static functions NewL and NewLC to replace the constructor.
C) Implement a ContructL function to construct the second stage. In fact, most initialization work can be done here.

The Code constructed in the two phases is quite formatted. For example, the Code of two static functions NewL and NewLC is generally like this:
 

 
  1. CMyClass*CMyClass::NewL()  
  2. {  
  3. CMyClass*self=NewLC();  
  4. CleanupStack::Pop();  
  5. returnself;  
  6. }  
  7. CMyClass*CMyClass:NewLC()  
  8. {  
  9. CMyClass*self=new(ELeave)CMyClass;  
  10. CleanupStack::PushL(self);  
  11. self->ContructL();  
  12. returnself;  
  13. }  

In ContructL, you can perform operations in real constructors, such as allocating memory and creating forms.

V. Naming rules for Symbian Development

This is also an area to be aware of. It is listed as follows:

The class name prefixes include T, C, M, and R, which are described as follows:
T indicates the basic class, which is located in the stack and is considered as a structure.
C Indicates the regular class, which inherits from CBase. This is the concept of C ++ standard class, so the structure should be analyzed.
M indicates an interface. It is easy to understand and must contain pure virtual functions.
R indicates a system resource, such as a file or network. Therefore, it must have a Close relationship with Open.

Also, start with K indicates constant, and start with E Indicates enumeration.
The parameter starts with a and the class member variable starts with I. This rule can be seen in automatically generated code.

In addition, the name of the function is also exquisite, but it is not the beginning but the end: Like L, it indicates that there may be Leave, LC indicates that not only may have Leave, but it will be automatically placed in CleanupStack.
There are two second-stage constructor and three function names are fixed: NewL, NewLC, and ContructL.

-- Kaixuan 'scnblogs
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.