mardi 21 février 2017

one #macro injects a field and a case (in switch-case)

How to define MACRO to recognize another MACRO occurrences as list,
and inject codes into 2 different places?

Is it possible?

Example

I want to create code injector that have syntax like this :-

 ###MyInject <Class Name> <Enum slot>  //inject fields
 ###MyInjectAll              //recognize all calling #MyInject (see below)

Below is the expected usage.

Injector.h:-

 ###MyInject Machine slot1
 //^-------- will be replaced by below code 
 template<class T> Sloter<Machine> : public GameObject {
     public: int slot1;            
 };

 ###MyInject Turret slot3
 //^-------- will be replaced by below code 
 template<class T> Sloter<Turret>  : public GameObject {
     public: int slot3;            
 };

Manager.h:-

 class Manager{
     enum SlotX{ slot1,slot2,slot3 };
     public: int* provider(GameObject* gameObject, SlotX slotX){
         switch(slotX){
             ###MyInjectAll 
             //^---- will be replaced by below code
             case slot1:{ 
                 return &(static_cast<Machine*>(gameObject)->slot1); 
             }break;
             case slot3:{ 
                 return &(static_cast<Turret *>(gameObject)->slot3); 
             }break;
         }
     }
 };

It is not a good design, but it is a simple example to consider whether MACRO can do something aggregate.

This feature is useful when I really want ultimate performance and flexibility,
e.g. store index into member itself for extremely-fast hashMap.

I have read :-

I am not sure whether x-macros tag is appropriate.
(I have limited knowledge in this area.)

Aucun commentaire:

Enregistrer un commentaire