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 :-
- Creating a string list and an enum list from a C++ macro
It is too simple. - How can I generate a list via the C preprocessor (cpp)?
SINGLE_CASE
(FUNCTION_TABLE
in the solution) contains code, while mine does not.
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?)