EventSource.h
1 /*
2  * Copyright (C) 2006-2007 Laurentiu-Gheorghe Crisan
3  * Copyright (C) 2006-2008 Marc Boris Duerner
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * As a special exception, you may use this file as part of a free
11  * software library without restriction. Specifically, if other files
12  * instantiate templates or use macros or inline functions from this
13  * file, or you compile this file and link it with other files to
14  * produce an executable, this file does not by itself cause the
15  * resulting executable to be covered by the GNU General Public
16  * License. This exception does not however invalidate any other
17  * reasons why the executable file might be covered by the GNU Library
18  * General Public License.
19  *
20  * This library is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  * Lesser General Public License for more details.
24  *
25  * You should have received a copy of the GNU Lesser General Public
26  * License along with this library; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28  */
29 
30 #ifndef PT_SYSTEM_EVENTSOURCE_H
31 #define PT_SYSTEM_EVENTSOURCE_H
32 
33 #include <Pt/System/Api.h>
34 #include <Pt/System/Mutex.h>
35 #include <Pt/System/EventSink.h>
36 #include <Pt/Event.h>
37 #include <Pt/Signal.h>
38 #include <map>
39 #include <typeinfo>
40 
41 namespace Pt {
42 
43 namespace System {
44 
45 class EventSink;
46 
54 class PT_SYSTEM_API EventSource : private NonCopyable
55 {
56  friend class EventSink;
57 
58  public:
61  EventSource();
62 
65  ~EventSource();
66 
69  void send(const Pt::Event& ev);
70 
73  void connect(EventSink& sink);
74 
77  void disconnect(EventSink& sink);
78 
81  template <typename EventT>
82  void subscribe(EventSink& sink)
83  {
84  subscribe( sink, typeid(EventT) );
85  }
86 
89  template <typename EventT>
90  void unsubscribe(EventSink& sink)
91  {
92  unsubscribe( sink, typeid(EventT) );
93  }
94 
95  private:
97  bool tryDisconnect(EventSink& sink);
98 
100  void subscribe(EventSink& sink, const std::type_info& ti);
101 
103  void unsubscribe(EventSink& sink, const std::type_info& ti);
104 
105  private:
106  struct Sentry;
107 
108  typedef std::multimap< const std::type_info*,
109  EventSink*,
110  CompareEventTypeInfo > SinkMap;
111 
112  mutable RecursiveMutex _mutex;
113  mutable RecursiveMutex* _dmutex;
114  mutable SinkMap _sinks;
115  mutable Sentry* _sentry;
116  mutable bool _dirty;
117 };
118 
119 } // namespace System
120 
121 } // namespace Pt
122 
123 #endif // PT_SYSTEM_EVENTSOURCE_H
void subscribe(EventSink &sink)
Send events of a certain type to the sink.
Definition: EventSource.h:82
Protects derived classes from being copied.
Definition: NonCopyable.h:54
Recursive mutual exclusion device.
Definition: Mutex.h:187
Receiver for events.
Definition: EventSink.h:45
Base class for all event types.
Definition: Event.h:49
Sends Events to receivers in other threads.
Definition: EventSource.h:54
void unsubscribe(EventSink &sink)
Do not send events of a certain type to the sink anymore.
Definition: EventSource.h:90