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 ();
}