[ create a new paste ] login | about

Link: http://codepad.org/IkYzrbiu    [ raw code | output | fork ]

C, pasted on Feb 14:
#include <stdio.h>

// this goes to shared header
#define DECLARE_STRING_ENUM_FST(name, value) name
#define DECLARE_STRING_ENUM_SND(name, value) value
#define DECLARE_STRING_ENUM(name, macro) \
    typedef enum name { macro(DECLARE_STRING_ENUM_FST) } name; \
    static const char* name##_table[] = { macro(DECLARE_STRING_ENUM_SND) }; \

// this is a usage example
#define MSG_ENUM_(X) \
    X(str_first, "This is the first message."), \
    X(str_second, "This is the second message.")

DECLARE_STRING_ENUM(str_id_t, MSG_ENUM_)

int main()
{
    printf("%s\n", str_id_t_table[str_first]);
    printf("%s\n", str_id_t_table[str_second]);
    return 0;
}


Output:
1
2
This is the first message.
This is the second message.


Create a new paste based on this one


Comments: