C++ Messages Classes Generation
Hi I need a library to automatically generate message classes in C++ from some temaplate (XML for example). Something similar to google buffers. However google buffers do not support inheritance and "free" fields. I don't n开发者_运维知识库eed to use for serialization it is just automatic generation from a template part, which is a must. Any ideas?
XSLT seems like the obvious choice, if your "source" is in XML.
Looks like you have at least two issues: reading from XML and creating instances on the Fly. As others will state, use a library for parsing XML. Search the web for "Factory design pattern c++"
.
If there is a common base class, a factory will return instances of the descendant object (allocated in dynamic memory). Otherwise the factory will be a collection of functions returning different objects.
One form of the factory implementation is to have methods that receive a text string (of the name of the class to be created) and return an instance pointer or NULL if method could not create the class. Something like this:
class Animal;
class Cat: public Animal;
class Dog: public Animal;
class Elephant: public Animal;
Animal * Create_Cat(const std::string& animal_name);
Animal * Create_Dog(const std::string& animal_name);
Animal * Create_Elephant(const std::string& animal_name);
Animal * Animal_Factory(const std::string& animal_name)
{
Animal * p_animal = NULL;
do
{
p_animal = Create_Cat(animal_name);
if (p_animal)
{
break;
}
p_animal = Create_Dog(animal_name);
if (p_animal)
{
break;
}
p_animal = Create_Elephant(animal_name);
if (p_animal)
{
break;
}
} while (false);
return p_animal;
}
精彩评论