Basic concepts of Symbian

Source: Internet
Author: User
Symbian Study Notes (2)-basic concepts: the first time you open the program source code of symbianc ++, the first thought is "is this C/C ++? Why ".
Reading the code is just a bit clear. Actually, it is nothing more than typedef. Symbian has defined more things than brew.

This is the foundation for future work, so it is easy to take notes.

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. Typedef void Tany;

Typedef signed Char tint8;
Typedef unsigned char tuint8;
Typedef short int tint16;
Typedef unsigned short int tuint16;
Typedef long int tint32;
Typedef unsigned long int tuint32;
Typedef signed int tint;
Typedef unsigned int tuint;

Typedef float treal32;
Typedef double treal64;
Typedef double treal;

Typedef unsigned char ttext8;
Typedef unsigned short int ttext16;

Typedef int tbool;

Typedef tuint32 tlinaddr; // defines a linear (virtual) Address type.

Ii. Descriptor

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
It is similar to Char [], that is, it is a string representation, for example, tbuf <20> STR; it is basically the same as the char STR [20] We wrote previously.
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
Similar to char *, that is, it is an alternative representation of a character (byte) pointer.

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.

Note three points for the heap descriptor:

First, 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. _ Blank (khello, "Hello China ");

Hbufc * Buf = hbufc: newl (64 );
* Buf = khello;

Tptr P = Buf-> des ();
P [0] = 'H ';

Second, 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.

The third is to differentiate the meanings of the two sentences: tptr P = Buf-> des ();
Tptr P (BUF-> des ());

The first sentence is to get a pointer based on the actual length of the Buf (the maximum length of P is the same as the actual length of the Buf, that is, the actual length of the Buf is 11 at this time ), 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: _ response (ksayhellostr, "Hello world .");

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 const tdesc *, while the () operator gets its const tdesc &. 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. Error Handling

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, user: Leave () can be used in the program to throw an error, similar to the throw new exception 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 to: cmycls * MC = new (eleave) cmycls;
Cleanupstack: pushl (MC );
Me-> dosth1l ();
Me-> dosth2l ();
Cleanupstack: popanddestroy ();

We usually use cleanupstack to protect local variables, but we cannot do this for class member variables (otherwise it will be destroyed twice and cause 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 do not provide public Constructors (this is not recommended) because they advocate 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: cmyclass * cmyclass: newl ()
...{
Cmyclass * Self = newlc ();
Cleanupstack: Pop ();
Return self;
}
Cmyclass * cmyclass: newlc ()
...{
Cmyclass * Self = new (eleave) cmyclass;
Cleanupstack: pushl (Self );
Self-> contructl ();
Return self;
}

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

V. Naming rules

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.

Reference from: http://www.sf.org.cn/Article/lumen/200803/20902.html

 

Related Article

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.