deal.II version GIT relicensing-2879-g253433159b 2025-03-21 00: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
32
33// Forward declaration
34template <typename T, typename P>
35class ObserverPointer;
36
37
73{
74public:
79
87
95
99 virtual ~EnableObserverPointer();
100
109
114 operator=(EnableObserverPointer &&) noexcept;
115
127 unsigned int
128 n_subscriptions() const;
129
133 template <typename StreamType>
134 void
135 list_subscribers(StreamType &stream) const;
136
140 void
141 list_subscribers() const;
142
154 int,
155 std::string,
156 std::string,
157 << "Object of class " << arg2 << " is still used by " << arg1
158 << " other objects."
159 << "\n\n"
160 << "(Additional information: " << arg3 << ")\n\n"
161 << "See the entry in the Frequently Asked Questions of "
162 << "deal.II (linked to from http://www.dealii.org/) for "
163 << "a lot more information on what this error means and "
164 << "how to fix programs in which it happens.");
165
172 std::string,
173 std::string,
174 << "No subscriber with identifier <" << arg2
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
223 using map_value_type = decltype(counter_map)::value_type;
224
228 using map_iterator = decltype(counter_map)::iterator;
229
234 mutable std::vector<std::atomic<bool> *> validity_pointers;
235
242 mutable const std::type_info *object_info;
243
249 static std::mutex mutex;
250
264 void
265 subscribe(std::atomic<bool> *const validity,
266 const std::string &identifier = "") const;
267
274 void
275 unsubscribe(std::atomic<bool> *const validity,
276 const std::string &identifier = "") const;
277
291 void
293
296
298};
299
300
310
311
312//---------------------------------------------------------------------------
313
318
319
320
326
327
328
335
336
337
338inline unsigned int
343
344
345
346template <class Archive>
347inline void
349{
350 // do nothing, as explained in the
351 // documentation of this function
352}
353
354template <typename StreamType>
355inline void
357{
358 std::lock_guard<std::mutex> lock(mutex);
359
360 for (const auto &it : counter_map)
361 stream << it.second << '/' << counter << " subscriptions from \""
362 << it.first << '\"' << std::endl;
363}
364
366
367#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:40
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:41
#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)