Reference documentation for deal.II version Git d2d482dcec 2020-10-26 10:29:03 -0400
\(\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 - 2019 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 {
63 public:
67  Subscriptor();
68 
75  Subscriptor(const Subscriptor &);
76 
83  Subscriptor(Subscriptor &&) noexcept;
84 
88  virtual ~Subscriptor();
89 
96  Subscriptor &
97  operator=(const Subscriptor &);
98 
102  Subscriptor &
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 
199  template <class Archive>
200  void
201  serialize(Archive &ar, const unsigned int version);
202 
203 private:
219  mutable std::atomic<unsigned int> counter;
220 
225  mutable std::map<std::string, unsigned int> counter_map;
226 
230  using map_value_type = decltype(counter_map)::value_type;
231 
235  using map_iterator = decltype(counter_map)::iterator;
236 
241  mutable std::vector<std::atomic<bool> *> validity_pointers;
242 
249  mutable const std::type_info *object_info;
250 
264  void
265  check_no_subscribers() const noexcept;
266 
271  static std::mutex mutex;
272 };
273 
274 //---------------------------------------------------------------------------
275 
277  : counter(0)
278  , object_info(nullptr)
279 {}
280 
281 
282 
284  : counter(0)
285  , object_info(nullptr)
286 {}
287 
288 
289 
290 inline Subscriptor &
292 {
293  object_info = s.object_info;
294  return *this;
295 }
296 
297 
298 
299 inline unsigned int
301 {
302  return counter;
303 }
304 
305 
306 
307 template <class Archive>
308 inline void
309 Subscriptor::serialize(Archive &, const unsigned int)
310 {
311  // do nothing, as explained in the
312  // documentation of this function
313 }
314 
315 template <typename StreamType>
316 inline void
317 Subscriptor::list_subscribers(StreamType &stream) const
318 {
319  std::lock_guard<std::mutex> lock(mutex);
320 
321  for (const auto &it : counter_map)
322  stream << it.second << '/' << counter << " subscriptions from \""
323  << it.first << '\"' << std::endl;
324 }
325 
327 
328 #endif
static ::ExceptionBase & ExcInUse(int arg1, std::string arg2, std::string arg3)
#define DeclException2(Exception2, type1, type2, outsequence)
Definition: exceptions.h:538
std::vector< std::atomic< bool > * > validity_pointers
Definition: subscriptor.h:241
const std::type_info * object_info
Definition: subscriptor.h:249
Subscriptor & operator=(const Subscriptor &)
Definition: subscriptor.h:291
void unsubscribe(std::atomic< bool > *const validity, const std::string &identifier="") const
Definition: subscriptor.cc:156
STL namespace.
void list_subscribers() const
Definition: subscriptor.cc:204
void subscribe(std::atomic< bool > *const validity, const std::string &identifier="") const
Definition: subscriptor.cc:136
void serialize(Archive &ar, const unsigned int version)
Definition: subscriptor.h:309
decltype(counter_map)::value_type map_value_type
Definition: subscriptor.h:230
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:369
std::map< std::string, unsigned int > counter_map
Definition: subscriptor.h:225
unsigned int n_subscriptions() const
Definition: subscriptor.h:300
decltype(counter_map)::iterator map_iterator
Definition: subscriptor.h:235
std::atomic< unsigned int > counter
Definition: subscriptor.h:219
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:368
static std::mutex mutex
Definition: subscriptor.h:271
virtual ~Subscriptor()
Definition: subscriptor.cc:44
#define DeclException3(Exception3, type1, type2, type3, outsequence)
Definition: exceptions.h:561
static ::ExceptionBase & ExcNoSubscriber(std::string arg1, std::string arg2)
void check_no_subscribers() const noexcept
Definition: subscriptor.cc:53