Reference documentation for deal.II version Git e159ac89de 2020-06-06 19:38:41 +0200
\(\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 
109  void
110  subscribe(std::atomic<bool> *const validity,
111  const std::string & identifier = "") const;
112 
119  void
120  unsubscribe(std::atomic<bool> *const validity,
121  const std::string & identifier = "") const;
122 
128  unsigned int
129  n_subscriptions() const;
130 
134  template <typename StreamType>
135  void
136  list_subscribers(StreamType &stream) const;
137 
141  void
142  list_subscribers() const;
143 
153  int,
154  std::string,
155  std::string,
156  << "Object of class " << arg2 << " is still used by " << arg1
157  << " other objects."
158  << "\n\n"
159  << "(Additional information: " << arg3 << ")\n\n"
160  << "See the entry in the Frequently Asked Questions of "
161  << "deal.II (linked to from http://www.dealii.org/) for "
162  << "a lot more information on what this error means and "
163  << "how to fix programs in which it happens.");
164 
170  std::string,
171  std::string,
172  << "No subscriber with identifier <" << arg2
173  << "> subscribes to this object of class " << arg1
174  << ". Consequently, it cannot be unsubscribed.");
176 
189  template <class Archive>
190  void
191  serialize(Archive &ar, const unsigned int version);
192 
193 private:
209  mutable std::atomic<unsigned int> counter;
210 
215  mutable std::map<std::string, unsigned int> counter_map;
216 
220  using map_value_type = decltype(counter_map)::value_type;
221 
225  using map_iterator = decltype(counter_map)::iterator;
226 
231  mutable std::vector<std::atomic<bool> *> validity_pointers;
232 
239  mutable const std::type_info *object_info;
240 
254  void
255  check_no_subscribers() const noexcept;
256 
261  static std::mutex mutex;
262 };
263 
264 //---------------------------------------------------------------------------
265 
267  : counter(0)
268  , object_info(nullptr)
269 {}
270 
271 
272 
274  : counter(0)
275  , object_info(nullptr)
276 {}
277 
278 
279 
280 inline Subscriptor &
282 {
283  object_info = s.object_info;
284  return *this;
285 }
286 
287 
288 
289 inline unsigned int
291 {
292  return counter;
293 }
294 
295 
296 
297 template <class Archive>
298 inline void
299 Subscriptor::serialize(Archive &, const unsigned int)
300 {
301  // do nothing, as explained in the
302  // documentation of this function
303 }
304 
305 template <typename StreamType>
306 inline void
307 Subscriptor::list_subscribers(StreamType &stream) const
308 {
309  std::lock_guard<std::mutex> lock(mutex);
310 
311  for (const auto &it : counter_map)
312  stream << it.second << '/' << counter << " subscriptions from \""
313  << it.first << '\"' << std::endl;
314 }
315 
317 
318 #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:231
const std::type_info * object_info
Definition: subscriptor.h:239
Subscriptor & operator=(const Subscriptor &)
Definition: subscriptor.h:281
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:299
decltype(counter_map)::value_type map_value_type
Definition: subscriptor.h:220
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:361
std::map< std::string, unsigned int > counter_map
Definition: subscriptor.h:215
unsigned int n_subscriptions() const
Definition: subscriptor.h:290
decltype(counter_map)::iterator map_iterator
Definition: subscriptor.h:225
std::atomic< unsigned int > counter
Definition: subscriptor.h:209
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:360
static std::mutex mutex
Definition: subscriptor.h:261
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