'define'에 해당되는 글 1건

  1. 2009.09.17 [C/C++] enum 내부에서의 macro 사용

[C/C++] enum 내부에서의 macro 사용

CS/C/C++ 2009. 9. 17. 19:38
# 소스 : WebKit JavascriptCore

#define FOR_EACH_OPCODE_ID(macro) \
    macro(op_enter, 1) \
    macro(op_enter_with_activation, 2)	\
    macro(op_init_arguments, 1) \
    macro(op_create_arguments, 1) \
    macro(op_convert_this, 2) \
    \
    macro(op_new_object, 2) \
    macro(op_new_array, 4) \
    macro(op_new_regexp, 3) \
    macro(op_mov, 3) \
    \
    macro(op_end, 2)

#define OPCODE_ID_ENUM(opcode, length) opcode,
typedef enum { FOR_EACH_OPCODE_ID(OPCODE_ID_ENUM) } OpcodeID;
#undef OPCODE_ID_ENUM

const int numOpcodeIDs = op_end + 1;

#define OPCODE_ID_LENGTHS(id, length) const int id##_length = length;
FOR_EACH_OPCODE_ID(OPCODE_ID_LENGTHS);
#undef OPCODE_ID_LENGTHS
    
#define OPCODE_LENGTH(opcode) opcode##_length

#define OPCODE_ID_LENGTH_MAP(opcode, length) length,
const int opcodeLengths[numOpcodeIDs] = {FOR_EACH_OPCODE_ID(OPCODE_ID_LENGTH_MAP)};
#undef OPCODE_ID_LENGTH_MAP

이를 전처리 작업하면
typedef enum {
    op_enter,
    op_enter_with_activation,
    op_init_arguments,
    op_create_arguments,
    op_convert_this,
    op_new_object,
    op_new_array,
    op_new_regexp,
    op_mov,
    op_end,
} OpcodeID;


const int numOpcodeIDs = op_end + 1;


const int op_enter_length = 1;
const int op_enter_with_activation_length = 2;
const int op_init_arguments_length = 1;
const int op_create_arguments_length = 1;
const int op_convert_this_length = 2;
const int op_new_object_length = 2;
const int op_new_array_length = 4;
const int op_new_regexp_length = 3;
const int op_mov_length = 3;
const int op_end_length = 2;


const int opcodeLengths[numOpcodeIDs] = { 1, 2, 1, 1, 2, 2, 4, 3, 3, 2, };


이렇게 된다.
깔끔하지 아니한가?
: