Loading [MathJax]/extensions/tex2jax.js
ug4
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
message_hub.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2015: G-CSC, Goethe University Frankfurt
3 * Author: Sebastian Reiter
4 *
5 * This file is part of UG4.
6 *
7 * UG4 is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License version 3 (as published by the
9 * Free Software Foundation) with the following additional attribution
10 * requirements (according to LGPL/GPL v3 §7):
11 *
12 * (1) The following notice must be displayed in the Appropriate Legal Notices
13 * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 *
15 * (2) The following notice must be displayed at a prominent place in the
16 * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 *
18 * (3) The following bibliography is recommended for citation and must be
19 * preserved in all covered files:
20 * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 * parallel geometric multigrid solver on hierarchically distributed grids.
22 * Computing and visualization in science 16, 4 (2013), 151-164"
23 * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 * flexible software system for simulating pde based models on high performance
25 * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 *
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU Lesser General Public License for more details.
31 */
32
33#ifndef __H__UG__message_hub__
34#define __H__UG__message_hub__
35
36#include <vector>
37#include <list>
38#include <string>
39#include <map>
40#include <boost/function.hpp>
41#include <boost/function_equal.hpp>
42#include <boost/bind.hpp>
43#include "common/assert.h"
46#include "common/error.h"
47#include "common/log.h"
48
49namespace ug
50{
51
54
55class MessageHub;
57
59
81{
82 public:
83 // predeclarations
84 class IMessage;
85 class CallbackId;
86
87 private:
88 // private type definitions
89 typedef boost::function<void (const IMessage&)> Callback;
90
92
101
102 typedef std::list<CallbackEntry> CallbackEntryList;
103 typedef CallbackEntryList::iterator CallbackEntryIterator;
104 typedef std::map<size_t, CallbackEntryList> CallbackMap;
105
106 public:
114
116
120 class Error : public UGError
121 {
122 public:
123 Error(const char* msg, ErrorIds errorId) :
124 UGError(msg), m_errorId(errorId) {}
125
127
128 protected:
130 };
131
133 class IMessage{
134 public:
136 virtual ~IMessage() {}
137 };
138
140
149 friend class MessageHub;
150
151 public:
152 ~CallbackId();
153 void set_auto_free(bool autoFree) {m_autoFree = autoFree;}
154
155 private:
156 CallbackId(MessageHub* hub, size_t msgTypeId,
157 CallbackEntryIterator callbackEntryIter, bool autoFree);
158
164 };
165
167
168 public:
169 MessageHub();
170 ~MessageHub();
171
173
183 template <class TMsg>
184 SPCallbackId register_function_callback(void (*callback)(const TMsg&),
185 bool autoFree = false);
186
188
202 template <class TMsg, class TClass>
204 void (TClass::*callback)(const TMsg&),
205 bool autoFree = true);
206
208
216
218 template <class TMsg>
219 void post_message(const TMsg& msg);
220
221 private:
223
237 template <class TMsg>
239 boost::function<void (const IMessage&)> callback,
240 bool autoFree);
241
244
245 private:
247};
248
249// end group ugbase_common_util
251
252}// end of namespace
253
254
256// include implementation
257#include "message_hub_impl.hpp"
258
259#endif
Definition smart_pointer.h:108
The callback-id allows to deregister previously registered callbacks.
Definition message_hub.h:148
void set_auto_free(bool autoFree)
Definition message_hub.h:153
bool m_autoFree
Definition message_hub.h:163
size_t m_msgTypeId
Make sure to only access the iterator while m_hub != NULL.
Definition message_hub.h:161
MessageHub * m_hub
Definition message_hub.h:159
CallbackEntryIterator m_callbackEntryIter
Definition message_hub.h:162
~CallbackId()
Definition message_hub.cpp:56
Instances of this class are thrown if an error occurs in MessageHub.
Definition message_hub.h:121
ErrorIds get_message_hub_error_id()
Definition message_hub.h:126
ErrorIds m_errorId
Definition message_hub.h:129
Error(const char *msg, ErrorIds errorId)
Definition message_hub.h:123
This is the base class of all messages, which may be passed to callbacks.
Definition message_hub.h:133
IMessage()
Definition message_hub.h:135
virtual ~IMessage()
Definition message_hub.h:136
Allows to register callbacks and post messages to those callbacks.
Definition message_hub.h:81
CallbackEntryList::iterator CallbackEntryIterator
Definition message_hub.h:103
CallbackMap m_callbackMap
given a msg-type-id, this map returns a list of associated callbacks
Definition message_hub.h:246
std::list< CallbackEntry > CallbackEntryList
Definition message_hub.h:102
void post_message(const TMsg &msg)
Posts a message to all callbacks which are registered for the given message tpye.
Definition message_hub_impl.hpp:67
void unregister_callback_impl(CallbackId *cbId)
performs unregistration of the given callback
Definition message_hub.cpp:103
SPCallbackId register_callback_impl(boost::function< void(const IMessage &)> callback, bool autoFree)
registers a callback given a message-id.
Definition message_hub_impl.hpp:83
SmartPtr< CallbackId > SPCallbackId
Definition message_hub.h:166
MessageHub()
Definition message_hub.cpp:74
SPCallbackId register_class_callback(TClass *cls, void(TClass::*callback)(const TMsg &), bool autoFree=true)
registers a method callback given a message-type.
Definition message_hub_impl.hpp:53
SPCallbackId register_function_callback(void(*callback)(const TMsg &), bool autoFree=false)
registers a function callback given a message-type.
Definition message_hub_impl.hpp:42
std::map< size_t, CallbackEntryList > CallbackMap
Definition message_hub.h:104
boost::function< void(const IMessage &)> Callback
Definition message_hub.h:89
~MessageHub()
Definition message_hub.cpp:78
void unregister_callback(SPCallbackId cbId)
Call this method to explicitly unregister a callback.
Definition message_hub.cpp:97
ErrorIds
Error codes which give information on the error-reason.
Definition message_hub.h:108
@ MSG_HUB_BAD_MESSAGE_ID
Definition message_hub.h:111
@ MSG_HUB_TYPE_MISMATCH
Definition message_hub.h:110
@ MSG_HUB_BAD_CALLBACK_ID
Definition message_hub.h:112
@ MSG_HUB_UNKNOWN_ERROR
Definition message_hub.h:109
Instances of this class or of derived classes are thrown if errors arise.
Definition error.h:104
SmartPtr< MessageHub > SPMessageHub
Definition message_hub.h:56
the ug namespace
The CallbackEntry holds the actual callback and the associated callback-id.
Definition message_hub.h:96
CallbackId * m_callbackId
Definition message_hub.h:99
Callback m_callback
Definition message_hub.h:98