Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Code generation with X-Macros in C (brush.co.nz)
7 points by benhoyt on Aug 21, 2009 | hide | past | favorite | 2 comments


I have a better idea that does not require an extra include file:

  #define TASK_STATUS_ENUM(VALUE) ts##VALUE
  #define TASK_STATUS_ENUM_COMA(VALUE) TASK_STATUS_ENUM(VALUE) ,

  #define TASK_STATUS_VALUES(_) _(Normal) _(ModifiedLocally) _(CreatedLocally)

  enum TaskStatus { TASK_STATUS_VALUES(TASK_STATUS_ENUM_COMA) };

  #define STRINGIFY_VALUE(v) "ts" #v
  #define ENUM_TO_STR(x) if (status == TASK_STATUS_ENUM(x)) return STRINGIFY_VALUE(x);

  char* enumToStr(TaskStatus status) {
    TASK_STATUS_VALUES(ENUM_TO_STR);
    return STRINGIFY_VALUE(Normal);
  }


We have these in our main product at work. They are powerful and very useful but they can get out of hand and be difficult to debug or add code to.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: