TheAtlasEngine
 
Loading...
Searching...
No Matches
atlas::memory::enable_strong_from_this< T > Class Template Reference

CRTP mixin to enable objects to create strong_ptr instances to themselves. More...

#include <memory.hpp>

Public Member Functions

strong_ptr< T > strong_from_this ()
 Get a strong_ptr to this object.
 
strong_ptr< T const > strong_from_this () const
 Get a strong_ptr to this object (const version)
 
weak_ptr< T > weak_from_this () noexcept
 Get a weak_ptr to this object.
 
weak_ptr< T const > weak_from_this () const noexcept
 Get a weak_ptr to this object (const version)
 

Protected Member Functions

 enable_strong_from_this ()=default
 Protected constructor to prevent direct instantiation.
 
 enable_strong_from_this (enable_strong_from_this const &) noexcept
 Protected copy constructor.
 
enable_strong_from_thisoperator= (enable_strong_from_this const &) noexcept
 Protected assignment operator.
 
 ~enable_strong_from_this ()=default
 Protected destructor.
 

Friends

template<class U , typename... Args>
strong_ptr< U > make_strong_ptr (std::pmr::polymorphic_allocator<>, Args &&...)
 

Detailed Description

template<typename T>
class atlas::memory::enable_strong_from_this< T >

CRTP mixin to enable objects to create strong_ptr instances to themselves.

Similar to std::enable_shared_from_this, this class allows an object to safely obtain a strong_ptr to itself. The object must inherit from this class and be managed by a strong_ptr created via make_strong_ptr.

Example usage:

class my_driver : public enable_strong_from_this<my_driver> {
public:
void register_callback() {
// Get a strong_ptr to ourselves
auto self = strong_from_this();
some_async_system.register_callback([self](){
self->handle_callback();
});
}
};
auto obj = make_strong_ptr<my_driver>(allocator);
obj->register_callback(); // Safe to get strong_ptr to self
CRTP mixin to enable objects to create strong_ptr instances to themselves.
Definition memory.hpp:632
Template Parameters
TThe derived class type

Constructor & Destructor Documentation

◆ enable_strong_from_this()

template<typename T >
atlas::memory::enable_strong_from_this< T >::enable_strong_from_this ( enable_strong_from_this< T > const &  )
inlineprotectednoexcept

Protected copy constructor.

Note: The weak_ptr is not copied - each object gets its own weak reference

Member Function Documentation

◆ operator=()

template<typename T >
enable_strong_from_this & atlas::memory::enable_strong_from_this< T >::operator= ( enable_strong_from_this< T > const &  )
inlineprotectednoexcept

Protected assignment operator.

Note: The weak_ptr is not assigned - each object keeps its own weak reference

◆ strong_from_this() [1/2]

template<typename T >
strong_ptr< T > atlas::memory::enable_strong_from_this< T >::strong_from_this ( )
inline

Get a strong_ptr to this object.

Returns
strong_ptr<T> pointing to this object
Exceptions
hal::bad_weak_ptrif this object is not managed by a strong_ptr

◆ strong_from_this() [2/2]

template<typename T >
strong_ptr< T const > atlas::memory::enable_strong_from_this< T >::strong_from_this ( ) const
inline

Get a strong_ptr to this object (const version)

Returns
strong_ptr<T const> pointing to this object
Exceptions
hal::bad_weak_ptrif this object is not managed by a strong_ptr

◆ weak_from_this() [1/2]

template<typename T >
weak_ptr< T const > atlas::memory::enable_strong_from_this< T >::weak_from_this ( ) const
inlinenoexcept

Get a weak_ptr to this object (const version)

Returns
weak_ptr<T const> pointing to this object

◆ weak_from_this() [2/2]

template<typename T >
weak_ptr< T > atlas::memory::enable_strong_from_this< T >::weak_from_this ( )
inlinenoexcept

Get a weak_ptr to this object.

Returns
weak_ptr<T> pointing to this object

The documentation for this class was generated from the following file: