dnotifier.h

Go to the documentation of this file.
00001 
00039 #ifndef DT_DNOTIFIER_H
00040 #define DT_DNOTIFIER_H
00041 
00042 #include <cfg/debug.h>
00043 
00044 #include <dt/dtag.h>
00045 #include <struct/list.h>
00046 
00047 //Fwd declaretion.
00048 struct DNotifier;
00049 struct DFilter;
00050 
00051 typedef void (* update_func_ptr)(struct DNotifier *, dtag_t, dval_t);
00052 typedef void (* update_filter_ptr)(struct DFilter *, dtag_t, dval_t);
00053 
00059 typedef struct DNotifier
00060 {
00062     update_func_ptr update;
00063 
00065     List targets;
00066 } DNotifier;
00067 
00072 typedef struct DFilterMap
00073 {
00074     DTagItem src;
00075     DTagItem dst;
00076 } DFilterMap;
00077 
00078 
00083 typedef struct DFilter
00084 {
00086     Node link;
00087 
00089     DNotifier *target;
00090 
00092     update_filter_ptr update;
00093 
00095     const DFilterMap *map;
00096 
00098     DB(uint8_t magic;)
00099 } DFilter;
00100 
00102 typedef uint16_t dfilter_mask_t;
00103 
00105 void filter_init(DFilter *f, const DFilterMap *map, bool masked, DNotifier *source, DNotifier *target);
00106 
00108 void filter_update(DFilter *f, dtag_t tag, dval_t val);
00109 
00111 void filter_mask_update(DFilter *f, dtag_t tag, dval_t val);
00112 
00114 void notifier_init(DNotifier *n);
00115 
00116 
00120 INLINE void dnotify(DNotifier *target, dtag_t tag, dval_t val)
00121 {
00122     if (target)
00123         target->update(target, tag, val);
00124 }
00125 
00129 INLINE void dnotify_targets(DNotifier *target, dtag_t tag, dval_t val)
00130 {
00131     DFilter *f;
00132     if (!LIST_EMPTY(&target->targets))
00133         FOREACH_NODE(f, &target->targets)
00134             f->update(f, tag, val);
00135 }
00136 
00137 
00144 #define DCONNECT(src, tgt, map, opt) \
00145     do { \
00146         static DFilter _filter_; /* Declare a filter */ \
00147         filter_init(&(_filter_), map, opt, src, tgt); /* Init it. */ \
00148     } while (0)
00149 
00150 
00151 #endif /* DT_DNOTIFIER_H */