1

How to define macro (ALL_SWITCH) to recognize another macro (SINGLE_CASE) occurrences as list, and insert codes into 2 different places?

Is it possible?

Example

I want to create macro that have syntax roughly similar as :-

 SINGLE_CASE <Class Name> <Enum slot>  //add fields
 ALL_SWITCH              //recognize all calling SINGLE_CASE (see below)

Below is the expected usage.

TopHeader.h:-

 SINGLE_CASE Machine slot1
 SINGLE_CASE Machine slot2
 //^-------- will be replaced by below code (generate the whole class)
 template<class T> Sloter<Machine> : public GameObject {
     public: int slot1;  
     public: int slot2;          
 };

 SINGLE_CASE 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){
             ALL_SWITCH 
             //^---- will be replaced by below code (add only cases)
             case slot1:{ 
                 return &(static_cast<Machine*>(gameObject)->slot1); 
             }break;
             case slot2:{ 
                 return &(static_cast<Machine*>(gameObject)->slot2); 
             }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.)

My progress

#define SINGLE_CASE( ClassName, slotX) /
template<class T> Sloter<ClassName> : public GameObject { /
     public: int slotX;  /
};

I still don't know how to make it support many slot for the same class or code ALL_SWITCH.
I am considering defining some kinds of table in macro, then make other macro read it. (not possible?)

Community
  • 1
  • 1
javaLover
  • 6,347
  • 2
  • 22
  • 67
  • @rici Thank a lot rici! I have never known that. – javaLover Feb 22 '17 at 05:54
  • 3
    Instead of using the C++ preprocessor, you could write a simple preprocessor that replaces these self-defined "macros", and then run this program before compiling the code. – Roland Illig Feb 22 '17 at 06:09
  • @Roland Illig I have zero experience about this approach. If it does not disturb you too much, may you share the workflow, please? ... may be some links .... e.g. .... Should I create a new project in Visual Studio, or write a custom script? Which script language? Should I use notepad++ or something? ... some good practices ... Thank! – javaLover Feb 22 '17 at 06:44
  • @rici I think I can inject the definition .... http://ideone.com/oC62F1 (template class specialization define using macro) .... and http://ideone.com/w2zCoN (add a field into a template class by macro). .... or you mentioned a different thing? – javaLover Feb 22 '17 at 07:01
  • @rici Sorry for being not clear. I edited the question by removing all "inject" words. Thanks. – javaLover Feb 22 '17 at 07:17
  • This is not possible. The preprocessor is not powerful enough. In general, avoid preprocessor. One of the stated design goals of C++ is eliminating the preprocessor. – n. m. could be an AI Feb 22 '17 at 07:41

0 Answers0