Reference documentation for deal.II version 9.1.1
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
subscriptor.h
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 
22 #include <deal.II/base/exceptions.h>
23 
24 #include <atomic>
25 #include <cstring>
26 #include <map>
27 #include <mutex>
28 #include <string>
29 #include <typeinfo>
30 #include <vector>
31 
32 DEAL_II_NAMESPACE_OPEN
33 
63 {
64 public:
68  Subscriptor();
69 
76  Subscriptor(const Subscriptor &);
77 
84  Subscriptor(Subscriptor &&) noexcept;
85 
89  virtual ~Subscriptor();
90 
97  Subscriptor &
98  operator=(const Subscriptor &);
99 
103  Subscriptor &
104  operator=(Subscriptor &&) noexcept;
105 
110  void
111  subscribe(std::atomic<bool> *const validity,
112  const std::string & identifier = "") const;
113 
120  void
121  unsubscribe(std::atomic<bool> *const validity,
122  const std::string & identifier = "") const;
123 
129  unsigned int
130  n_subscriptions() const;
131 
135  template <typename StreamType>
136  void
137  list_subscribers(StreamType &stream) const;
138 
142  void
143  list_subscribers() const;
144 
154  int,
155  std::string,
156  std::string,
157  << "Object of class " << arg2 << " is still used by " << arg1
158  << " other objects."
159  << "\n\n"
160  << "(Additional information: " << arg3 << ")\n\n"
161  << "See the entry in the Frequently Asked Questions of "
162  << "deal.II (linked to from http://www.dealii.org/) for "
163  << "a lot more information on what this error means and "
164  << "how to fix programs in which it happens.");
165 
171  std::string,
172  std::string,
173  << "No subscriber with identifier <" << arg2
174  << "> subscribes to this object of class " << arg1
175  << ". Consequently, it cannot be unsubscribed.");
177 
190  template <class Archive>
191  void
192  serialize(Archive &ar, const unsigned int version);
193 
194 private:
211  mutable std::atomic<unsigned int> counter;
212 
217  mutable std::map<std::string, unsigned int> counter_map;
218 
222  using map_value_type = decltype(counter_map)::value_type;
223 
227  using map_iterator = decltype(counter_map)::iterator;
228 
233  mutable std::vector<std::atomic<bool> *> validity_pointers;
234 
241  mutable const std::type_info *object_info;
242 
256  void
257  check_no_subscribers() const noexcept;
258 
263  static std::mutex mutex;
264 };
265 
266 //---------------------------------------------------------------------------
267 
269  : counter(0)
270  , object_info(nullptr)
271 {}
272 
273 
274 
276  : counter(0)
277  , object_info(nullptr)
278 {}
279 
280 
281 
282 inline Subscriptor &
284 {
286  return *this;
287 }
288 
289 
290 
291 inline unsigned int
293 {
294  return counter;
295 }
296 
297 
298 
299 template <class Archive>
300 inline void
301 Subscriptor::serialize(Archive &, const unsigned int)
302 {
303  // do nothing, as explained in the
304  // documentation of this function
305 }
306 
307 template <typename StreamType>
308 inline void
309 Subscriptor::list_subscribers(StreamType &stream) const
310 {
311  std::lock_guard<std::mutex> lock(mutex);
312 
313  for (const auto &it : counter_map)
314  stream << it.second << '/' << counter << " subscriptions from \""
315  << it.first << '\"' << std::endl;
316 }
317 
318 DEAL_II_NAMESPACE_CLOSE
319 
320 #endif
static ::ExceptionBase & ExcInUse(int arg1, std::string arg2, std::string arg3)
#define DeclException2(Exception2, type1, type2, outsequence)
Definition: exceptions.h:541
std::vector< std::atomic< bool > * > validity_pointers
Definition: subscriptor.h:233
const std::type_info * object_info
Definition: subscriptor.h:241
Subscriptor & operator=(const Subscriptor &)
Definition: subscriptor.h:283
void unsubscribe(std::atomic< bool > *const validity, const std::string &identifier="") const
Definition: subscriptor.cc:150
STL namespace.
void list_subscribers() const
Definition: subscriptor.cc:198
void subscribe(std::atomic< bool > *const validity, const std::string &identifier="") const
Definition: subscriptor.cc:130
void serialize(Archive &ar, const unsigned int version)
Definition: subscriptor.h:301
decltype(counter_map)::value_type map_value_type
Definition: subscriptor.h:222
std::map< std::string, unsigned int > counter_map
Definition: subscriptor.h:217
unsigned int n_subscriptions() const
Definition: subscriptor.h:292
decltype(counter_map)::iterator map_iterator
Definition: subscriptor.h:227
std::atomic< unsigned int > counter
Definition: subscriptor.h:211
static std::mutex mutex
Definition: subscriptor.h:263
virtual ~Subscriptor()
Definition: subscriptor.cc:44
#define DeclException3(Exception3, type1, type2, type3, outsequence)
Definition: exceptions.h:564
static ::ExceptionBase & ExcNoSubscriber(std::string arg1, std::string arg2)
void check_no_subscribers() const noexcept
Definition: subscriptor.cc:53