0

I frequently use enum in C to access array elemnets with numerical data, e.g.

#define KEYS_MAX 1
#define FIELD_MAX 2

enum {FIELD1=0, FIELD2};

double array[KEYS_MAX][FIELD_MAX];
array[1][FIELD1] = 1.0; array[1][FIELD2] = 2.0;

I then print the data to a file in KEYS_MAX lines and FIELD_MAX columns. To know later the content of the columns I would like to print a header line.

# KEY FIELD1 FIELD2
1 1.0 2.0

I would like to have a routine which does this during runtime correctly even if I update the code by changing only enum. I.e., how can I print the header line using C-code and possibly macros using a fixed routine independent of updates of enum?

I found this: Mapping enum values to strings in C++ , but I would prefer something which would also work with the intel compiler.

Community
  • 1
  • 1
highsciguy
  • 2,569
  • 3
  • 34
  • 59

1 Answers1

4

You could play some preprocessor tricks. As an exmple, in GCC source tree implementation, you might get inspired by gcc/tree.def files.

So you might have one file, e.g. myenum.def, with e.g. things like

MYENUM(Pink)
MYENUM(Red)
MYENUM(Ivory)

Then, you might define your enum with some code like

enum my_enum {
#define MYENUM(Name) Name,
#include "myenum.def"
#undef MYENUM
};

(You might want to put None_ before the #include above, and Last_ after it)

Then you could have an enum to string converter with e.g.

const char* myenum_to_string(enum my_enum en) {
  switch(en) {
#define MYENUM(Name) case Name: return #Name;
#include "myenum.def"
#undef MYENUM
  default: return NULL; /* should not happen */
  }
}

and you might have a string to enum converter with e.g.

enum myenum string_to_my_enum (const char*str) {
#define MYENUM(Name) if (!strcmp(str, #Name)) return Name;
#include "myenum.def"
#undef MYENUM
  else abort ();
}

All the above is common practice, not tied to a particular compiler (it should work with gcc, clang, tcc, icc or any C99 compliant compiler).

In addition, if you have a large code base (than you can compile with a recent gcc) in which you have hundreds of enum and you don't want to play such tricks for every of them, you could e.g. develop a GCC plugin or extension (in MELT for instance) to generate -using the internal representations inside GCC of your code- the similar C code just from the enum declarations. If you are coding a new software, or you know your software base quite well, you could replace the enum code with similar tricks.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • It is not GCC specific, but the implementation of GCC uses such tricks. Some other free software also uses it (probably Gtk, and perhaps some sub-systems of the Linux kernel). I got my inspiration from the `gcc/tree.def` file of GCC, which I happens to know. – Basile Starynkevitch Oct 06 '12 at 17:01
  • The only GCC specific trick would be to write a MELT extension to automagically generate such code (from the abstract syntax trees built by the GCC compiler), without using the preprocessor trick I am suggesting. – Basile Starynkevitch Oct 06 '12 at 17:07
  • This is essentially isomorphic with the mechanism described in [Enums, Strings, and Laziness](http://labs.mudynamics.com/2007/01/03/enums-strings-and-laziness/), which has also been mentioned on SO before (in [SO147267](http://stackoverflow.com/questions/147267/easy-way-to-use-variables-of-enum-types-as-string-in-c/) and [SO 10915520](http://stackoverflow.com/questions/10915520/displaying-define-values-in-c/) to name but two). This is no way means you didn't already know about the technique or find out about it from other sources. – Jonathan Leffler Oct 06 '12 at 18:55