C ++ rtti: Two macros are used to create an object based on the object name"

Source: Internet
Author: User

1. Question:

C ++ rtti lacks some runtime information and cannot directly create an object based on the object name. 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 an integrated testing tool with a configuration file to specify which cases need to be tested. The content is roughly as follows:
<Ut>
<Case name = "case1"/>
<Case name = "case2"/>
</UT>

When the test tool imports 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 registered to the object factory using two macros. Then, you can use the object factory to create the 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 );

Cutfactory is a singletion object. It 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. Key points:
The most critical code is
# 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, the classname: regisiterobject method is called. 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.

5. Others:
Personal notes ..

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.