dnotifier.c

Go to the documentation of this file.
00001 
00041 #include <cfg/debug.h>
00042 
00043 #include <dt/dtag.h>
00044 #include <dt/dnotifier.h>
00045 #include <struct/list.h>
00046 
00051 static void notifier_update(DNotifier *n, dtag_t tag, dval_t val)
00052 {
00053     dnotify_targets(n, tag, val);
00054 }
00055 
00059 void notifier_init(DNotifier *n)
00060 {
00061     // Init instance
00062     n->update = notifier_update;
00063     LIST_INIT(&n->targets);
00064 }
00065 
00072 void filter_update(DFilter *f, dtag_t tag, dval_t val)
00073 {
00074 
00075     const DFilterMap *map = f->map;
00076 
00077     if (map)
00078     {
00079         while (map->src.tag != TAG_END)
00080         {
00081             if ((map->src.tag == tag) && (map->src.val == val))
00082             {
00083                 tag = map->dst.tag;
00084                 val = map->dst.val;
00085                 break;
00086             }
00087             /* TAG_ANY matches anything */
00088             if (map->src.tag == TAG_ANY)
00089                 break;
00090             map++;
00091         }
00092 
00093         if (map->src.tag != TAG_END)
00094             dnotify(f->target, tag, val);
00095     }
00096     else
00097         dnotify(f->target, tag, val);
00098 }
00099 
00100 
00108 void filter_mask_update(DFilter *f, dtag_t tag, dval_t val)
00109 {
00110 
00111     const DFilterMap *map = f->map;
00112     dfilter_mask_t mask;
00113 
00114     if (map)
00115     {
00116         while (map->src.tag != TAG_END)
00117         {
00118             mask = (dfilter_mask_t) map->src.val;
00119             if ((map->src.tag == tag) && ((mask & (dfilter_mask_t)val) == mask))
00120             {
00121                 tag = map->dst.tag;
00122                 val = map->dst.val;
00123                 break;
00124             }
00125             /* TAG_ANY matches anything */
00126             if (map->src.tag == TAG_ANY)
00127                 break;
00128             map++;
00129         }
00130 
00131 
00132         if (map->src.tag != TAG_END)
00133             dnotify(f->target, tag, val);
00134     }
00135     else
00136         dnotify(f->target, tag, val);
00137 }
00138 
00139 
00140 #define FILTER_MAGIC_ACTIVE 0xAA
00141 
00145 void filter_init(DFilter *f, const DFilterMap *map, bool masked, DNotifier *source, DNotifier *target)
00146 {
00147     // Init instance
00148     if (masked)
00149         f->update = (update_filter_ptr)filter_mask_update;
00150     else
00151         f->update = (update_filter_ptr)filter_update;
00152 
00153     /* set filter map and target */
00154     f->map = map;
00155     f->target = target;
00156 
00157     /* these ensure that the filter is not inserted in more than one list */
00158     ASSERT(f->magic != FILTER_MAGIC_ACTIVE);
00159     DB(f->magic = FILTER_MAGIC_ACTIVE;)
00160 
00161     /* Add the filter to source filter list */
00162     ADDTAIL(&source->targets, &f->link);
00163 }