set keyword svn property on more files
[senf.git] / PPI / Events.hh
1 // $Id$
2 //
3 // Copyright (C) 2007 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS) 
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY 
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief Events public header */
25
26 #ifndef HH_Events_
27 #define HH_Events_ 1
28
29 // Custom includes
30 #include <vector>
31 #include "../Scheduler/ClockService.hh"
32 #include "predecl.hh"
33
34 //#include "Events.mpp"
35 ///////////////////////////////hh.p////////////////////////////////////////
36
37 namespace senf {
38 namespace ppi {
39     
40     /** \defgroup event_group Events
41
42         Events provide notification of events outside the PPI framework: I/O activity, Timers
43         etc. Events are very important since they drive the PPI: Without events, nothing will
44         happen.
45
46         \section event_impl Implementing Events
47
48         All events are derived from EventImplementation which is based on EventDescriptor.
49         \see EventImplementation \n
50             \ref ppi_events        
51      */
52
53     // Implementation: The concrete EventDescriptor implementation will need to set things up so
54     // some callback (within the EventDescriptor implementation) will be called when the event
55     // happens. This setup happens in 'v_enable()'. This internal handler sets up an EventType
56     // instance if needed and calls 'callback()'. 
57     //
58     // 'callback()' will access the EventBinding wrapper to find the user-callback to signal. It
59     // will do any needed internal processing, call that user callback and clean up afterwards.
60
61     /** \brief Generic event interface base-class
62
63         The EventDescriptor base-class provides an interface to control events.
64
65         \see \ref ppi_events
66      */ 
67     class EventDescriptor
68     {
69     public:
70         virtual ~EventDescriptor();
71
72         bool enabled(); ///< Check, whether the event is currently enabled
73         void enabled(bool v); ///< Enable or disable the event
74
75     protected:
76         EventDescriptor();
77
78     private:
79         virtual void v_enable() = 0;    ///< Called to enable the event delivery
80         virtual void v_disable() = 0;   ///< Called to disable the event delivery
81
82         virtual bool v_isRegistered() = 0;
83
84         void notifyThrottle();
85         void notifyUnthrottle();
86
87         void registerRoute(ForwardingRoute & route);
88
89         bool enabled_;
90
91         typedef std::vector<ForwardingRoute*> Routes;
92         Routes routes_;
93
94         friend class ForwardingRoute;
95     };
96     
97     /** \brief Internal: Callback forwarders
98      */
99     template <class EventType, class Self>
100     class EventImplementationHelper
101     {
102     protected:
103         typedef typename detail::EventArgType<EventType>::type EventArg;
104
105         void callback(EventArg event, ClockService::clock_type time);
106                                         ///< Forward event to user callback
107                                         /**< \param[in] event Event argument to pass to the user
108                                              callback
109                                              \param[in] time Expected time of the event */
110         void callback(EventArg event);  ///< Forward event to user callback
111                                         /**< \param[in] event Event argument to pass to the user
112                                              callback. */
113
114     private:
115         detail::EventBinding<EventType> & binding();
116     };
117     
118 #ifndef DOXYGEN
119
120     template <class Self>
121     class EventImplementationHelper<void,Self>
122     {
123     protected:
124         void callback(ClockService::clock_type time);
125         void callback();
126
127     private:
128         detail::EventBinding<void> & binding();
129     };
130
131 #endif
132
133     /** \brief Event implementation base class
134
135         EventImplementation provides the base-class for all Event implementations. 
136         \code
137         class SomeEvent : public EventImplementation<SomeEventArg>
138         {
139         public:
140             SomeEvent() {}
141
142         private:
143             virtual void v_enable() {
144                 // register cb() to be called when the event occurs
145             }
146
147             virtual void v_disable() {
148                 // unregister cb()
149             }
150
151             void cb() {
152                 // Build event argument
153                 SomeEventArg arg (...); 
154                 // Call the event callback
155                 callback(arg);
156             }
157         };
158         \endcode
159
160         Every event needs to implement v_enable() and v_disable(). v_enable() should register some
161         member (in the example \c cb() ) to be called whenever the event occurs, while v_disable()
162         should unregister it.
163
164         The \a EventType argument to EventImplementation defines the type of argument passed to the
165         user callback. It defaults to \c void. . This user callback is called from within the
166         registered member (e.g. \c cb() ) by calling the inherited callback() member. This member
167         takes an \a EventType reference as argument which will be forwarded to the user callback. If
168         available, you should also provide the \e expected event time as a second argument.
169      */
170     template <class EventType>
171     class EventImplementation
172         : public EventDescriptor, 
173           public EventImplementationHelper< EventType, EventImplementation<EventType> >
174     {
175     public:
176         typedef EventType Event;
177         typedef typename detail::EventArgType<EventType>::type EventArg;
178
179         module::Module & module() const; ///< Module in which the event is registered
180         EventManager & manager() const; ///< EventManager of the event
181         
182     protected:
183         EventImplementation();
184
185     private:
186         virtual bool v_isRegistered();
187         void setBinding(detail::EventBinding<Event> & binding);
188
189         detail::EventBinding<Event> * binding_;
190
191         friend class EventManager;
192         friend class EventImplementationHelper< EventType, EventImplementation<EventType> >;
193     };
194
195 }}
196
197 ///////////////////////////////hh.e////////////////////////////////////////
198 #include "Events.cci"
199 //#include "Events.ct"
200 #include "Events.cti"
201 #endif
202
203 \f
204 // Local Variables:
205 // mode: c++
206 // fill-column: 100
207 // c-file-style: "senf"
208 // indent-tabs-mode: nil
209 // ispell-local-dictionary: "american"
210 // compile-command: "scons -u test"
211 // comment-column: 40
212 // End: