Reference documentation for deal.II version 9.3.3
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
subscriptor.h
Go to the documentation of this file.
1// ---------------------------------------------------------------------
2//
3// Copyright (C) 1998 - 2021 by the deal.II authors
4//
5// This file is part of the deal.II library.
6//
7// The deal.II library is free software; you can use it, redistribute
8// it, and/or modify it under the terms of the GNU Lesser General
9// Public License as published by the Free Software Foundation; either
10// version 2.1 of the License, or (at your option) any later version.
11// The full text of the license can be found in the file LICENSE.md at
12// the top level directory of deal.II.
13//
14// ---------------------------------------------------------------------
15
16#ifndef dealii_subscriptor_h
17#define dealii_subscriptor_h
18
19
20#include <deal.II/base/config.h>
21
23
24#include <atomic>
25#include <cstring>
26#include <map>
27#include <mutex>
28#include <string>
29#include <typeinfo>
30#include <vector>
31
33
62{
63public:
68
75 Subscriptor(const Subscriptor &);
76
83 Subscriptor(Subscriptor &&) noexcept;
84
88 virtual ~Subscriptor();
89
97 operator=(const Subscriptor &);
98
103 operator=(Subscriptor &&) noexcept;
104
111 // @{
112
117 void
118 subscribe(std::atomic<bool> *const validity,
119 const std::string & identifier = "") const;
120
127 void
128 unsubscribe(std::atomic<bool> *const validity,
129 const std::string & identifier = "") const;
130
136 unsigned int
137 n_subscriptions() const;
138
142 template <typename StreamType>
143 void
144 list_subscribers(StreamType &stream) const;
145
149 void
150 list_subscribers() const;
151
152 // @}
153
163 int,
164 std::string,
165 std::string,
166 << "Object of class " << arg2 << " is still used by " << arg1
167 << " other objects."
168 << "\n\n"
169 << "(Additional information: " << arg3 << ")\n\n"
170 << "See the entry in the Frequently Asked Questions of "
171 << "deal.II (linked to from http://www.dealii.org/) for "
172 << "a lot more information on what this error means and "
173 << "how to fix programs in which it happens.");
174
180 std::string,
181 std::string,
182 << "No subscriber with identifier <" << arg2
183 << "> subscribes to this object of class " << arg1
184 << ". Consequently, it cannot be unsubscribed.");
186
200 template <class Archive>
201 void
202 serialize(Archive &ar, const unsigned int version);
203
204private:
220 mutable std::atomic<unsigned int> counter;
221
226 mutable std::map<std::string, unsigned int> counter_map;
227
231 using map_value_type = decltype(counter_map)::value_type;
232
236 using map_iterator = decltype(counter_map)::iterator;
237
242 mutable std::vector<std::atomic<bool> *> validity_pointers;
243
250 mutable const std::type_info *object_info;
251
265 void
266 check_no_subscribers() const noexcept;
267
272 static std::mutex mutex;
273};
274
275//---------------------------------------------------------------------------
276
278 : counter(0)
279 , object_info(nullptr)
280{}
281
282
283
285 : counter(0)
286 , object_info(nullptr)
287{}
288
289
290
291inline Subscriptor &
293{
295 return *this;
296}
297
298
299
300inline unsigned int
302{
303 return counter;
304}
305
306
307
308template <class Archive>
309inline void
310Subscriptor::serialize(Archive &, const unsigned int)
311{
312 // do nothing, as explained in the
313 // documentation of this function
314}
315
316template <typename StreamType>
317inline void
318Subscriptor::list_subscribers(StreamType &stream) const
319{
320 std::lock_guard<std::mutex> lock(mutex);
321
322 for (const auto &it : counter_map)
323 stream << it.second << '/' << counter << " subscriptions from \""
324 << it.first << '\"' << std::endl;
325}
326
328
329#endif
Subscriptor & operator=(const Subscriptor &)
Definition: subscriptor.h:292
virtual ~Subscriptor()
Definition: subscriptor.cc:44
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:402
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:403
std::vector< std::atomic< bool > * > validity_pointers
Definition: subscriptor.h:242
void unsubscribe(std::atomic< bool > *const validity, const std::string &identifier="") const
Definition: subscriptor.cc:156
std::atomic< unsigned int > counter
Definition: subscriptor.h:220
void check_no_subscribers() const noexcept
Definition: subscriptor.cc:53
void list_subscribers() const
Definition: subscriptor.cc:204
const std::type_info * object_info
Definition: subscriptor.h:250
void subscribe(std::atomic< bool > *const validity, const std::string &identifier="") const
Definition: subscriptor.cc:136
static ::ExceptionBase & ExcInUse(int arg1, std::string arg2, std::string arg3)
void serialize(Archive &ar, const unsigned int version)
Definition: subscriptor.h:310
std::map< std::string, unsigned int > counter_map
Definition: subscriptor.h:226
static std::mutex mutex
Definition: subscriptor.h:272
#define DeclException2(Exception2, type1, type2, outsequence)
Definition: exceptions.h:538
static ::ExceptionBase & ExcNoSubscriber(std::string arg1, std::string arg2)
#define DeclException3(Exception3, type1, type2, type3, outsequence)
Definition: exceptions.h:561
decltype(counter_map)::iterator map_iterator
Definition: subscriptor.h:236
decltype(counter_map)::value_type map_value_type
Definition: subscriptor.h:231
unsigned int n_subscriptions() const
Definition: subscriptor.h:301
STL namespace.