Advanced Multi-Physics (AMP)
On-Line Documentation
enable_shared_from_this.h
Go to the documentation of this file.
1// This file wraps enable_shared_from_this so that it is more flexible
2#ifndef included_AMP_enable_shared_from_this
3#define included_AMP_enable_shared_from_this
4
5#include "AMP/utils/UtilityMacros.h"
6
7#include <memory>
8
9
10namespace AMP {
11
12
24template<class T>
25class enable_shared_from_this : public std::enable_shared_from_this<T>
26{
27public:
28 std::shared_ptr<T> shared_from_this()
29 {
30 std::shared_ptr<T> ptr;
31 if ( weak_ptr_.use_count() == 0 ) {
32 T *tmp = dynamic_cast<T *>( this );
33 AMP_ASSERT( tmp != NULL );
34 try {
35 std::enable_shared_from_this<T> *tmp2 = this;
36 ptr = tmp2->shared_from_this();
37 } catch ( ... ) {
38 ptr = std::shared_ptr<T>( tmp, []( void * ) {} );
39 }
40 weak_ptr_ = ptr;
41 } else {
42 ptr = std::shared_ptr<T>( weak_ptr_ );
43 }
44 return ptr;
45 }
46 std::shared_ptr<const T> shared_from_this() const
47 {
48 std::shared_ptr<const T> ptr;
49 if ( weak_ptr_.use_count() == 0 ) {
50 const T *tmp = dynamic_cast<const T *>( this );
51 AMP_ASSERT( tmp != NULL );
52 try {
53 const std::enable_shared_from_this<T> *tmp2 = this;
54 ptr = tmp2->shared_from_this();
55 } catch ( ... ) {
56 // Note: Clang on MAC has issues with the const version of this line, hence the
57 // const_cast
58 ptr = std::shared_ptr<T>( const_cast<T *>( tmp ), []( void * ) {} );
59 }
60 weak_ptr_ = std::const_pointer_cast<T>( ptr );
61 } else {
62 ptr = std::shared_ptr<const T>( weak_ptr_ );
63 }
64 return ptr;
65 }
66 virtual ~enable_shared_from_this() = default;
67
68protected:
69 mutable std::weak_ptr<T> weak_ptr_;
70};
71
72
73} // namespace AMP
74
75#endif
Enhancement of std::enable_shared_from_this.
virtual ~enable_shared_from_this()=default
std::shared_ptr< T > shared_from_this()
std::shared_ptr< const T > shared_from_this() const
#define AMP_ASSERT(EXP)
Assert error.



Advanced Multi-Physics (AMP)
Oak Ridge National Laboratory
Idaho National Laboratory
Los Alamos National Laboratory
This page automatically produced from the
source code by doxygen
Last updated: Tue Mar 10 2026 13:06:41.
Comments on this page