Suppose I have a list of #define
s in a header file for an external library. These #define
s represent error codes returned from functions. I want to write a conversion function that can take as an input an error code and return as an output a string literal representing the actual #define
name.
As an example, if I have
#define NO_ERROR 0
#define ONE_KIND_OF_ERROR 1
#define ANOTHER_KIND_OF_ERROR 2
I would like a function to be able to called like
int errorCode = doSomeLibraryFunction();
if (errorCode)
writeToLog(convertToString(errorCode));
And have convertToString()
be able to auto-convert that error code without being a giant switch-case looking like
const char* convertToString(int errorCode)
{
switch (errorCode)
{
case NO_ERROR:
return "NO_ERROR";
case ONE_KIND_OF_ERROR:
return "ONE_KIND_OF_ERROR";
...
...
...
I have a feeling that if this is possible, it would be possible using templates and metaprogramming, but that would only work the error codes were actually a type and not a bunch of processor macros.
To convert a list to dictionary, we can use list comprehension and make a key:value pair of consecutive elements. Finally, typecase the list to dict type.
tuple () function can take any iterable as an argument and convert it into a tuple object. As you wish to convert a python list to a tuple, you can pass the entire list as a parameter within the tuple() function, and it will return the tuple data type as an output.
How to Convert a String to a List of Words. Another way to convert a string to a list is by using the split() Python method. The split() method splits a string into a list, where each list item is each word that makes up the string. Each word will be an individual list item.
A list can be converted to a set object using Set constructor. The resultant set will eliminate any duplicate entry present in the list and will contains only the unique values. Set<String> set = new HashSet<>(list);
I normally do it the giant switch case way, although I make it somewhat easier with:
#define STR(code) case code: return #code
switch (errorCode)
{
STR(NO_ERROR);
STR(ONE_KIND_OF_ERROR);
}
This is a good question, I'm interested to see what better ways people have
You are correct. There's no way to recover preprocessor-defined identifiers at runtime (unless you can read the source, but that's cheating). You would be better off creating a constant array of the names and indexing it with the error code (with proper boundary checks of course) - it should be quite easy to write a script to generate it.
take a look at boost preprocessor. You could create list/array/sequence of code pairs:
#define codes ((1,"code1"))((...))
#define code1 1
...
// then use preprocessor FOR_EACH to generate error handlers
relevant link:
http://www.boost.org/doc/libs/1_41_0/libs/preprocessor/doc/ref/seq_for_each.html
http://www.boost.org/doc/libs/1_41_0/libs/preprocessor/doc/ref/tuple_elem.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With