I am referring to a code sample from the opensource project tig. Which is a great tool!
file:tig.c
I am struggling to find a reason for defining the request enumeration as follows:
enum request {
#define REQ_GROUP(help)
#define REQ_(req, help) REQ_##req
/* Offset all requests to avoid conflicts with ncurses getch values. */
REQ_UNKNOWN = KEY_MAX + 1,
REQ_OFFSET,
REQ_INFO,
/* Internal requests. */
REQ_JUMP_COMMIT,
#undef REQ_GROUP
#undef REQ_
};
even structures as well..
static const struct request_info req_info[] = {
#define REQ_GROUP(help) { 0, NULL, 0, (help) },
#define REQ_(req, help) { REQ_##req, (#req), STRING_SIZE(#req), (help) }
REQ_INFO
#undef REQ_GROUP
#undef REQ_
};
as can be seen REQ_GROUP has been #defined multiple times creating confusion.. atleast to me. Well i know there might be a good reason to do as such.. what is the actual reason to hide the enum/struct definition in the code using macros?
This is usually down when different treatment should be used on the same data source.
For example, you might do:
And then:
In which case,
MY_LISTis expanded using the current definition ofX.Now, in the same file, I can also use
MY_LISTto create ato_stringmethodThis way, the set of values of the enum is only ever written once, and automatically both the enum and a number of methods dealing with it are kept in sync with this set.