15#ifndef dealii_observer_pointer_h
16#define dealii_observer_pointer_h
105template <
typename T,
typename P =
void>
204 operator T *()
const;
268 const std::string
id;
284template <
typename T,
typename P =
void>
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'.");
305template <
typename T,
typename P>
308 , id(typeid(P).name())
309 , pointed_to_object_is_alive(false)
311 static_assert(std::is_base_of_v<EnableObserverPointer, T>,
312 "This class can only be used if the first template argument "
313 "is a class derived from 'EnableObserverPointer'.");
321template <
typename T,
typename P>
325 , pointed_to_object_is_alive(false)
327 static_assert(std::is_base_of_v<EnableObserverPointer, T>,
328 "This class can only be used if the first template argument "
329 "is a class derived from 'EnableObserverPointer'.");
337template <
typename T,
typename P>
341 : pointer(other.pointer)
343 , pointed_to_object_is_alive(false)
345 static_assert(std::is_base_of_v<EnableObserverPointer, T>,
346 "This class can only be used if the first template argument "
347 "is a class derived from 'EnableObserverPointer'.");
349 if (other !=
nullptr)
352 ExcMessage(
"You can't copy a smart pointer object that "
353 "is pointing to an object that is no longer alive."));
360template <
typename T,
typename P>
363 : pointer(other.pointer)
365 , pointed_to_object_is_alive(false)
367 static_assert(std::is_base_of_v<EnableObserverPointer, T>,
368 "This class can only be used if the first template argument "
369 "is a class derived from 'EnableObserverPointer'.");
371 if (other !=
nullptr)
374 ExcMessage(
"You can't copy a smart pointer object that "
375 "is pointing to an object that is no longer alive."));
382template <
typename T,
typename P>
385 : pointer(other.pointer)
387 , pointed_to_object_is_alive(
false)
389 static_assert(std::is_base_of_v<EnableObserverPointer, T>,
390 "This class can only be used if the first template argument "
391 "is a class derived from 'EnableObserverPointer'.");
393 if (other !=
nullptr)
395 Assert(other.pointed_to_object_is_alive,
396 ExcMessage(
"You can't move a smart pointer object that "
397 "is pointing to an object that is no longer alive."));
401 pointer->subscribe(&pointed_to_object_is_alive,
id);
407 "Calling subscribe() failed with an exception, but we "
408 "are in a function that cannot throw exceptions. "
409 "Aborting the program here."));
420template <
typename T,
typename P>
423 static_assert(std::is_base_of_v<EnableObserverPointer, T>,
424 "This class can only be used if the first template argument "
425 "is a class derived from 'EnableObserverPointer'.");
427 if (pointed_to_object_is_alive && pointer !=
nullptr)
428 pointer->unsubscribe(&pointed_to_object_is_alive,
id);
433template <
typename T,
typename P>
437 if (pointed_to_object_is_alive && pointer !=
nullptr)
439 pointer->unsubscribe(&pointed_to_object_is_alive,
id);
448template <
typename T,
typename P>
457 if (pointed_to_object_is_alive && pointer !=
nullptr)
458 pointer->unsubscribe(&pointed_to_object_is_alive,
id);
463 pointer->subscribe(&pointed_to_object_is_alive,
id);
470template <
typename T,
typename P>
482 if (pointed_to_object_is_alive && pointer !=
nullptr)
483 pointer->unsubscribe(&pointed_to_object_is_alive,
id);
486 pointer = (other !=
nullptr ? other.
get() :
nullptr);
487 if (other !=
nullptr)
490 ExcMessage(
"You can't copy a smart pointer object that "
491 "is pointing to an object that is no longer alive."));
492 pointer->subscribe(&pointed_to_object_is_alive,
id);
499template <
typename T,
typename P>
510 if (pointed_to_object_is_alive && pointer !=
nullptr)
511 pointer->unsubscribe(&pointed_to_object_is_alive,
id);
514 pointer = (other !=
nullptr ? other.
get() :
nullptr);
515 if (other !=
nullptr)
518 ExcMessage(
"You can't copy a smart pointer object that "
519 "is pointing to an object that is no longer alive."));
520 pointer->subscribe(&pointed_to_object_is_alive,
id);
527template <
typename T,
typename P>
531 if (other ==
nullptr)
537 else if (&other !=
this)
540 if (pointer !=
nullptr && pointed_to_object_is_alive)
541 pointer->unsubscribe(&pointed_to_object_is_alive,
id);
544 Assert(other.pointed_to_object_is_alive,
545 ExcMessage(
"You can't move a smart pointer object that "
546 "is pointing to an object that is no longer alive."));
547 pointer = other.get();
550 pointer->subscribe(&pointed_to_object_is_alive,
id);
556 "Calling subscribe() failed with an exception, but we "
557 "are in a function that cannot throw exceptions. "
558 "Aborting the program here."));
569template <
typename T,
typename P>
577template <
typename T,
typename P>
582 Assert(pointed_to_object_is_alive,
583 ExcMessage(
"The object pointed to is not valid anymore."));
589template <
typename T,
typename P>
594 Assert(pointed_to_object_is_alive,
595 ExcMessage(
"The object pointed to is not valid anymore."));
601template <
typename T,
typename P>
610template <
typename T,
typename P>
623 std::swap(pointer, other.
pointer);
629template <
typename T,
typename P>
633 if (pointed_to_object_is_alive && pointer !=
nullptr)
634 pointer->unsubscribe(pointed_to_object_is_alive,
id);
636 std::swap(pointer, ptr);
638 if (pointer !=
nullptr)
639 pointer->subscribe(pointed_to_object_is_alive,
id);
644template <
typename T,
typename P>
667template <
typename T,
typename P,
class Q>
683template <
typename T,
typename P>
699template <
typename T,
typename P>
204 operator T *()
const; {
…}
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)