Home » Community » Newbie corner » Search for constant and show it....
Search for constant and show it.... [message #36178] |
Wed, 09 May 2012 11:00 |
Wolfgang
Messages: 146 Registered: November 2011 Location: Germany
|
Experienced Member |
|
|
Hi,
I've got an untypical task, I have a namespace with a amount of constants (const uint16 ...) and for debugging reasons I want to search for the name of the constant by their value:
For better understanding:
namespace CanMethod
{
const uint16 PING_REQUEST = 1001;
const uint16 PING_REPLAY = 1002;
const uint16 DEVICE_STATE_REQUEST = 1003;
const uint16 DEVICE_STATE_REPLAY = 1004;
}
(the real list is larger than the examle...)
I get the value "1003" and now I want to get the String "DEVICE_STATE_REQUEST" - is this possible without to create a complete new list with all constants?
|
|
|
|
|
Re: Search for constant and show it.... [message #36193 is a reply to message #36178] |
Thu, 10 May 2012 14:36 |
|
If you are allowed to modify the code defining the constants, you can use some preprocessor trick, e.g.:#ifndef flagDEBUG
#define CONSTVAL(NAME,VALUE) const uint16 NAME = VALUE
#else
VectorMap<int,String> const_values;
#define CONSTVAL(NAME,VALUE) const_values.Add(VALUE,#NAME); const uint16 NAME = VALUE
#endif
namespace CanMethod
{
CONSTVAL(PING_REQUEST, 1001);
CONSTVAL(PING_REPLAY, 1002);
CONSTVAL(DEVICE_STATE_REQUEST, 1003);
CONSTVAL(DEVICE_STATE_REPLAY, 1004);
}
It defines the constants using a macro, just if it is in debug mode, it adds each value to a "dictionary", which you can later simply query like const_values[1003] to get string description "DEVICE_STATE_REQUEST", which is what you want if I understand correctly
There also might or might not be a way to do this using debug informations present in the executable. You could probably hack some solution using gdb or some of its code... But it is certainly too much work to even think of it
Best regards,
Honza
|
|
|
|
Goto Forum:
Current Time: Mon May 06 04:06:34 CEST 2024
Total time taken to generate the page: 0.02243 seconds
|