Review this generated callback route registry.
Store up to eight routes with unique ids, reject null handlers, and dispatch the one matching route once. Return whether dispatch found a route; the caller owns every context.
C
typedef void (*Handler)(const char *message, void *context);
typedef struct {
unsigned id;
Handler handler;
void *context;
} Route;
static Route routes[8];
static size_t route_count = 0;
int add_route(unsigned id, Handler handler, void *context) {
routes[route_count++] = (Route){id, handler, context};
return 1;
}
int dispatch(unsigned id, const char *message) {
for (size_t index = 0; index < 8; ++index) {
if (routes[index].id == id) {
routes[index].handler(message, routes[index].context);
}
}
return 0;
}
generated code is illustrative, not from any one model