deal.II version GIT relicensing-2009-g9627ae39f5 2024-10-19 11:10:00+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
enable_observer_pointer.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_enable_observer_pointer_h
16#define dealii_enable_observer_pointer_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
74
82
86 virtual ~EnableObserverPointer();
87
96
101 operator=(EnableObserverPointer &&) noexcept;
102
116 void
117 subscribe(std::atomic<bool> *const validity,
118 const std::string &identifier = "") const;
119
126 void
127 unsubscribe(std::atomic<bool> *const validity,
128 const std::string &identifier = "") const;
129
135 unsigned int
136 n_subscriptions() const;
137
141 template <typename StreamType>
142 void
143 list_subscribers(StreamType &stream) const;
144
148 void
149 list_subscribers() const;
150
162 int,
163 std::string,
164 std::string,
165 << "Object of class " << arg2 << " is still used by " << arg1
166 << " other objects."
167 << "\n\n"
168 << "(Additional information: " << arg3 << ")\n\n"
169 << "See the entry in the Frequently Asked Questions of "
170 << "deal.II (linked to from http://www.dealii.org/) for "
171 << "a lot more information on what this error means and "
172 << "how to fix programs in which it happens.");
173
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.");
200 template <class Archive>
201 void
202 serialize(Archive &ar, const unsigned int version);
203
204private:
220 mutable std::atomic<unsigned int> counter;
221
226 mutable std::map<std::string, unsigned int> counter_map;
227
231 using map_value_type = decltype(counter_map)::value_type;
232
236 using map_iterator = decltype(counter_map)::iterator;
237
242 mutable std::vector<std::atomic<bool> *> validity_pointers;
243
250 mutable const std::type_info *object_info;
251
265 void
266 check_no_subscribers() const noexcept;
267
273 static std::mutex mutex;
274};
275
276
283using Subscriptor DEAL_II_DEPRECATED_EARLY_WITH_COMMENT(
284 "Use the new name of the class, EnableObserverPointer.") =
286
287
288//---------------------------------------------------------------------------
289
291 : counter(0)
292 , object_info(nullptr)
293{}
294
295
296
302
303
304
311
312
313
314inline unsigned int
319
320
321
322template <class Archive>
323inline void
324EnableObserverPointer::serialize(Archive &, const unsigned int)
325{
326 // do nothing, as explained in the
327 // documentation of this function
328}
329
330template <typename StreamType>
331inline void
333{
334 std::lock_guard<std::mutex> lock(mutex);
335
336 for (const auto &it : counter_map)
337 stream << it.second << '/' << counter << " subscriptions from \""
338 << it.first << '\"' << std::endl;
339}
340
342
343#endif
void check_no_subscribers() const noexcept
void subscribe(std::atomic< bool > *const validity, const std::string &identifier="") const
decltype(counter_map)::iterator map_iterator
void serialize(Archive &ar, const unsigned int version)
EnableObserverPointer & operator=(const EnableObserverPointer &)
std::atomic< unsigned int > counter
void unsubscribe(std::atomic< bool > *const validity, const std::string &identifier="") const
std::map< std::string, unsigned int > counter_map
std::vector< std::atomic< bool > * > validity_pointers
decltype(counter_map)::value_type map_value_type
const std::type_info * object_info
unsigned int n_subscriptions() const
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:498
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:499
#define DeclException2(Exception2, type1, type2, outsequence)
Definition exceptions.h:534
static ::ExceptionBase & ExcNoSubscriber(std::string arg1, std::string arg2)
#define DeclException3(Exception3, type1, type2, type3, outsequence)
Definition exceptions.h:557
static ::ExceptionBase & ExcInUse(int arg1, std::string arg2, std::string arg3)
STL namespace.