deal.II version GIT relicensing-2165-gc91f007519 2024-11-20 01:40: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
67{
68public:
73
81
89
93 virtual ~EnableObserverPointer();
94
103
108 operator=(EnableObserverPointer &&) noexcept;
109
123 void
124 subscribe(std::atomic<bool> *const validity,
125 const std::string &identifier = "") const;
126
133 void
134 unsubscribe(std::atomic<bool> *const validity,
135 const std::string &identifier = "") const;
136
142 unsigned int
143 n_subscriptions() const;
144
148 template <typename StreamType>
149 void
150 list_subscribers(StreamType &stream) const;
151
155 void
156 list_subscribers() const;
157
169 int,
170 std::string,
171 std::string,
172 << "Object of class " << arg2 << " is still used by " << arg1
173 << " other objects."
174 << "\n\n"
175 << "(Additional information: " << arg3 << ")\n\n"
176 << "See the entry in the Frequently Asked Questions of "
177 << "deal.II (linked to from http://www.dealii.org/) for "
178 << "a lot more information on what this error means and "
179 << "how to fix programs in which it happens.");
180
187 std::string,
188 std::string,
189 << "No subscriber with identifier <" << arg2
190 << "> subscribes to this object of class " << arg1
191 << ". Consequently, it cannot be unsubscribed.");
207 template <class Archive>
208 void
209 serialize(Archive &ar, const unsigned int version);
210
211private:
227 mutable std::atomic<unsigned int> counter;
228
233 mutable std::map<std::string, unsigned int> counter_map;
234
238 using map_value_type = decltype(counter_map)::value_type;
239
243 using map_iterator = decltype(counter_map)::iterator;
244
249 mutable std::vector<std::atomic<bool> *> validity_pointers;
250
257 mutable const std::type_info *object_info;
258
272 void
273 check_no_subscribers() const noexcept;
274
280 static std::mutex mutex;
281};
282
283
290using Subscriptor DEAL_II_DEPRECATED_EARLY_WITH_COMMENT(
291 "Use the new name of the class, EnableObserverPointer.") =
293
294
295//---------------------------------------------------------------------------
296
298 : counter(0)
299 , object_info(nullptr)
300{}
301
302
303
309
310
311
318
319
320
321inline unsigned int
326
327
328
329template <class Archive>
330inline void
331EnableObserverPointer::serialize(Archive &, const unsigned int)
332{
333 // do nothing, as explained in the
334 // documentation of this function
335}
336
337template <typename StreamType>
338inline void
340{
341 std::lock_guard<std::mutex> lock(mutex);
342
343 for (const auto &it : counter_map)
344 stream << it.second << '/' << counter << " subscriptions from \""
345 << it.first << '\"' << std::endl;
346}
347
349
350#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.