1. A wordy look
Speaking of C + +, Many people feel difficult to learn, in fact, I think so, in this mobile fire to explode in the era, I want to change, mobile to do things that are the existing Third-party library, can be used, and stability, development speed, and the most critical is something.
Talking about dynamically generated objects, why is the powerful C + + not supported? People who want to use such a function must implement a set of such logic on their own.
2. Reasons for Realization
Sometimes development is really a contradiction, for example: 1, to achieve a function can use a lot of similar code, can also use templates, then how do we choose? 2, if after implementing a class, he has a large number of properties, and these properties all need set and get method, then we still have to Ctrl +c and v? If there are many such classes, is it CTRL + C and CTRL + V? For the first question, a motivated developer, I believe he will choose the template, the answer to the second question, what we need to talk about in this article, dynamically generate objects, serialize and Deserialize.
3. Realization of Ideas
In fact, the function of the implementation of the code is still relatively small, is the use of a large number of macro and Factory mode
1, Write a factory class, specifically for the generation of objects
1typedefvoid* (* Createclass) (void);2 3 classcclassfactory4 {5 public:6 StaticCclassfactory &intancefactory ();7 8 public:9 void* CreateObject (Conststd::string&className);Ten voidRegistclass (Conststd::string& name,ConstCreateclass &method); one a Private: -std::map<std::string, createclass>m_classmap; -};
2, and then write a convenient class, This class is just for registration convenience, when this class is declared, that is, register a class to the factory
1 class cdynamicclass 2 3 public : cdynamicclass (const std::string & name, const createclass & METHOD) 5 { 6 cclassfactory::intancefactory (). Registclass (name, method); 7 8 };
3, 2 Key macro, The two macros one is used for cdynamicclass static object, One is used to initialize the cdynamicclass object, the function please see a section, hehe, is actually registered macro parameter class to the factory
1 #define Declare_class (className)2 std::string classname# #Name; 3 Static Cdynamicclass * classname# #Namedc; 4 5 #define Implement_class (className)6new cdynamicclass (#className, classname::instance);
4, 2 attribute macro, Access_interface macro is used to register the related interface of the property, Access_register macro is to record the attribute name and the Object's property call interface, so as to set the properties Later.
1 #defineAccess_interface (classtype, type, name, describe)2 public:3std::stringm_describe# #name =#describe;4InlineStatic voidset# #name (cbaseclass * cp,void*Value) {5ClassType * TP = (classtype *) cp;6tp->m_# #name = * (type *) value;7 }8Inline Type get# #name (void)Const {9 returnm_# #name;Ten } oneInline std::stringget# #name # #Describe () { a returnm_describe# #name; - } - the #defineAccess_register (name) -M_propertymap.insert ({#name, set# #name});
5. Base class, the base class for all objects, them_propertymap member is a set interface pair that stores properties and properties for
1 classCbaseclass2 {3 public:4 cbaseclass () {}5 Virtual~cbaseclass () {}6 7 public:8std::map<std::string, setvalueproperty>m_propertymap;9 Ten Private: one};
4. Test Class
1 classChelloclass: publicCbaseclass2 {3 public:4 Declare_class (chelloclass);5Access_interface (chelloclass,int, age,"Age")6Access_interface (chelloclass,int, Sex,"Sex")7 8 public:9 Chelloclass ();Ten Virtual~Chelloclass (); one a public: - Static void*Instance (); - the public: - Virtual voidRegistproperty (); - - protected: + intM_age =0; - intM_sex =0; +};
The Chelloclass class is a test class used to test whether the dynamically generated object written in section III is correct, and theRegistproperty interface is a property registration
1. Test main function
1 intMainintargcChar*Argv[])2 {3 qcoreapplication a (argc, argv);4 5 6Chelloclass * PVar = (chelloclass*) cclassfactory::intancefactory (). CreateObject ("Chelloclass");7 if(pVar)8 {9 intPAge =2;Ten intPsex =1; one apvar->m_propertymap[" age"] (pVar, &pAge); -pvar->m_propertymap["Sex"] (pVar, &psex); - theStd::cout << pvar->getagedescribe () << pvar->getage () <<std::endl; -Std::cout << pvar->getsexdescribe () << pvar->getsex () <<std::endl; - } - + returna.exec (); -}
2. Effect Results
Figure 1 Chelloclass Test Results
5, Serialization and deserialization
This article is mainly to explain the dynamic generation of objects, and do not intend to delve into the serialization and deserialization of the module, the demo also has a small number of serialized code, mainly using TINYXML2 to read the file, the code is as Follows:
1 voidDynamicobject::D eserialize ()2 {3 tinyxml2::xmldocument doc;4 if(tinyxml2::xml_no_error = = Doc. LoadFile ("D:\\example\\paint\\dynamiccreateobject\\test.xml"))5 {6 if(tinyxml2::xmlnode * RootNode = doc.) Firstchildelement ("ojbectlist"))7 {8 Const Char* Roottext = Rootnode->toelement ()->attribute ("name");9 TenTinyxml2::xmlelement * element = Rootnode->firstchildelement ("Object"); one while(element) a { - Const Char* ObjectName = Element->attribute ("name"); -Tinyxml2::xmlelement * propertyelement = element->firstchildelement (" property"); the while(propertyelement) - { - Const Char* PropertyName = Propertyelement->attribute ("name"); - Const Char* PropertyValue = Propertyelement->attribute ("value"); + } -Tinyxml2::xmlnode * NextNode = element->NextSibling (); + if(nextnode = =Nullptr) a { at break; - } -element = Nextnode->toelement (); - } - } - } in}
When it comes to object serialization, I think that there is a problem that is difficult to fix, that objects contain objects, that is, recursive serialization, and if it involves judging recursion then we may also need to implement a set of structures to indicate whether the current object contains other objects and whether the problem of recursive serialization needs to Continue. There is a chance I will explain this issue in a specific article.
6. Demo
C + + dynamically generated objects
C + + dynamically generated objects