Rtti implements C ++ reflection

Source: Internet
Author: User
Standard C ++ rtti-similar MFC implementation-only two macros are used to implement "Object creation Based on Object Name"

By jerry cat
Time: 2006/05/30
Link: http://www.cppblog.com/jerysun0818/archive/2006/05/30/7895.html

1. Starting from:

C ++ rtti lacks some runtime information, even c ++ 98, which cannot be used to directly create Objects Based on Object names. to achieve serialization, MFC constructs its own rtti information from the ground up and defines a set of macros. for details, refer to the implementation of the following classes and Macros in the MFC source generation:

Cruntimeclass, cobject, declare_dynamic, implement_dynamic

2. Requirements:
I am writing a tool for integration testing. I have a configuration file for which cases need to be tested. The similar structure is as follows:

<Ut>
<Case name = "case1"/>
<Case name = "case2"/>
</UT>

Therefore, when the test tool imports and modifies the configuration information, it must be able to create the corresponding case object according to "case1", "case2" and other names. Obviously, C ++ does not provide similar functions.

3. Implementation:
Following the implementation of MFC serialize, define a base class and two macros. the object to be dynamically created is derived from the base class, and two macros are registered to the object Factory (factory object). Then, the object factory can be used to create an object based on the object name.

Definition of base classes and macros:
Class cutobject
{
Public:
Virtual ~ Cutobject (){}
Virtual void setutname (cfstring name );
Virtual cfstring getutname ();
Virtual cutobject * clone () {return NULL ;}

PRIVATE:
Cfstring m_strutname;
};

# Define declare_utobject (classname )/
Public :/
Virtual cutobject * clone ()/
{Return New classname ();}/
Static cutobject * Createobject ()/
{Return New classname ();}/
Static bool registerobject (cfstring utname )/
{/
Classname * pobj = (classname *) Createobject ();/
Cutfactory * pfactory = cutfactory: instance ();/
Pfactory-> registerobject (utname, pobj );/
Return true ;/
}

# Define register_utobject (utname, classname) bool B # classname = classname: registerobject (utname );

The class factory is a standard Singleton mode that provides two methods-registerobject and Createobject:
Class cutfactory
{
Public:
Static cutfactory * instance ();

PRIVATE:
Cutfactory ();
Cutfactory (const cutfactory & Other ){}
Cutfactory & operator = (const cutfactory & Other ){}

Public:
Bool registerobject (cfstring name, cutobject * pobj );
Cutobject * Createobject (cfstring name );

PRIVATE:
Static cutfactory * m_pinstance;
Static STD: Map <cfstring, cutobject *> m_mapobjects;
};

4. Summary:

The most criticalCodeYes:
# Define register_utobject (utname, classname) bool B # classname = classname: registerobject (utname );

When this line of code is called, the object classname will be registered to the object factory with the name of utname.

Actually, it is simple to call the classname: regisiterobject method.

Create a classname instance and place the instance in the factory array.
Because the declare_utobject macro defines the clone method for each object, the class factory can create other instances of this object from instances in this array.

As for why register_utobject needs to define a bool B # classname object, it is entirely because the write can pass the compiler check. when the compiler reads classname: registerobejct (utname), it will think that this is a function definition rather than a function call. function call is considered only when the return value is explicitly specified.

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.