Reference documentation for deal.II version 9.1.0-pre
subscriptor.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 2018 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 <map>
26 #include <string>
27 #include <typeinfo>
28 
29 #ifdef DEAL_II_WITH_THREADS
30 # include <mutex>
31 #endif
32 
33 DEAL_II_NAMESPACE_OPEN
34 
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 
105  Subscriptor &
106  operator=(Subscriptor &&) noexcept;
107 
112  void
113  subscribe(const char *identifier = nullptr) const;
114 
121  void
122  unsubscribe(const char *identifier = nullptr) 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:
198  using map_value_type = std::map<const char *, unsigned int>::value_type;
199 
203  using map_iterator = std::map<const char *, unsigned int>::iterator;
204 
221  mutable std::atomic<unsigned int> counter;
222 
227  mutable std::map<const char *, unsigned int> counter_map;
228 
235  mutable const std::type_info *object_info;
236 
250  void
251  check_no_subscribers() const noexcept;
252 
253 #ifdef DEAL_II_WITH_THREADS
254 
259  static std::mutex mutex;
260 
261 #endif
262 };
263 
264 //---------------------------------------------------------------------------
265 
266 template <class Archive>
267 inline void
268 Subscriptor::serialize(Archive &, const unsigned int)
269 {
270  // do nothing, as explained in the
271  // documentation of this function
272 }
273 
274 template <typename StreamType>
275 inline void
276 Subscriptor::list_subscribers(StreamType &stream) const
277 {
278 #ifdef DEAL_II_WITH_THREADS
279  std::lock_guard<std::mutex> lock(mutex);
280 #endif
281 
282  for (map_iterator it = counter_map.begin(); it != counter_map.end(); ++it)
283  stream << it->second << '/' << counter << " subscriptions from \""
284  << it->first << '\"' << std::endl;
285 }
286 
287 DEAL_II_NAMESPACE_CLOSE
288 
289 #endif
static::ExceptionBase & ExcInUse(int arg1, std::string arg2, std::string arg3)
#define DeclException2(Exception2, type1, type2, outsequence)
Definition: exceptions.h:420
void subscribe(const char *identifier=nullptr) const
Definition: subscriptor.cc:157
const std::type_info * object_info
Definition: subscriptor.h:235
Subscriptor & operator=(const Subscriptor &)
Definition: subscriptor.cc:136
std::map< const char *, unsigned int >::value_type map_value_type
Definition: subscriptor.h:198
void unsubscribe(const char *identifier=nullptr) const
Definition: subscriptor.cc:179
std::map< const char *, unsigned int >::iterator map_iterator
Definition: subscriptor.h:203
unsigned int n_subscriptions() const
Definition: subscriptor.cc:205
void serialize(Archive &ar, const unsigned int version)
Definition: subscriptor.h:268
std::atomic< unsigned int > counter
Definition: subscriptor.h:221
std::map< const char *, unsigned int > counter_map
Definition: subscriptor.h:227
static std::mutex mutex
Definition: subscriptor.h:259
virtual ~Subscriptor()
Definition: subscriptor.cc:61
#define DeclException3(Exception3, type1, type2, type3, outsequence)
Definition: exceptions.h:432
static::ExceptionBase & ExcNoSubscriber(std::string arg1, std::string arg2)
void list_subscribers() const
Definition: subscriptor.cc:213
void check_no_subscribers() const noexcept
Definition: subscriptor.cc:69