Line data Source code
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 : #include "message_hub.h"
34 : #include "common/assert.h"
35 : #include "common/error.h"
36 :
37 : namespace ug{
38 :
39 0 : MessageHub::CallbackEntry::
40 0 : CallbackEntry(const Callback& cb, CallbackId* cbId) :
41 : m_callback(cb),
42 0 : m_callbackId(cbId)
43 : {
44 0 : }
45 :
46 0 : MessageHub::CallbackId::
47 : CallbackId(MessageHub* hub, size_t msgTypeId,
48 0 : CallbackEntryIterator callbackEntryIter, bool autoFree) :
49 0 : m_hub(hub),
50 0 : m_msgTypeId(msgTypeId),
51 0 : m_callbackEntryIter(callbackEntryIter),
52 0 : m_autoFree(autoFree)
53 : {
54 0 : }
55 :
56 0 : MessageHub::CallbackId::~CallbackId()
57 : {
58 : // Make sure that the associated message hub still exists
59 0 : if(m_hub){
60 0 : if(m_autoFree)
61 0 : m_hub->unregister_callback_impl(this);
62 : else{
63 : // we have to set the callback-id of the associated callback-entry to
64 : // NULL, to avoid memory access errors when the associated MessageHub
65 : // is destroyed.
66 0 : m_callbackEntryIter->m_callbackId = NULL;
67 : }
68 : }
69 0 : }
70 :
71 :
72 : ////////////////////////////////////////////////////////////////////////////////
73 : // MessageHub implementation
74 1 : MessageHub::MessageHub()
75 : {
76 1 : }
77 :
78 1 : MessageHub::~MessageHub()
79 : {
80 : // we have to make sure to invalidate all associated callback-ids.
81 : // All entry-lists have to be deleted
82 1 : for(CallbackMap::iterator i_table = m_callbackMap.begin();
83 1 : i_table != m_callbackMap.end(); ++i_table)
84 : {
85 : CallbackEntryList& entryList = i_table->second;
86 0 : for(CallbackEntryList::iterator i_entry = entryList.begin();
87 0 : i_entry != entryList.end(); ++i_entry)
88 : {
89 : // if the associated callback-id is valid, then set its hub to NULL
90 0 : if(i_entry->m_callbackId != NULL)
91 0 : i_entry->m_callbackId->m_hub = NULL;
92 : }
93 : }
94 1 : }
95 :
96 0 : void MessageHub::
97 : unregister_callback(MessageHub::SPCallbackId cbId)
98 : {
99 0 : unregister_callback_impl(cbId.get());
100 0 : }
101 :
102 0 : void MessageHub::
103 : unregister_callback_impl(MessageHub::CallbackId* cbId)
104 : {
105 0 : if(cbId->m_hub == NULL){
106 : throw(Error("MessageHub::unregister_callback: Invalid callback-id. "
107 : "The callback was probably already unregistered.",
108 0 : MSG_HUB_BAD_CALLBACK_ID));
109 : }
110 :
111 : UG_ASSERT(cbId->m_hub == this, "Wrong MessageHub");
112 :
113 0 : CallbackEntryList& callbacks = m_callbackMap[cbId->m_msgTypeId];
114 :
115 : // clear the entry
116 : callbacks.erase(cbId->m_callbackEntryIter);
117 :
118 : // set the associated hub to NULL, since it was just unregistered
119 0 : cbId->m_hub = NULL;
120 0 : }
121 :
122 : }// end of namespace
|