Created
January 28, 2015 08:46
-
-
Save fredyr/3103417e404f6c03466e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
// hook implementation | |
typedef void (*void_func_t)(void); | |
typedef struct tHookElem { | |
unsigned int id; // dispatch id | |
void_func_t func; | |
} HookElem; | |
#define HOOKLIST_STATIC_SIZE 20 | |
typedef struct tHookList { | |
size_t size; | |
HookElem elems[HOOKLIST_STATIC_SIZE]; | |
} HookList; | |
void initHookList(HookList* hl) { | |
hl->size = 0; | |
} | |
void registerHook(HookList* hl, unsigned int id, void_func_t func) { | |
size_t s = hl->size; | |
hl->elems[s].id = id; | |
hl->elems[s].func = func; | |
hl->size = (s + 1) % HOOKLIST_STATIC_SIZE; | |
} | |
void dispatchHook(HookList* hl, unsigned int id) { | |
unsigned int i; | |
for (i = 0; i < hl->size; ++i) { | |
if (id == hl->elems[i].id) | |
(*hl->elems[i].func)(); | |
} | |
} | |
void myTestHook1() { | |
printf("hellow from hook 1\n"); | |
} | |
void myTestHook2() { | |
printf("hellow from hook 2\n"); | |
} | |
int main(void) { | |
unsigned int i; | |
HookList hooks; | |
initHookList(&hooks); | |
registerHook(&hooks, 12, &myTestHook1); | |
registerHook(&hooks, 2, &myTestHook2); | |
for (i = 0; i < 13; ++i) | |
dispatchHook(&hooks, i); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment