一つは、関数ポインタテーブルを作り、実行するやり方(下記サンプルのfunc_event_tblを使うやり方)。
一つは、switch文でイベントを分類して、実行するやり方(下記サンプルのfunc_event_switchを使うやり方)。
typedef enum { FUNC_EVENT0, FUNC_EVENT1, FUNC_EVENT2, } FUNC_EVENT; typedef void (*FUNC_POINTER)(void); typedef struct { FUNC_EVENT event; FUNC_POINTER p_func; } ST_FUNC_TBL; /* プロトタイプ */ static void func_event_tbl(FUNC_EVENT event); static void func_event_switch(FUNC_EVENT event); static void func_event_0(void); static void func_event_1(void); static void func_event_2(void); /* 関数テーブル */ static ST_FUNC_TBL fnc_tbl[] = { {FUNC_EVENT0, func_event_0}, {FUNC_EVENT1, func_event_1}, {FUNC_EVENT2, func_event_2}, }; #define fnc_tbl_size (sizeof(fnc_tbl)/sizeof(fnc_tbl[0])) int main(void) { /* 関数ポインタテーブルで呼び出し */ func_event_tbl(FUNC_EVENT0); /* Switch文で呼び出し */ func_event_switch(FUNC_EVENT0); } static void func_event_tbl(FUNC_EVENT event) { int i; for(i = 0; i < fnc_tbl_size; i++){ if(fnc_tbl[i].event == event){ fnc_tbl[i].p_func(); } } } static void func_event_switch(FUNC_EVENT event) { switch(event){ case FUNC_EVENT0: func_event_0(); break; case FUNC_EVENT1: func_event_1(); break; case FUNC_EVENT2: func_event_2(); break; default: break; } } static void func_event_0(void) { /* FUNC_EVENT0 に対応した関数 */ } static void func_event_1(void) { /* FUNC_EVENT1 に対応した関数 */ } static void func_event_2(void) { /* FUNC_EVENT2 に対応した関数 */ }