4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief Events public header */
26 #ifndef HH_SENF_PPI_Events_
27 #define HH_SENF_PPI_Events_ 1
31 #include <senf/Scheduler/ClockService.hh>
34 //#include "Events.mpp"
35 ///////////////////////////////hh.p////////////////////////////////////////
40 namespace detail { class EventBindingBase; }
42 /** \defgroup event_group Events
44 Events provide notification of events outside the PPI framework: I/O activity, Timers
45 etc. Events are very important since they drive the PPI: Without events, nothing will
48 \section event_impl Implementing Events
50 All events are derived from EventImplementation which is based on EventDescriptor.
51 \see EventImplementation \n
55 // Implementation: The concrete EventDescriptor implementation will need to set things up so
56 // some callback (within the EventDescriptor implementation) will be called when the event
57 // happens. This setup happens in 'v_enable()'. This internal handler sets up an EventType
58 // instance if needed and calls 'callback()'.
60 // 'callback()' will access the EventBinding wrapper to find the user-callback to signal. It
61 // will do any needed internal processing, call that user callback and clean up afterwards.
63 /** \brief Generic event interface base-class
65 The EventDescriptor base-class provides an interface to control events.
72 virtual ~EventDescriptor();
74 bool enabled(); ///< Check, whether the event is currently enabled
75 void enabled(bool v); ///< Enable or disable the event
81 virtual void v_enable() = 0; ///< Called to enable the event delivery
82 virtual void v_disable() = 0; ///< Called to disable the event delivery
84 virtual bool v_isRegistered() = 0;
86 void notifyThrottle();
87 void notifyUnthrottle();
89 void registerRoute(ForwardingRoute & route);
90 void unregisterRoute(ForwardingRoute & route);
95 typedef std::vector<ForwardingRoute*> Routes;
98 detail::EventBindingBase * binding_;
100 friend class ForwardingRoute;
101 friend class detail::EventBindingBase;
104 /** \brief Internal: Callback forwarders
106 template <class EventType, class Self>
107 class EventImplementationHelper
110 typedef typename detail::EventArgType<EventType>::type EventArg;
112 void callback(EventArg event, ClockService::clock_type time);
113 ///< Forward event to user callback
114 /**< \param[in] event Event argument to pass to the user
116 \param[in] time Expected time of the event */
117 void callback(EventArg event); ///< Forward event to user callback
118 /**< \param[in] event Event argument to pass to the user
122 detail::EventBinding<EventType> & binding();
127 template <class Self>
128 class EventImplementationHelper<void,Self>
131 void callback(ClockService::clock_type time);
135 detail::EventBinding<void> & binding();
140 /** \brief Event implementation base class
142 EventImplementation provides the base-class for all Event implementations.
144 class SomeEvent : public EventImplementation<SomeEventArg>
150 virtual void v_enable() {
151 // register cb() to be called when the event occurs
154 virtual void v_disable() {
159 // Build event argument
160 SomeEventArg arg (...);
161 // Call the event callback
167 Every event needs to implement v_enable() and v_disable(). v_enable() should register some
168 member (in the example \c cb() ) to be called whenever the event occurs, while v_disable()
169 should unregister it.
171 The \a EventType argument to EventImplementation defines the type of argument passed to the
172 user callback. It defaults to \c void. . This user callback is called from within the
173 registered member (e.g. \c cb() ) by calling the inherited callback() member. This member
174 takes an \a EventType reference as argument which will be forwarded to the user callback. If
175 available, you should also provide the \e expected event time as a second argument.
177 template <class EventType>
178 class EventImplementation
179 : public EventDescriptor,
180 public EventImplementationHelper< EventType, EventImplementation<EventType> >
183 typedef EventType Event;
184 typedef typename detail::EventArgType<EventType>::type EventArg;
186 module::Module & module() const; ///< Module in which the event is registered
187 EventManager & manager() const; ///< EventManager of the event
190 EventImplementation();
193 virtual bool v_isRegistered();
194 void setBinding(detail::EventBinding<Event> & binding);
196 detail::EventBinding<Event> * binding_;
198 friend class EventManager;
199 friend class EventImplementationHelper< EventType, EventImplementation<EventType> >;
204 ///////////////////////////////hh.e////////////////////////////////////////
205 #include "Events.cci"
206 //#include "Events.ct"
207 #include "Events.cti"
214 // c-file-style: "senf"
215 // indent-tabs-mode: nil
216 // ispell-local-dictionary: "american"
217 // compile-command: "scons -u test"
218 // comment-column: 40