BeRTOS
dnotifier.h
Go to the documentation of this file.
00001 
00038 #ifndef DT_DNOTIFIER_H
00039 #define DT_DNOTIFIER_H
00040 
00041 #include <cfg/debug.h>
00042 
00043 #include <dt/dtag.h>
00044 #include <struct/list.h>
00045 
00046 //Fwd declaretion.
00047 struct DNotifier;
00048 struct DFilter;
00049 
00050 typedef void (* update_func_ptr)(struct DNotifier *, dtag_t, dval_t);
00051 typedef void (* update_filter_ptr)(struct DFilter *, dtag_t, dval_t);
00052 
00058 typedef struct DNotifier
00059 {
00061     update_func_ptr update;
00062 
00064     List targets;
00065 } DNotifier;
00066 
00071 typedef struct DFilterMap
00072 {
00073     DTagItem src;
00074     DTagItem dst;
00075 } DFilterMap;
00076 
00077 
00082 typedef struct DFilter
00083 {
00085     Node link;
00086 
00088     DNotifier *target;
00089 
00091     update_filter_ptr update;
00092 
00094     const DFilterMap *map;
00095 
00097     DB(uint8_t magic;)
00098 } DFilter;
00099 
00101 typedef unsigned int dfilter_mask_t;
00102 
00104 void filter_init(DFilter *f, const DFilterMap *map, bool masked, DNotifier *source, DNotifier *target);
00105 
00107 void filter_update(DFilter *f, dtag_t tag, dval_t val);
00108 
00110 void filter_mask_update(DFilter *f, dtag_t tag, dval_t val);
00111 
00113 void notifier_init(DNotifier *n);
00114 
00115 
00119 INLINE void dnotify(DNotifier *target, dtag_t tag, dval_t val)
00120 {
00121     if (target)
00122         target->update(target, tag, val);
00123 }
00124 
00128 INLINE void dnotify_targets(DNotifier *target, dtag_t tag, dval_t val)
00129 {
00130     DFilter *f;
00131     if (!LIST_EMPTY(&target->targets))
00132         FOREACH_NODE(f, &target->targets)
00133             f->update(f, tag, val);
00134 }
00135 
00136 
00143 #define DCONNECT(src, tgt, map, opt) \
00144     do { \
00145         static DFilter _filter_; /* Declare a filter */ \
00146         filter_init(&(_filter_), map, opt, src, tgt); /* Init it. */ \
00147     } while (0)
00148 
00149 
00150 #endif /* DT_DNOTIFIER_H */