Symbian Development Summary-implementation and principles of RTTI

Source: Internet
Author: User

This article will focus on the implementation and principles of RTTI, a summary of Symbian development. RTTI runtime type information) is one of the features widely supported by modern advanced programming languages. However, SymbianOSC ++ does not support this feature, this causes great inconvenience for developers or code porting from Win32 and JAVA to Symbian. This article will solve this problem.

Symbian Development Summary-implementation and principles of RTTI

I. Preface

RTTI runtime type information) is one of the features widely supported by modern advanced programming languages. For example, "aisA" in C # And "ainstanceofA" in JAVA belong to the RTTI category. However, SymbianOSC ++ does not support this feature, which leads to great inconvenience caused by migration from Win32 and JAVA to Symbian developers or code. This article will solve this problem.

2. What is RTTI?

In Symbian development, RTTI indicates "Run-TimeTypeIdentification" or "Run-TimeTypeInformation )", the program can use pointers or references of the base class to check the actual derived types of these pointers or referenced objects.

Different applications require different RTTI ranges. The simplest RTTI includes:

Classidenication ication-including the class name or ID.

Inheritancerelationship: supports downwardcasting during execution, that is, dynamiccasting ).
  
Iii. RTTI in Symbian Development

Due to the limitations of the Symbian System and its running hardware environment, the program of the Symbian system cannot be completely as the C ++ program is designed at will. SymbianOSC ++ does not provide RTTI support. Therefore, dynamic_cast <>, typeid (), and type_info in Standard C ++ are not supported.

Iv. porting the MFC code to implement RTTI

In Symbian development, the VC ++ compiler supports RTTI only from version 4.0, but MFC4.x does not use the compiler capability to support RTTI. MFC has its own set of methods that have been used for a long time since 1.0 ). Here, we use the RTTI code in MFC to support SymbianOSC ++ RTTI.

Regarding the Implementation Principles of RTTI in MFC, Hou Jie's in-depth introduction to MFC has already elaborated in detail, the basic principle is to use several special macros to manually determine an Object Inheritance link linked list during compilation. The detailed principle is not described here.

We transplanted the RTTI Code implemented by MFC in VC ++ 9.0, instead of using the simulation code provided by Hou Jie in "getting down to MFC. Because Hou Jie's code contains a lot of "writable static data", it cannot be used in SymbianDLL or 2nd apps. However, the MFC code in VC ++ 9.0 does not have the above problems, so it can be used in any Symbian code.

The package contains two files: Rtti. h and Rtti. cpp. After adding these two files to the project, we started to design and implement the RTTI class:

 1. Class declaration:

Rtti. the CRttiBase in the h header file is a basic class with the RTTI feature. This class is equivalent to the CObject in MFC. It inherits from CBase. All classes to implement the RTTI feature must be derived from this class, add a special macro to the Declaration:
 

 
 
  1. class CMyClass : public CRttiBase    
  2.  {    
  3.  DECLARE_DYNAMIC(CMyClass)    
  4.  ...    
  5. };    
  6.  

Note: The first parameter in the macro DECLARE_DYNAMIC is the class name of the current class: CMyClass.

Declare that the second class inherits from CMyClass. Similarly, add the DECLARE_DYNAMIC macro:
 

 
 
  1. 1 class CMyClass1 : public CMyClass  
  2. 2 {  
  3. 3 DECLARE_DYNAMIC(CMyClass1)  
  4. 4 ...  
  5. 5 };  

Note: The implementation of the RTTI subclass inherits from the parent class, and the parent class must inherit from the CRttiBase.

 2. Class implementation

Add the following two lines of code to the implementation source files of CMyClass and CMyClass1:
 

 
 
  1. 1 IMPLEMENT_DYNAMIC(CMyClass, CRttiBase);  
  2. 2 IMPLEMENT_DYNAMIC(CMyClass1, CMyClass);  

The first parameter in macro IMPLEMENT_DYNAMIC is the current Child type, and the second parameter is the direct parent type. For example, the direct parent class of CMyClass is CRttiBase, and the direct parent class of CMyClass1 is CMyClass.

3. Use RTTI features

Through the above two simple steps, we can use the RTTI feature, the complete code:
 

 
 
  1. 1 class CMyClass : CRttiBase  
  2.  2 {  
  3.  3 DECLARE_DYNAMIC(CMyClass)  
  4.  4 };  
  5.  5   
  6.  6 class CMyClass1 : CMyClass  
  7.  7 {  
  8.  8 DECLARE_DYNAMIC(CMyClass1)  
  9.  9 };  
  10. 10   
  11. 11 class CMyClass2 : CRttiBase  
  12. 12 {  
  13. 13 DECLARE_DYNAMIC(CMyClass2)  
  14. 14 };  
  15. 15   
  16. 16 IMPLEMENT_DYNAMIC(CMyClass, CRttiBase);  
  17. 17 IMPLEMENT_DYNAMIC(CMyClass1, CMyClass);  
  18. 18 IMPLEMENT_DYNAMIC(CMyClass2, CRttiBase);  
  19. 19   
  20. 20 LOCAL_C void MainL()  
  21. 21 {  
  22. 22 CMyClass1* mc1 = new (ELeave) CMyClass1;  
  23. 23 TBool a = mc1->IsKindOf(RUNTIME_CLASS(CMyClass));  
  24. 24 TBool b = mc1->IsKindOf(RUNTIME_CLASS(CRttiBase));  
  25. 25 TBool c = mc1->IsKindOf(RUNTIME_CLASS(CMyClass2));  
  26. 26 }  

