ug4
Loading...
Searching...
No Matches
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 <functional> // for std::function
41
42#include "common/assert.h"
45#include "common/error.h"
46#include "common/log.h"
47
48namespace ug
49{
50
53
54class MessageHub;
56
58
80{
81 public:
82 // predeclarations
83 class IMessage;
84 class CallbackId;
85
86 private:
87 // private type definitions
88 typedef std::function<void (const IMessage&)> Callback;
89
91
100
101 typedef std::list<CallbackEntry> CallbackEntryList;
102 typedef CallbackEntryList::iterator CallbackEntryIterator;
103 typedef std::map<size_t, CallbackEntryList> CallbackMap;
104
105 public:
113
115
119 class Error : public UGError
120 {
121 public:
122 Error(const char* msg, ErrorIds errorId) :
123 UGError(msg), m_errorId(errorId) {}
124
126
127 protected:
129 };
130
132 class IMessage{
133 public:
135 virtual ~IMessage() {}
136 };
137
139
148 friend class MessageHub;
149
150 public:
151 ~CallbackId();
152 void set_auto_free(bool autoFree) {m_autoFree = autoFree;}
153
154 private:
155 CallbackId(MessageHub* hub, size_t msgTypeId,
156 CallbackEntryIterator callbackEntryIter, bool autoFree);
157
163 };
164
166
167 public:
168 MessageHub();
169 ~MessageHub();
170
172
182 template <class TMsg>
183 SPCallbackId register_function_callback(void (*callback)(const TMsg&),
184 bool autoFree = false);
185
187
201 template <class TMsg, class TClass>
203 void (TClass::*callback)(const TMsg&),
204 bool autoFree = true);
205
207
215
217 template <class TMsg>
218 void post_message(const TMsg& msg);
219
220 private:
222
236 template <class TMsg>
238 std::function<void (const IMessage&)> callback,
239 bool autoFree);
240
243
244 private:
246};
247
248// end group ugbase_common_util
250
251}// end of namespace
252
253
255// include implementation
256#include "message_hub_impl.hpp"
257
258#endif
Definition smart_pointer.h:107
The callback-id allows to deregister previously registered callbacks.
Definition message_hub.h:147
void set_auto_free(bool autoFree)
Definition message_hub.h:152
bool m_autoFree
Definition message_hub.h:162
size_t m_msgTypeId
Make sure to only access the iterator while m_hub != NULL.
Definition message_hub.h:160
MessageHub * m_hub
Definition message_hub.h:158
CallbackEntryIterator m_callbackEntryIter
Definition message_hub.h:161
~CallbackId()
Definition message_hub.cpp:56
Instances of this class are thrown if an error occurs in MessageHub.
Definition message_hub.h:120
ErrorIds get_message_hub_error_id()
Definition message_hub.h:125
ErrorIds m_errorId
Definition message_hub.h:128
Error(const char *msg, ErrorIds errorId)
Definition message_hub.h:122
This is the base class of all messages, which may be passed to callbacks.
Definition message_hub.h:132
IMessage()
Definition message_hub.h:134
virtual ~IMessage()
Definition message_hub.h:135
Allows to register callbacks and post messages to those callbacks.
Definition message_hub.h:80
SPCallbackId register_callback_impl(std::function< void(const IMessage &)> callback, bool autoFree)
registers a callback given a message-id.
Definition message_hub_impl.hpp:89
CallbackEntryList::iterator CallbackEntryIterator
Definition message_hub.h:102
CallbackMap m_callbackMap
given a msg-type-id, this map returns a list of associated callbacks
Definition message_hub.h:245
std::list< CallbackEntry > CallbackEntryList
Definition message_hub.h:101
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:73
void unregister_callback_impl(CallbackId *cbId)
performs unregistration of the given callback
Definition message_hub.cpp:103
SmartPtr< CallbackId > SPCallbackId
Definition message_hub.h:165
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:55
SPCallbackId register_function_callback(void(*callback)(const TMsg &), bool autoFree=false)
registers a function callback given a message-type.
Definition message_hub_impl.hpp:44
std::map< size_t, CallbackEntryList > CallbackMap
Definition message_hub.h:103
~MessageHub()
Definition message_hub.cpp:78
std::function< void(const IMessage &)> Callback
Definition message_hub.h:88
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:107
@ MSG_HUB_BAD_MESSAGE_ID
Definition message_hub.h:110
@ MSG_HUB_TYPE_MISMATCH
Definition message_hub.h:109
@ MSG_HUB_BAD_CALLBACK_ID
Definition message_hub.h:111
@ MSG_HUB_UNKNOWN_ERROR
Definition message_hub.h:108
Instances of this class or of derived classes are thrown if errors arise.
Definition error.h:104
SmartPtr< MessageHub > SPMessageHub
Definition message_hub.h:55
the ug namespace
The CallbackEntry holds the actual callback and the associated callback-id.
Definition message_hub.h:95
CallbackId * m_callbackId
Definition message_hub.h:98
Callback m_callback
Definition message_hub.h:97