Reference documentation for deal.II version GIT relicensing-478-g3275795167 2024-04-24 07:10:02+00:00
\(\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\}}\)
Loading...
Searching...
No Matches
subscriptor.h
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 1998 - 2023 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Part of the source code is dual licensed under Apache-2.0 WITH
9// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10// governing the source code and code contributions can be found in
11// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12//
13// ------------------------------------------------------------------------
14
15#ifndef dealii_subscriptor_h
16#define dealii_subscriptor_h
17
18
19#include <deal.II/base/config.h>
20
22
23#include <atomic>
24#include <map>
25#include <mutex>
26#include <string>
27#include <typeinfo>
28#include <vector>
29
31
60{
61public:
66
73 Subscriptor(const Subscriptor &);
74
81 Subscriptor(Subscriptor &&) noexcept;
82
86 virtual ~Subscriptor();
87
95 operator=(const Subscriptor &);
96
101 operator=(Subscriptor &&) noexcept;
102
115 void
116 subscribe(std::atomic<bool> *const validity,
117 const std::string &identifier = "") const;
118
125 void
126 unsubscribe(std::atomic<bool> *const validity,
127 const std::string &identifier = "") const;
128
134 unsigned int
135 n_subscriptions() const;
136
140 template <typename StreamType>
141 void
142 list_subscribers(StreamType &stream) const;
143
147 void
148 list_subscribers() const;
149
161 int,
162 std::string,
163 std::string,
164 << "Object of class " << arg2 << " is still used by " << arg1
165 << " other objects."
166 << "\n\n"
167 << "(Additional information: " << arg3 << ")\n\n"
168 << "See the entry in the Frequently Asked Questions of "
169 << "deal.II (linked to from http://www.dealii.org/) for "
170 << "a lot more information on what this error means and "
171 << "how to fix programs in which it happens.");
172
178 std::string,
179 std::string,
180 << "No subscriber with identifier <" << arg2
181 << "> subscribes to this object of class " << arg1
182 << ". Consequently, it cannot be unsubscribed.");
198 template <class Archive>
199 void
200 serialize(Archive &ar, const unsigned int version);
201
202private:
218 mutable std::atomic<unsigned int> counter;
219
224 mutable std::map<std::string, unsigned int> counter_map;
225
229 using map_value_type = decltype(counter_map)::value_type;
230
234 using map_iterator = decltype(counter_map)::iterator;
235
240 mutable std::vector<std::atomic<bool> *> validity_pointers;
241
248 mutable const std::type_info *object_info;
249
263 void
264 check_no_subscribers() const noexcept;
265
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
290inline Subscriptor &
292{
294 return *this;
295}
296
297
298
299inline unsigned int
301{
302 return counter;
303}
304
305
306
307template <class Archive>
308inline void
309Subscriptor::serialize(Archive &, const unsigned int)
310{
311 // do nothing, as explained in the
312 // documentation of this function
313}
314
315template <typename StreamType>
316inline void
317Subscriptor::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
std::vector< std::atomic< bool > * > validity_pointers
void unsubscribe(std::atomic< bool > *const validity, const std::string &identifier="") const
std::atomic< unsigned int > counter
void check_no_subscribers() const noexcept
void list_subscribers() const
const std::type_info * object_info
Subscriptor & operator=(const Subscriptor &)
void subscribe(std::atomic< bool > *const validity, const std::string &identifier="") const
void serialize(Archive &ar, const unsigned int version)
std::map< std::string, unsigned int > counter_map
static std::mutex mutex
decltype(counter_map)::iterator map_iterator
decltype(counter_map)::value_type map_value_type
virtual ~Subscriptor()
unsigned int n_subscriptions() const
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:502
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:503
static ::ExceptionBase & ExcInUse(int arg1, std::string arg2, std::string arg3)
#define DeclException2(Exception2, type1, type2, outsequence)
Definition exceptions.h:539
static ::ExceptionBase & ExcNoSubscriber(std::string arg1, std::string arg2)
#define DeclException3(Exception3, type1, type2, type3, outsequence)
Definition exceptions.h:562
STL namespace.