eventsource.h
Go to the documentation of this file.
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 #ifndef CXXTOOLS_EVENTSOURCE_H
30 #define CXXTOOLS_EVENTSOURCE_H
31 
32 #include <cxxtools/event.h>
33 #include <cxxtools/signal.h>
34 #include <cxxtools/api.h>
35 #include <cxxtools/mutex.h>
36 #include <cxxtools/eventsink.h>
37 #include <map>
38 #include <typeinfo>
39 
40 namespace cxxtools {
41 
42 class EventSink;
43 
54 {
55  friend class EventSink;
56 
57  public:
58  EventSource();
59 
60  ~EventSource();
61 
62  void send(const cxxtools::Event& ev);
63 
64  void connect(EventSink& sink);
65 
66  void disconnect(EventSink& sink);
67 
68  template <typename EventT>
69  void subscribe(EventSink& sink)
70  {
71  subscribe( sink, typeid(EventT) );
72  }
73 
74  template <typename EventT>
75  void unsubscribe(EventSink& sink)
76  {
77  unsubscribe( sink, typeid(EventT) );
78  }
79 
80  private:
81  bool tryDisconnect(EventSink& sink);
82 
83  void subscribe(EventSink& sink, const std::type_info& ti);
84 
85  void unsubscribe(EventSink& sink, const std::type_info& ti);
86 
87  private:
88  struct Sentry;
89 
90  typedef std::multimap< const std::type_info*,
91  EventSink*,
92  CompareEventTypeInfo > SinkMap;
93 
94  mutable RecursiveMutex _mutex;
95  mutable RecursiveMutex* _dmutex;
96  mutable SinkMap _sinks;
97  mutable Sentry* _sentry;
98  mutable bool _dirty;
99 };
100 
101 } // namespace cxxtools
102 
103 #endif
104