I am posting this question here after I have done enough research within our community. However, I couldn't find a proper solution for my problem yet. So, I am posting my question here.
#define EXPAND_AS_ENUMERATION(a) CODE_##a,
#define EXPAND_AS_ARRAY(a) a,
#define CODE_TABLE(EXPAND)\
EXPAND(0x00E30054uL)\
EXPAND(0x00ED3581uL)\
EXPAND(0x00ED3983uL)\
EXPAND(0x00EE0368uL)\
EXPAND(0x00EE0368uL)\
EXPAND(0x00D01087uL)\
EXPAND(0x00ED4181uL)\
EXPAND(0x00505602uL)\
// Actual Event IDs which need to be selected at run time
#define SWC_FAULT_CODE_0x00E30054_EVENT 113
#define SWC_FAULT_CODE_0x00ED3581_EVENT 213
#define SWC_FAULT_CODE_0x00ED3983_EVENT 432
#define SWC_FAULT_CODE_0x00EE0368_EVENT 411
#define SWC_FAULT_CODE_0x00EE0368_EVENT 311
#define SWC_FAULT_CODE_0x00D01087_EVENT 231
#define SWC_FAULT_CODE_0x00ED4181_EVENT 471
#define SWC_FAULT_CODE_0x00505602_EVENT 419
#define prefix_str SWC_FAULT_CODE
#define postfix_str _EVENT
#define Get_EventID_FROM_CODE(code) ????? // ex: when code= 0x00ED3581, then it should return SWC_FAULT_CODE_0x00ED3581_EVENT macro or it's value 213
#define Get_EventID_FROM_ENUM(code_enum) ????? // ex: when code_enum= CODE_0x00ED3581, then it should return SWC_FAULT_CODE_0x00ED3581_EVENT macro or it's value 213
#define Get_CodeEnumName(code) ????? // ex: when code = 0x00ED3581, then it should return DTC_0x00ED3581 which is enum element
typedef enum
{
CODE_TABLE(EXPAND_AS_ENUMERATION)
CODE_COUNT
}codeList_t ;
void Cycle_1ms(const uint32 code)
{
// Based on code value, I want to get the macro value of SWC_FAULT_CODE_0xZZZZZZZZ_EVENT.
// ex: when code = 0x00ED3581, then eventID = SWC_FAULT_CODE_0x00ED3581_EVENT = 213
uint16 eventID_FromCode = Get_EventID(code);
}
void Cycle_1ms_anotherFunc(codeList_t code_enum)
{
// Based on code value, I want to get the macro value of SWC_FAULT_CODE_0xZZZZZZZZ_EVENT.
// ex: when code = 0x00ED3581, then eventID = SWC_FAULT_CODE_0x00ED3581_EVENT = 213
uint16 eventID_FromCodeEnum = Get_EventID_FROM_ENUM(code_enum);
}
void callback_func(uint32 code, uint8* buf)
{
// here, based on code value Get_CodeEnumName() should provide the enum element for that code. ex: when code= 0x00ED3581, enum_var = DTC_0x00ED3581
codeList_t enum_var = Get_CodeEnumName(code);
}
Here, CODE_TABLE will be changed from project to project and number of elements with in this table. After reading several articles and topics on Xmacros, I though of using Xmacros concept here to avoid looping and heavy use of RAM memory .
You cannot use X macros with run-time input, they are expanded at compile time. So if you need to do table look-ups based on run-time values, they are not the solution.
You can however use X macros to generate a look-up table and then use a magic number compile-time constant to expand into an enum, which in turn is corresponding to a look-up table index:
The GET_EVENT_ID expands to something like
LOOKUP[CODE_0x00ED3581]
which is a an array access.Also the "events" could be bunched together with the codes in the same X macro list even if you don't need to use them in every macro. Also note that you have duplicate items in your X macro list which won't work since you plan to use it for generating unique enum constants. We can change it like this:
Then create SWC_FAULTS as enum constants instead of #defines:
And then the enum to use for index of the lookup table:
A complete example:
Output:
0x00ED3581 = 213
.On gcc x86 the whole table can get optimized out leaving just this: