deal.II version GIT relicensing-6842-g793a97d2aa 2026-10-02 14:00:01+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: 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
126 unsigned int
127 n_subscriptions() const;
128
132 template <typename StreamType>
133 void
134 list_subscribers(StreamType &stream) const;
135
139 void
140 list_subscribers() const;
141
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.");
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
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
291//---------------------------------------------------------------------------
292
294 : counter(0)
295 , object_info(nullptr)
296{}
297
298
299
305
306
307
314
315
316
317inline unsigned int
322
323
324
325template <class Archive>
326inline void
327EnableObserverPointer::serialize(Archive &, const unsigned int)
328{
329 // do nothing, as explained in the
330 // documentation of this function
331}
332
333template <typename StreamType>
334inline void
336{
337 std::scoped_lock lock(mutex);
338
339 for (const auto &it : counter_map)
340 stream << it.second << '/' << counter << " subscriptions from \""
341 << it.first << '\"' << std::endl;
342}
343
345
346#endif
void check_no_subscribers() const noexcept
void subscribe(std::atomic< bool > *const validity, const std::string &identifier="") 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_NAMESPACE_CLOSE
Definition config.h:39
#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)