13#ifndef dealii_observer_pointer_h
14#define dealii_observer_pointer_h
103template <
typename T,
typename P =
void>
202 operator T *()
const;
266 const std::string
id;
279template <
typename T,
typename P>
282 , id(typeid(P).name())
283 , pointed_to_object_is_alive(false)
285 static_assert(std::is_base_of_v<EnableObserverPointer, T>,
286 "This class can only be used if the first template argument "
287 "is a class derived from 'EnableObserverPointer'.");
292template <
typename T,
typename P>
295 , id(typeid(P).name())
296 , pointed_to_object_is_alive(false)
298 static_assert(std::is_base_of_v<EnableObserverPointer, T>,
299 "This class can only be used if the first template argument "
300 "is a class derived from 'EnableObserverPointer'.");
308template <
typename T,
typename P>
312 , pointed_to_object_is_alive(false)
314 static_assert(std::is_base_of_v<EnableObserverPointer, T>,
315 "This class can only be used if the first template argument "
316 "is a class derived from 'EnableObserverPointer'.");
324template <
typename T,
typename P>
330 , pointed_to_object_is_alive(false)
332 static_assert(std::is_base_of_v<EnableObserverPointer, T>,
333 "This class can only be used if the first template argument "
334 "is a class derived from 'EnableObserverPointer'.");
336 if (other !=
nullptr)
339 ExcMessage(
"You can't copy a smart pointer object that "
340 "is pointing to an object that is no longer alive."));
347template <
typename T,
typename P>
352 , pointed_to_object_is_alive(false)
354 static_assert(std::is_base_of_v<EnableObserverPointer, T>,
355 "This class can only be used if the first template argument "
356 "is a class derived from 'EnableObserverPointer'.");
358 if (other !=
nullptr)
361 ExcMessage(
"You can't copy a smart pointer object that "
362 "is pointing to an object that is no longer alive."));
369template <
typename T,
typename P>
374 , pointed_to_object_is_alive(
false)
376 static_assert(std::is_base_of_v<EnableObserverPointer, T>,
377 "This class can only be used if the first template argument "
378 "is a class derived from 'EnableObserverPointer'.");
380 if (other !=
nullptr)
382 Assert(other.pointed_to_object_is_alive,
383 ExcMessage(
"You can't move a smart pointer object that "
384 "is pointing to an object that is no longer alive."));
388 pointer->subscribe(&pointed_to_object_is_alive,
id);
394 "Calling subscribe() failed with an exception, but we "
395 "are in a function that cannot throw exceptions. "
396 "Aborting the program here."));
407template <
typename T,
typename P>
410 static_assert(std::is_base_of_v<EnableObserverPointer, T>,
411 "This class can only be used if the first template argument "
412 "is a class derived from 'EnableObserverPointer'.");
414 if (pointed_to_object_is_alive &&
pointer !=
nullptr)
415 pointer->unsubscribe(&pointed_to_object_is_alive,
id);
420template <
typename T,
typename P>
424 if (pointed_to_object_is_alive &&
pointer !=
nullptr)
426 pointer->unsubscribe(&pointed_to_object_is_alive,
id);
435template <
typename T,
typename P>
444 if (pointed_to_object_is_alive &&
pointer !=
nullptr)
445 pointer->unsubscribe(&pointed_to_object_is_alive,
id);
450 pointer->subscribe(&pointed_to_object_is_alive,
id);
457template <
typename T,
typename P>
469 if (pointed_to_object_is_alive &&
pointer !=
nullptr)
470 pointer->unsubscribe(&pointed_to_object_is_alive,
id);
473 pointer = (other !=
nullptr ? other.
get() :
nullptr);
474 if (other !=
nullptr)
477 ExcMessage(
"You can't copy a smart pointer object that "
478 "is pointing to an object that is no longer alive."));
479 pointer->subscribe(&pointed_to_object_is_alive,
id);
486template <
typename T,
typename P>
497 if (pointed_to_object_is_alive &&
pointer !=
nullptr)
498 pointer->unsubscribe(&pointed_to_object_is_alive,
id);
501 pointer = (other !=
nullptr ? other.
get() :
nullptr);
502 if (other !=
nullptr)
505 ExcMessage(
"You can't copy a smart pointer object that "
506 "is pointing to an object that is no longer alive."));
507 pointer->subscribe(&pointed_to_object_is_alive,
id);
514template <
typename T,
typename P>
518 if (other ==
nullptr)
524 else if (&other !=
this)
527 if (
pointer !=
nullptr && pointed_to_object_is_alive)
528 pointer->unsubscribe(&pointed_to_object_is_alive,
id);
531 Assert(other.pointed_to_object_is_alive,
532 ExcMessage(
"You can't move a smart pointer object that "
533 "is pointing to an object that is no longer alive."));
537 pointer->subscribe(&pointed_to_object_is_alive,
id);
543 "Calling subscribe() failed with an exception, but we "
544 "are in a function that cannot throw exceptions. "
545 "Aborting the program here."));
556template <
typename T,
typename P>
564template <
typename T,
typename P>
569 Assert(pointed_to_object_is_alive,
570 ExcMessage(
"The object pointed to is not valid anymore."));
576template <
typename T,
typename P>
581 Assert(pointed_to_object_is_alive,
582 ExcMessage(
"The object pointed to is not valid anymore."));
588template <
typename T,
typename P>
597template <
typename T,
typename P>
616template <
typename T,
typename P>
620 if (pointed_to_object_is_alive &&
pointer !=
nullptr)
621 pointer->unsubscribe(pointed_to_object_is_alive,
id);
626 pointer->subscribe(pointed_to_object_is_alive,
id);
631template <
typename T,
typename P>
654template <
typename T,
typename P,
class Q>
670template <
typename T,
typename P>
686template <
typename T,
typename P>
std::size_t memory_consumption() const
void swap(ObserverPointer< T, Q > &tt)
std::atomic< bool > pointed_to_object_is_alive
ObserverPointer< T, P > & operator=(const ObserverPointer< T, P > &other)
ObserverPointer< T, P > & operator=(const ObserverPointer< T, Q > &other)
ObserverPointer(const ObserverPointer< T, P > &other)
ObserverPointer< T, P > & operator=(T *tt)
ObserverPointer< T, P > & operator=(ObserverPointer< T, P > &&other) noexcept
ObserverPointer(const ObserverPointer< T, Q > &other)
ObserverPointer(ObserverPointer< T, P > &&other) noexcept
ObserverPointer(T *t, const std::string &id)
#define DEAL_II_DEPRECATED
#define DEAL_II_NAMESPACE_OPEN
constexpr bool running_in_debug_mode()
#define DEAL_II_NAMESPACE_CLOSE
#define Assert(cond, exc)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcNotInitialized()
static ::ExceptionBase & ExcMessage(std::string arg1)
void swap(ObserverPointer< T, P > &t1, ObserverPointer< T, Q > &t2)