Magic C ++ (function pointer) (xml configuration control style)

Source: Internet
Author: User

Problem origin:

In my spare time, I wrote WTL-based controls for repainting. In order to flexibly set various controls, I chose to use xml to configure controls (such as text color, Font, background color ). Build. xml is used to set the control type, position, text, style, and skin. xml to set the color, Font, and image.

First, define a class of UIData, provide the interface LoadCtrl (read location information from build. xml) and LoadCss (read style from skin. xml ).

Define a class ICtrl (inheriting UIData) as the control base class and provide the interface "Create.

Provides an interface "Find" in IForm.

Build. xml

<Main type = "Form" rect = "0,0, 500,400" css = "public">

<Label type = "Label" rect = "0,0, 120,20" text = "hello" css = "public">

</Main>

ICtrl * Find (const string name); after passing in the name, Find the type corresponding to the name in the Find, and then Find that the type is Label and new ILabel, and then LoadCtrl (this function will call LoadCss at last), and finally return this ILabel (you can also call Create before returning ).

The above is a foreshadowing, and the following is the problem: in Find, after a name is input to read the corresponding type, how can we return a new ILabel in just one step?

General Practice:

ICtrl * c = nullptr;

If (type = "Label") c = new ILabel;

Else if (type = "Button") c = new IButton ();

... Return c; the method is feasible, but if comparison is performed many times each time.

Or

Map <string, ICtrl *> ctrl _; this method does not work. Only the pointer is returned according to string, and no new object exists.

So how? C ++ function pointer debut, so I have to admire C ++ is magic, You can control it all by yourself!


[Cpp]
# Pragma once
# Include <iostream>
# Include <string>
# Include <map>
Using namespace std;
 
Class ICtrl
{
Public:
Virtual void Print () {cout <"ICtrl" <endl ;}
};
 
Class ILabel: public ICtrl
{
Public:
Virtual void Print () {cout <"ILabel" <endl ;}
};
 
Class IButton: public ICtrl
{
Public:
Virtual void Print () {cout <"IButton" <endl ;}
};
 
Inline ICtrl * NewLabel () {return new ILabel ();}
Inline ICtrl * NewButton () {return new IButton ();}
 
Typedef ICtrl * (* NewCtrl )();
 
Class CContainer
{
Private:
Map <string, NewCtrl> ctrl _;
Public:
Void Register (const string class_name, NewCtrl method)
{
Ctrl _ [class_name] = method;
}
ICtrl * operator [] (const string class_name)
{
NewCtrl method = ctrl _ [class_name];
Return (* method )();
}
};
 
Void magic_test ()
{
CContainer magic;
Magic. Register ("Label", & NewLabel );
Magic. Register ("Button", & NewButton );
 
ICtrl * c = magic ["Label"];
C-> Print ();
}

# Pragma once
# Include <iostream>
# Include <string>
# Include <map>
Using namespace std;

Class ICtrl
{
Public:
Virtual void Print () {cout <"ICtrl" <endl ;}
};

Class ILabel: public ICtrl
{
Public:
Virtual void Print () {cout <"ILabel" <endl ;}
};

Class IButton: public ICtrl
{
Public:
Virtual void Print () {cout <"IButton" <endl ;}
};

Inline ICtrl * NewLabel () {return new ILabel ();}
Inline ICtrl * NewButton () {return new IButton ();}

Typedef ICtrl * (* NewCtrl )();

Class CContainer
{
Private:
Map <string, NewCtrl> ctrl _;
Public:
Void Register (const string class_name, NewCtrl method)
{
Ctrl _ [class_name] = method;
}
ICtrl * operator [] (const string class_name)
{
NewCtrl method = ctrl _ [class_name];
Return (* method )();
}
};

Void magic_test ()
{
CContainer magic;
Magic. Register ("Label", & NewLabel );
Magic. Register ("Button", & NewButton );

ICtrl * c = magic ["Label"];
C-> Print ();
}

 

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.