deal.II version 9.8.0
\(\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: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 1998 - 2025 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13#ifndef dealii_enable_observer_pointer_h
14#define dealii_enable_observer_pointer_h
15
16
17#include <deal.II/base/config.h>
18
20
21#include <atomic>
22#include <map>
23#include <mutex>
24#include <ostream>
25#include <string>
26#include <typeinfo>
27#include <vector>
28
30
31
32// Forward declaration
33template <typename T, typename P>
34class ObserverPointer;
35
36
72{
73public:
78
86
94
98 virtual ~EnableObserverPointer();
99
108
113 operator=(EnableObserverPointer &&) noexcept;
114
120
126 unsigned int
128
132 template <typename StreamType>
133 void
134 list_subscribers(StreamType &stream) const;
135
139 void
140 list_subscribers() const;
141
143
148
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://dealii.org/) for "
162 << "a lot more information on what this error means and "
163 << "how to fix programs in which it happens.");
164
171 std::string,
172 std::string,
173 << "No subscriber with identifier <"
174 << (arg2.empty() ? "unknown subscriber" : arg2.c_str())
175 << "> subscribes to this object of class " << arg1
176 << ". Consequently, it cannot be unsubscribed.");
178
192 template <class Archive>
193 void
194 serialize(Archive &ar, const unsigned int version);
195
196private:
212 mutable std::atomic<unsigned int> counter;
213
218 mutable std::map<std::string, unsigned int> counter_map;
219
224 mutable std::vector<std::atomic<bool> *> validity_pointers;
225
232 mutable const std::type_info *object_info;
233
239 static std::mutex mutex;
240
249
254 void
255 subscribe(std::atomic<bool> *const validity,
256 const std::string &identifier = "") const;
257
264 void
265 unsubscribe(std::atomic<bool> *const validity,
266 const std::string &identifier = "") const;
267
281 void
282 check_no_subscribers() const noexcept;
283
284 template <typename, typename>
285 friend class ObserverPointer;
286
288};
289
290
298 "Use the new name of the class, EnableObserverPointer.") =
300
301
302//---------------------------------------------------------------------------
307{}
308
309
315{}
316
317
318
324}
325
326
327
328inline unsigned int
330{
331 return counter;
332}
333
334
335
336template <class Archive>
337inline void
338EnableObserverPointer::serialize(Archive &, const unsigned int)
339{
340 // do nothing, as explained in the
341 // documentation of this function
342}
343
344template <typename StreamType>
345inline void
346EnableObserverPointer::list_subscribers(StreamType &stream) const
347{
348 std::scoped_lock lock(mutex);
349
350 for (const auto &it : counter_map)
351 stream << it.second << '/' << counter << " subscriptions from \""
352 << it.first << '\"' << std::endl;
353}
354
356
357#endif
void check_no_subscribers() const noexcept
void subscribe(std::atomic< bool > *const validity, const std::string &identifier="") const
void list_subscribers(StreamType &stream) const
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
const std::type_info * object_info
unsigned int n_subscriptions() const
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_DEPRECATED_WITH_COMMENT(comment)
Definition config.h:287
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
EnableObserverPointer Subscriptor
#define DeclException2(Exception2, type1, type2, outsequence)
static ::ExceptionBase & ExcNoSubscriber(std::string arg1, std::string arg2)
#define DeclException3(Exception3, type1, type2, type3, outsequence)
static ::ExceptionBase & ExcInUse(int arg1, std::string arg2, std::string arg3)