Converting XMacro into template in C++03

I have an XMacro which I use to create a sort-of object database surrogate (kind of). Basically, each entry of the XMacro is processed and then added to the database.

The procedure is always the same for each record in the database, however the fine details (e.g. what a function does or which instance is required for a computation) are changing. At the time I felt that an XMacro would do fine, but as the pre-processing of each entry became more and more complicated, I feel that an XMacro is more of a shortcoming than an advantage, since it cannot be properly debugged without mentioning the code bloat.

I am thinking to convert the procedure into a templated function which will perform the same steps and is debuggable, however I have no idea of how to convert the "database part" (the list of records) into a template.

It would be great if someone could suggest the following:

1. How can be converted the "data part" of an XMacro into a template?
2. Can a templated function be used to populate a collection from a list of records?
3. Any other idea on how to populate a collection of records of any type without creating a code bloat (if possible) ?

As additional complexity, I cannot use C++11 to convert the XMacro.

Below there is a simplified example of what I am trying to do

Animals.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//each record will perform an operation on the collection below
static std::vector<double> petStatementDatabase; 
class Animal{}

class Persian : public Animal {
   int talk(std::string say) {return say.size();}
}
class GermanShep : public Animal {
   double talk(std::string say) {return say.size()+0.5;}
}

enum Pets{
   CAT
   DOG
   PetNum
};
//below is the "data part" of the XMacro which I don't know how to convert into template
#define PETS_DB \
     MYPET(CAT, Persian, "meaow", int, CatDispenser) \
     MYPET(DOG, GermanShep, "woof", double, DogDispenser)
    [...]

Animals.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
std::vector<double> petStatementDatabase(0.0,PetNum);

void init(std::string& animalTalk) //animal talk has many strings, one being either meaow or woof
{
//below is the simplified preprocessing of each item of the XMacro
#define MYPET(type, cClass,sProperty,value_type,cDispenser)
     bool found=findVoice(animalTalk,sProperty);   //findVoice is a parsing function
     Animal* being=cDispenser::getSingleton().get();    //get the pet from the singleton
     cClass* pet=dynamic_cast<cClass*>(being);  //downcast (not needed here, but mandatory in my case)
     if(pet!=NULL)
     {
         value_type val=pet.talk(sProperty);   //obtain class-specific value
        //add value to the database, depending on the result of findVoice
         petStatementDatabase.at(type)+=found? val : val*2;        
     }
 PETS_DB
 #undef MYPET 
Last edited on
Topic archived. No new replies allowed.