From the code, we can see that the parent class of CMyClass1 is CMyClass, the parent class of CMyClass is RTTI base class CRttiBase, and the base class of CMyClass2 is CRttiBase. CMyClass1 and CMyClass2 are not inherited.

Therefore, for lines 23rd to 25, the values of abc are true, true, and false.

CRttiBase: The IsKindOf method is similar to the "is" keyword in C # And the "instanceof" keyword in JAVA. It imports the runtime information of a class, the macro "RUNTIME_CLASS" obtains the runtime information "CRuntimeClass" of a class ".

4. runtime information

The "runtime information" structure CRuntimeClass saves the class information during creation for viewing during the program running, including the class name, class size, and parent class information. This information is included in the macro IMPLEMENT_DYNAMIC and determined during program Compilation:
 

 
 
  1. 1 struct CRuntimeClass  
  2.  2 {  
  3.  3 const char* iClassName;  
  4.  4 TInt iObjectSize;  
  5.  5 TUint iSchema;   
  6.  6 CRttiBase* (*iCreateObjectProc)();   
  7.  7 CRuntimeClass* iBaseClass;  
  8.  8 CRttiBase* CreateObject();  
  9.  9 TBool IsDerivedFrom(const CRuntimeClass* aBaseClass) const;  
  10. 10 CRuntimeClass* iNextClass;  
  11. 11 };  

Note: CRuntimeClass can be understood as System. Type in C.

5. Get runtime information of classes and objects in Symbian Development

Use the macro RUNTIME_CLASS to obtain the class runtime information, for example:


CRuntimeClass * classType = RUNTIME_CLASS (CMyClass );

Note: The above code can be understood as the "TypeclassType = typeof (CTestClass);" method in C # to obtain the type information of the class.

Use the CRttiBase: GetRuntimeClass () method to obtain the object runtime information, for example:


CMyClass1 * mc1 = new (ELeave) CMyClass1;
CRuntimeClass * rc = mc1-> GetRuntimeClass ();

Note: The above code can be understood as the "TypeclassType = theClass. GetType ();" method in C # To obtain the object type information.

Both methods return CRuntimeClass *.

6. dynamically create an object through runtime information

You may notice that CRuntimeClass has a method called "CreateObject", which allows you to dynamically create objects through runtime information. This is often necessary to implement complex functions. For example:

There is a factory that can produce different parts, and the types of parts that can be produced are diverse.

Before RTTI is implemented, we may write a large case statement in the factory method to identify different part types and call constructors of different classes.

After RTTI is implemented, we only need to keep a hash table between the part type and CRuntimeClass, input the part type to the hash table in the factory method, find the CRuntimeClass, and call CRuntimeClass :: the CreateObject () method.

To dynamically create an object, you must change DECLARE_DYNAMIC in the function declaration to DECLARE_DYNCREATE and change IMPLEMENT_DYNAMIC to IMPLEMENT_DYNCREATE. For example:
 

 
 
  1. 1 class CMyClass : CRttiBase  
  2. 2 {  
  3. 3 DECLARE_DYNCREATE(CMyClass)  
  4. 4 };  
  5. 5   
  6. 6 IMPLEMENT_DYNCREATE(CMyClass, CRttiBase);  

In this way, the type information of CMyClass can provide the function of dynamically creating objects.

5. Notes

CRttiBase is the parent class that supports the RTTI feature. The system itself does not support the RTTI feature. Therefore, the RTTI class must be inherited from CRttiBase directly or indirectly, which usually has a great impact on our design. For example, if a class is an active object and inherits from CActive, it must implement the RTTI feature. Obviously, the following statement is incorrect because both CActive and CRttiBase inherit from CBase:

Class CMyActiveObject: public CActive, public CRttiBase {...}

There are two solutions:

Uses Wrapper mode to encapsulate CActive and export Interfaces

By modifying rtti. h, so that CRttiBase does not inherit from CBase. Each RTTI-based class manually specifies the base class CBase or others, and then uses C ++ multi-inheritance to support class design.

Vi. References

In-depth introduction to MFC and Hou Jie

How to determine the object type RTTI at runtime)

SymbianOSC ++ efficient programming

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.