Doc fixes.
[bertos.git] / dt / dnotifier.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2005 Develer S.r.l. (http://www.develer.com/)
30  * -->
31  *
32  * \version $Id$
33  *
34  * \brief Notifier obj (interface).
35  *
36  * \version $Id$
37  * \author Bernardo Innocenti <bernie@develer.com>
38  * \author Francesco Sacchi <batt@develer.com>
39  */
40
41 /*#*
42  *#* $Log$
43  *#* Revision 1.2  2006/07/19 12:56:26  bernie
44  *#* Convert to new Doxygen style.
45  *#*
46  *#* Revision 1.1  2005/11/04 18:26:38  bernie
47  *#* Import into DevLib.
48  *#*
49  *#* Revision 1.4  2005/06/09 13:23:58  batt
50  *#* Add some comments.
51  *#*
52  *#* Revision 1.3  2005/06/08 17:32:33  batt
53  *#* Switch to new messaging system.
54  *#*
55  *#* Revision 1.2  2005/06/06 11:04:12  batt
56  *#* Add some comments.
57  *#*
58  *#* Revision 1.1  2005/05/26 08:32:53  batt
59  *#* Add new Develer widget system :)
60  *#*
61  *#*/
62 #ifndef DT_DNOTIFIER_H
63 #define DT_DNOTIFIER_H
64
65 #include <cfg/debug.h>
66 #include <dt/dtag.h>
67 #include <mware/list.h>
68
69 //Fwd declaretion.
70 struct DNotifier;
71 struct DFilter;
72
73 typedef void (* update_func_ptr)(struct DNotifier *, dtag_t, dval_t);
74 typedef void (* update_filter_ptr)(struct DFilter *, dtag_t, dval_t);
75
76 /**
77  * Base object for receive and forward messages.
78  * It contains an update function used to update itslef and a list to
79  * notify other DNotifer eventually connected.
80  */
81 typedef struct DNotifier
82 {
83         /// Receive new attributes from other notifiers.
84         update_func_ptr update;
85
86         /// List of target notifiers to set new attributes to.
87         List targets;
88 } DNotifier;
89
90 /**
91  * Map for messages.
92  * Used to translate src message to dst message.
93  */
94 typedef struct DFilterMap
95 {
96         DTagItem src;
97         DTagItem dst;
98 } DFilterMap;
99
100
101 /**
102  * A filter is an interface between two notifier.
103  * It can translate messages between them through a map (if it is not null).
104  */
105 typedef struct DFilter
106 {
107         /// Allow creating a list of dfilter objects.
108         Node link;
109
110         /// Target of the filter
111         DNotifier *target;
112
113         /// Update function called by the source dnotifier
114         update_filter_ptr update;
115
116         ///Map for translating messages for target
117         const DFilterMap *map;
118
119         ///Used in debug to prevent inserting this filter in more than one list
120         DB(uint8_t magic;)
121 } DFilter;
122
123 /// Type for filter-mask checking
124 typedef uint16_t dfilter_mask_t;
125
126 /// Filter init
127 void filter_init(DFilter *f, const DFilterMap *map, bool masked, DNotifier *source, DNotifier *target);
128
129 /// Filter update function without masking capabilities.
130 void filter_update(DFilter *f, dtag_t tag, dval_t val);
131
132 /// Filter update function with masking capabilities.
133 void filter_mask_update(DFilter *f, dtag_t tag, dval_t val);
134
135 /// Notifier init
136 void notifier_init(DNotifier *n);
137
138
139 /**
140  * Macro to notify the target object.
141  */
142 INLINE void dnotify(DNotifier *target, dtag_t tag, dval_t val)
143 {
144         if (target)
145                 target->update(target, tag, val);
146 }
147
148 /**
149  * Macro to notify all the targets of \a target object.
150  */
151 INLINE void dnotify_targets(DNotifier *target, dtag_t tag, dval_t val)
152 {
153         DFilter *f;
154         if (!ISLISTEMPTY(&target->targets))
155                 FOREACHNODE(f, &target->targets)
156                         f->update(f, tag, val);
157 }
158
159
160 /**
161  * Macro that connect \a src notifier to \a tgt using \a map and passing \a opt for filtering option.
162  * It declares a static filter to achieve connection and messages translation.
163  * \note Due its static filter declaration, DCONNECT MUST NOT be used inside loops or in functions called multiple times.
164  * Failing to do so will lead to unpredictable connections between notifiers.
165  */
166 #define DCONNECT(src, tgt, map, opt) \
167         do { \
168                 static DFilter _filter_; /* Declare a filter */ \
169                 filter_init(&(_filter_), map, opt, src, tgt); /* Init it. */ \
170         } while (0)
171
172
173 #endif /* DT_DNOTIFIER_H */