Click here to Skip to main content
15,886,626 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 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 .

What I have tried:

I have tried below snippets of code.


#define Get_RealEventID_From_CodeEnum(code) SWC_FAULT_CODE_##code##_EVENT
#define Get_EventID_FROM_CODE(code) Get_RealEventID_From_CodeEnum(code)

#define Get_RealCodeName(code) CODE_#code
#define Get_CodeEnumName(code) Get_RealCodeName(code)
Posted
Updated 1-Jun-21 21:54pm
v2

1 solution

I have a similar issue which I solved with two stage macros. I am not sure that the following actually gives the result you are looking for, but it generates the correct string as far as I can see. Maybe it is something you can build on.
C++
#define Event1 0x00ED3581

#define	STR(x)				#x
// sets the full version string to the form SWC_FAULT_CODE_0x00ED3581_EVENT etc.
#define	Get_EventID(code)	STR(SWC_FAULT_CODE_)##STR(code)##STR(_EVENT)
cout << Get_EventID(Event1) << endl;
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900