1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
// ------------------------------------------------------------------------
//
// SPDX-License-Identifier: LGPL-2.1-or-later
// Copyright (C) 1998 - 2025 by the deal.II authors
//
// This file is part of the deal.II library.
//
// Part of the source code is dual licensed under Apache-2.0 WITH
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
// governing the source code and code contributions can be found in
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
//
// ------------------------------------------------------------------------
#include <deal.II/base/enable_observer_pointer.h>
#include <deal.II/base/exceptions.h>
#include <deal.II/base/logstream.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <typeinfo>
DEAL_II_NAMESPACE_OPEN
static const char *unknown_subscriber = "unknown subscriber";
std::mutex EnableObserverPointer::mutex;
EnableObserverPointer::EnableObserverPointer(
EnableObserverPointer &&subscriptor) noexcept
: counter(0)
, object_info(subscriptor.object_info)
{
for (auto *const validity_ptr : subscriptor.validity_pointers)
*validity_ptr = false;
subscriptor.validity_pointers.clear();
}
EnableObserverPointer::~EnableObserverPointer()
{
for (auto *const validity_ptr : validity_pointers)
*validity_ptr = false;
object_info = nullptr;
}
void
EnableObserverPointer::check_no_subscribers() const noexcept
{
// Check whether there are still subscriptions to this object. If so, output
// the actual name of the class to which this object belongs, i.e. the most
// derived class. Note that the name may be mangled, so it need not be the
// clear-text class name. However, you can obtain the latter by running the
// c++filt program over the output.
if constexpr (running_in_debug_mode())
{
// If there are still active pointers, show a message and kill the
// program. However, under some circumstances, this is not so desirable.
// For example, in code like this:
//
// Triangulation tria;
// DoFHandler *dh = new DoFHandler(tria);
// ...some function that throws an exception
//
// the exception will lead to the destruction of the triangulation, but
// since the dof_handler is on the heap it will not be destroyed. This
// will trigger an assertion in the triangulation. If we kill the program
// at this point, we will never be able to learn what caused the problem.
// In this situation, just display a message and continue the program.
if (counter != 0)
{
if (std::uncaught_exceptions() == 0)
{
std::string infostring;
for (const auto &map_entry : counter_map)
{
if (map_entry.second > 0)
infostring +=
"\n from Subscriber " + std::string(map_entry.first);
}
if (infostring.empty())
infostring = "<none>";
AssertNothrow(counter == 0,
ExcInUse(counter.load(),
object_info->name(),
infostring));
}
else
{
std::cerr
<< "---------------------------------------------------------"
<< std::endl
<< "An object pointed to by a ObserverPointer is being destroyed."
<< std::endl
<< "Under normal circumstances, this would abort the program."
<< std::endl
<< "However, another exception is being processed at the"
<< std::endl
<< "moment, so the program will continue to run to allow"
<< std::endl
<< "this exception to be processed." << std::endl
<< "---------------------------------------------------------"
<< std::endl;
}
}
}
}
EnableObserverPointer &
EnableObserverPointer::operator=(EnableObserverPointer &&s) noexcept
{
for (auto *const validity_ptr : s.validity_pointers)
*validity_ptr = false;
s.validity_pointers.clear();
object_info = s.object_info;
return *this;
}
void
EnableObserverPointer::subscribe(std::atomic<bool> *const validity,
const std::string &id) const
{
std::lock_guard<std::mutex> lock(mutex);
if (object_info == nullptr)
object_info = &typeid(*this);
++counter;
const std::string &name = id.empty() ? unknown_subscriber : id;
++counter_map[name];
*validity = true;
validity_pointers.push_back(validity);
}
void
EnableObserverPointer::unsubscribe(std::atomic<bool> *const validity,
const std::string &id) const
{
const std::string &name = id.empty() ? unknown_subscriber : id;
if (counter == 0)
{
AssertNothrow(counter > 0, ExcNoSubscriber(object_info->name(), name));
// This is for the case that we do not abort after the exception
return;
}
std::lock_guard<std::mutex> lock(mutex);
map_iterator it = counter_map.find(name);
if (it == counter_map.end())
{
AssertNothrow(it != counter_map.end(),
ExcNoSubscriber(object_info->name(), name));
// This is for the case that we do not abort after the exception
return;
}
if (it->second == 0)
{
AssertNothrow(it->second > 0, ExcNoSubscriber(object_info->name(), name));
// This is for the case that we do not abort after the exception
return;
}
auto validity_ptr_it =
std::find(validity_pointers.begin(), validity_pointers.end(), validity);
if (validity_ptr_it == validity_pointers.end())
{
AssertNothrow(
validity_ptr_it != validity_pointers.end(),
ExcMessage(
"This EnableObserverPointer object does not know anything about the supplied pointer!"));
return;
}
validity_pointers.erase(validity_ptr_it);
--counter;
--it->second;
}
void
EnableObserverPointer::list_subscribers() const
{
list_subscribers(deallog);
}
DEAL_II_NAMESPACE_CLOSE
|