Search
lxdream.org :: lxdream/src/hook.h :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/hook.h
changeset 678:35eb00945316
next681:1755a126b109
author nkeynes
date Thu May 29 11:00:26 2008 +0000 (14 years ago)
permissions -rw-r--r--
last change Split gdrom.h into public and private gddriver.h
Reorganize gdrom mount to use a disc change hook
file annotate diff log raw
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/hook.h Thu May 29 11:00:26 2008 +0000
1.3 @@ -0,0 +1,82 @@
1.4 +/**
1.5 + * $Id: hook.h 662 2008-03-02 11:38:08Z nkeynes $
1.6 + *
1.7 + * This file defines some useful generic macros for hooks
1.8 + *
1.9 + * Copyright (c) 2008 Nathan Keynes.
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify
1.12 + * it under the terms of the GNU General Public License as published by
1.13 + * the Free Software Foundation; either version 2 of the License, or
1.14 + * (at your option) any later version.
1.15 + *
1.16 + * This program is distributed in the hope that it will be useful,
1.17 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.19 + * GNU General Public License for more details.
1.20 + */
1.21 +
1.22 +#ifndef lxdream_hook_H
1.23 +#define lxdream_hook_H 1
1.24 +
1.25 +#include <assert.h>
1.26 +
1.27 +/**
1.28 + * Hook functions are generally useful, so we'd let to limit the overhead (and
1.29 + * opportunity for stupid bugs) by minimizing the amount of code involved. Glib
1.30 + * has GHook (and of course signals), but they don't actually simplify anything
1.31 + * at this level.
1.32 + *
1.33 + * Hence, the gratuitous macro abuse here.
1.34 + *
1.35 + * Usage:
1.36 + *
1.37 + * In header file:
1.38 + *
1.39 + * DECLARE_HOOK( hook_name, hook_fn_type );
1.40 + *
1.41 + * In implementation file:
1.42 + *
1.43 + * DEFINE_HOOK( hook_name, hook_fn_type );
1.44 + *
1.45 + */
1.46 +#define DECLARE_HOOK( name, fn_type ) \
1.47 + void register_##name( fn_type fn, void *user_data ); \
1.48 + void unregister_##name( fn_type fn, void *user_data );
1.49 +
1.50 +#define FOREACH_HOOK( h, name ) struct name##_hook_struct *h; for( h = name##_hook_list; h != NULL; h = h->next )
1.51 +
1.52 +#define CALL_HOOKS( name, args... ) FOREACH_HOOK(h, name) { h->fn(args, h->user_data); }
1.53 +
1.54 +#define DEFINE_HOOK( name, fn_type ) \
1.55 + struct name##_hook_struct { \
1.56 + fn_type fn; \
1.57 + void *user_data; \
1.58 + struct name##_hook_struct *next; \
1.59 + } *name##_hook_list = NULL; \
1.60 + void register_##name( fn_type fn, void *user_data ) { \
1.61 + struct name##_hook_struct *h = malloc(sizeof(struct name##_hook_struct)); \
1.62 + assert(h != NULL); \
1.63 + h->fn = fn; \
1.64 + h->user_data = user_data; \
1.65 + h->next = name##_hook_list; \
1.66 + name##_hook_list = h; \
1.67 + } \
1.68 + void unregister_##name( fn_type fn, void *user_data ) { \
1.69 + struct name##_hook_struct *last = NULL, *h = name##_hook_list; \
1.70 + while( h != NULL ) { \
1.71 + if( h->fn == fn && h->user_data == user_data ) { \
1.72 + if( last == NULL ) { \
1.73 + name##_hook_list = h->next; \
1.74 + } else { \
1.75 + last->next = h->next; \
1.76 + } \
1.77 + free( h ); \
1.78 + } \
1.79 + last = h; \
1.80 + h = h->next; \
1.81 + }\
1.82 + }
1.83 +
1.84 +
1.85 +#endif /* !lxdream_hook_H */
1.86 \ No newline at end of file
.