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
41namespace Pt {
42
43namespace System {
44
45class EventSink;
46
61class PT_SYSTEM_API EventSource : private NonCopyable
62{
63 friend class EventSink;
64
65 public:
69
73
76 void send(const Pt::Event& ev);
77
80 void connect(EventSink& sink);
81
84 void disconnect(EventSink& sink);
85
88 template <typename EventT>
89 void subscribe(EventSink& sink)
90 {
91 subscribe( sink, typeid(EventT) );
92 }
93
96 template <typename EventT>
97 void unsubscribe(EventSink& sink)
98 {
99 unsubscribe( sink, typeid(EventT) );
100 }
101
102 private:
104 bool tryDisconnect(EventSink& sink);
105
107 void subscribe(EventSink& sink, const std::type_info& ti);
108
110 void unsubscribe(EventSink& sink, const std::type_info& ti);
111
112 private:
113 struct Sentry;
114
115 typedef std::multimap< const std::type_info*,
116 EventSink*,
117 CompareEventTypeInfo > SinkMap;
118
119 mutable RecursiveMutex _mutex;
120 mutable RecursiveMutex* _dmutex;
121 mutable SinkMap _sinks;
122 mutable Sentry* _sentry;
123 mutable bool _dirty;
124};
125
126} // namespace System
127
128} // namespace Pt
129
130#endif // PT_SYSTEM_EVENTSOURCE_H
Base class for typed events.
Definition Event.h:66
NonCopyable()
Default constructor.
Definition NonCopyable.h:63
Receiver for events.
Definition EventSink.h:56
void disconnect(EventSink &sink)
Disonnect from an EventSink.
void subscribe(EventSink &sink)
Send events of a certain type to the sink.
Definition EventSource.h:89
void connect(EventSink &sink)
Connect to an EventSink.
EventSource()
Default Constructor.
~EventSource()
Destructor.
void send(const Pt::Event &ev)
Send the event to all connected sinks.
void unsubscribe(EventSink &sink)
Do not send events of a certain type to the sink anymore.
Definition EventSource.h:97
Recursive mutual exclusion device.
Definition Mutex.h:190
System programming
Definition Connection.h:26
Core module.
Definition Allocator.h:33