Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » Search for constant and show it....
Search for constant and show it.... [message #36178] Wed, 09 May 2012 11:00 Go to next message
Wolfgang is currently offline  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 #36191 is a reply to message #36178] Thu, 10 May 2012 12:24 Go to previous messageGo to next message
Wolfgang is currently offline  Wolfgang
Messages: 146
Registered: November 2011
Location: Germany
Experienced Member
none knows or isn't it possible?
Re: Search for constant and show it.... [message #36192 is a reply to message #36191] Thu, 10 May 2012 14:05 Go to previous messageGo to next message
cbpporter is currently offline  cbpporter
Messages: 1401
Registered: September 2007
Ultimate Contributor
I think it can't be done without enums.
Re: Search for constant and show it.... [message #36193 is a reply to message #36178] Thu, 10 May 2012 14:36 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

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 Smile

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 Wink

Best regards,
Honza
Re: Search for constant and show it.... [message #36194 is a reply to message #36193] Thu, 10 May 2012 21:51 Go to previous message
BioBytes is currently offline  BioBytes
Messages: 307
Registered: October 2008
Location: France
Senior Member
Hi all,

Honza's solution is smart as usual Smile . From my side I would prefer enumeration like Wolfgang for "more C++ flavour".

Regards

Biobytes
Previous Topic: XML exercise
Next Topic: time measurement :: RTIMING, TimeStop, GetTickCount
Goto Forum:
  


Current Time: Wed Apr 24 02:19:14 CEST 2024

Total time taken to generate the page: 0.02332 seconds