OpenDDS  Snapshot(2023/04/28-20:55)
EventDispatcher.h
Go to the documentation of this file.
1 /*
2  *
3  *
4  * Distributed under the OpenDDS License.
5  * See: http://www.opendds.org/license.html
6  */
7 
8 #ifndef OPENDDS_DCPS_EVENT_DISPATCHER_H
9 #define OPENDDS_DCPS_EVENT_DISPATCHER_H
10 
11 #include "Definitions.h"
12 #include "RcObject.h"
13 #include "TimeTypes.h"
14 
16 
17 namespace OpenDDS {
18 namespace DCPS {
19 
20 /**
21  * EventBase is an abstract base class for events
22  *
23  * EventBase is an abstract base class for events dispatched or scheduled by
24  * an EventDispatcher. Derived classes are required to implement handle_event
25  * which will be called when the event is dispatched by the event dispatcher
26  */
28  virtual ~EventBase();
29 
30  /// Called when the event is dispatched by an EventDispatcher
31  virtual void handle_event() = 0;
32 
33  /// Only called when an exception is caught during handle_event
34  virtual void handle_error();
35 
36  /// Called when the dispatch or schedule of an event is canceled by an
37  /// EventDispatcher, for example when immediate shutdown is requested
38  virtual void handle_cancel();
39 
40  void operator()();
41 };
43 
44 /**
45  * PmfEvent is a helper class for adapting void member funtions of existing
46  * classes into dispatchable events
47  */
48 template <typename Delegate>
49 class PmfEvent : public EventBase {
50 public:
51  typedef void (Delegate::*PMF)();
52 
53  PmfEvent(RcHandle<Delegate> delegate, PMF function)
54  : delegate_(delegate)
55  , function_(function)
56  {}
57 
58  void handle_event()
59  {
60  RcHandle<Delegate> handle = delegate_.lock();
61  if (handle) {
62  ((*handle).*function_)();
63  }
64  }
65 
66 private:
68  PMF function_;
69 };
70 
71 /**
72  * PmfNowEvent is a helper class for adapting MonotonicTimePoint-accepting member
73  * funtions of existing classes into dispatchable events. (c.f. PmfEvent)
74  */
75 template <typename Delegate>
76 class PmfNowEvent : public EventBase {
77 public:
78  typedef void (Delegate::*PMF)(const MonotonicTimePoint&);
79 
80  PmfNowEvent(RcHandle<Delegate> delegate, PMF function)
81  : delegate_(delegate)
82  , function_(function)
83  {}
84 
85  void handle_event()
86  {
87  RcHandle<Delegate> handle = delegate_.lock();
88  if (handle) {
89  ((*handle).*function_)(MonotonicTimePoint::now());
90  }
91  }
92 
93 private:
95  PMF function_;
96 };
97 
98 /**
99  * EventDispatcher is an interface for the scheduling and dispatching of events
100  *
101  * Implementations of EventDispatcher will accept reference counted handles to
102  * event objects derived from EventBase either for immediate dispatch (dispatch)
103  * or for future scheduled dispatch (schedule). Scheduled dispatches return id
104  * values which can be used to cancel the dispatch before the scheduled time.
105  */
107 public:
108 
109  EventDispatcher();
110  virtual ~EventDispatcher();
111 
112  /**
113  * Request shutdown of this EventDispatcher, which prevents sucessful future
114  * calls to either dispatch or schedule and cancels all scheduled events.
115  * @param immediate prevent any further dispatches from event queue, otherwise allow current queue to empty
116  */
117  virtual void shutdown(bool immediate = false) = 0;
118 
119  /**
120  * Dispatch an event.
121  * @param event the event to dispatch
122  * @return true if event successfully enqueue, otherwise false
123  */
124  virtual bool dispatch(EventBase_rch event) = 0;
125 
126  /**
127  * Schedule the future dispatch of an event.
128  * @param event the event to dispatch
129  * @param expiration the requested dispatch time (no earlier than)
130  * @return -1 on a failure, otherwise the timer id for the future dispatch
131  */
132  virtual long schedule(EventBase_rch event, const MonotonicTimePoint& expiration = MonotonicTimePoint::now()) = 0;
133 
134  /**
135  * Cancel a scheduled event dispatch.
136  * @param id the id of the event timer to cancel
137  * @return the number of canceled events (0 for failure to cancel, 1 for success)
138  */
139  virtual size_t cancel(long id) = 0;
140 
141 };
143 
144 } // DCPS
145 } // OpenDDS
146 
148 
149 #endif // OPENDDS_DCPS_EVENT_DISPATCHER_H
PmfNowEvent(RcHandle< Delegate > delegate, PMF function)
RcHandle< EventDispatcher > EventDispatcher_rch
#define OpenDDS_Dcps_Export
Definition: dcps_export.h:24
RcHandle< EventBase > EventBase_rch
static TimePoint_T< MonotonicClock > now()
Definition: TimePoint_T.inl:41
WeakRcHandle< Delegate > delegate_
void handle_event()
Called when the event is dispatched by an EventDispatcher.
void handle_event()
Called when the event is dispatched by an EventDispatcher.
WeakRcHandle< Delegate > delegate_
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
int shutdown(ACE_HANDLE handle, int how)
PmfEvent(RcHandle< Delegate > delegate, PMF function)
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28