Cpp Utilities 1.2.3
Public Types | Public Member Functions | Friends | List of all members
Memory::EnableSafeSharedFromThis< T, mutex_t, read_lock_t, write_lock_t > Class Template Reference

A proxy class from std::enable_shared_from_this to provide same functionality for SafeSharedPtr. More...

#include <SafeSharedPtr.hpp>

Inheritance diagram for Memory::EnableSafeSharedFromThis< T, mutex_t, read_lock_t, write_lock_t >:

Public Types

using SharedMutex = mutex_t
 Type alias for template shared_mutex_t. More...
 
using SharedLock = read_lock_t
 Type alias for template read_lock_t. More...
 
using UniqueLock = write_lock_t
 Type alias for template write_lock_t. More...
 

Public Member Functions

constexpr EnableSafeSharedFromThis () noexcept
 Constructs a new EnableSafeSharedFromThis object. The private std::weak_ptr<T> member is empty-initialized. More...
 
 EnableSafeSharedFromThis (const EnableSafeSharedFromThis &other) noexcept
 Constructs a new EnableSafeSharedFromThis object. The private std::weak_ptr<T> member is value-initialized. More...
 
 EnableSafeSharedFromThis (const std::enable_shared_from_this< T > &other) noexcept
 Constructs a new EnableSafeSharedFromThis object. The private std::weak_ptr<T> member is value-initialized. More...
 
 ~EnableSafeSharedFromThis ()=default
 Destroys *this. More...
 
EnableSafeSharedFromThisoperator= (const EnableSafeSharedFromThis &other) noexcept
 Does nothing; returns *this. More...
 
EnableSafeSharedFromThis< T > & operator= (const std::enable_shared_from_this< T > &other) noexcept
 Does nothing; returns *this. More...
 
SafeSharedPtr< T, SharedMutex, SharedLock, UniqueLockshared_from_this ()
 Returns a SafeSharedPtr<T> that shares ownership of *this with all existing SafeSharedPtr that refer to *this. More...
 
SafeSharedPtr< T const > shared_from_this () const
 Returns a SafeSharedPtr<T const> that shares ownership of *this with all existing SafeSharedPtr that refer to *this. More...
 
SafeWeakPtr< T > weak_from_this ()
 Returns a SafeWeakPtr<T> that tracks ownership of *this by all existing SafeSharedPtr that refer to *this. More...
 
SafeWeakPtr< T const > weak_from_this () const
 Returns a SafeWeakPtr<T const> that tracks ownership of *this by all existing SafeSharedPtr that refer to *this. More...
 

Friends

template<typename Y , typename M , typename R , typename W >
class SafeSharedPtr
 

Detailed Description

template<typename T, typename mutex_t = shared_mutex_t, typename read_lock_t = shared_lock_t, typename write_lock_t = unique_lock_t>
class Memory::EnableSafeSharedFromThis< T, mutex_t, read_lock_t, write_lock_t >

A proxy class from std::enable_shared_from_this to provide same functionality for SafeSharedPtr.

Template Parameters
TObject type same as SafeSharedPtr.
mutex_tType of the mutex used, default is shared_mutex_t.
read_lock_tType of the read-lock used, default is shared_lock_t.
write_lock_tType of the write-lock used, default is unique_lock_t.

This class has no member values, so could be static_cast from std::enable_shared_from_this directly.
See https://en.cppreference.com/w/cpp/memory/enable_shared_from_this for more details.

See also
SafeSharedPtr

Member Typedef Documentation

◆ SharedMutex

template<typename T , typename mutex_t = shared_mutex_t, typename read_lock_t = shared_lock_t, typename write_lock_t = unique_lock_t>
using Memory::EnableSafeSharedFromThis< T, mutex_t, read_lock_t, write_lock_t >::SharedMutex = mutex_t

Type alias for template shared_mutex_t.

◆ SharedLock

template<typename T , typename mutex_t = shared_mutex_t, typename read_lock_t = shared_lock_t, typename write_lock_t = unique_lock_t>
using Memory::EnableSafeSharedFromThis< T, mutex_t, read_lock_t, write_lock_t >::SharedLock = read_lock_t

Type alias for template read_lock_t.

◆ UniqueLock

template<typename T , typename mutex_t = shared_mutex_t, typename read_lock_t = shared_lock_t, typename write_lock_t = unique_lock_t>
using Memory::EnableSafeSharedFromThis< T, mutex_t, read_lock_t, write_lock_t >::UniqueLock = write_lock_t

Type alias for template write_lock_t.

Constructor & Destructor Documentation

◆ EnableSafeSharedFromThis() [1/3]

template<typename T , typename mutex_t = shared_mutex_t, typename read_lock_t = shared_lock_t, typename write_lock_t = unique_lock_t>
constexpr Memory::EnableSafeSharedFromThis< T, mutex_t, read_lock_t, write_lock_t >::EnableSafeSharedFromThis ( )
inlineconstexprnoexcept

Constructs a new EnableSafeSharedFromThis object. The private std::weak_ptr<T> member is empty-initialized.

See also
SafeSharedPtr

◆ EnableSafeSharedFromThis() [2/3]

template<typename T , typename mutex_t = shared_mutex_t, typename read_lock_t = shared_lock_t, typename write_lock_t = unique_lock_t>
Memory::EnableSafeSharedFromThis< T, mutex_t, read_lock_t, write_lock_t >::EnableSafeSharedFromThis ( const EnableSafeSharedFromThis< T, mutex_t, read_lock_t, write_lock_t > &  other)
inlinenoexcept

Constructs a new EnableSafeSharedFromThis object. The private std::weak_ptr<T> member is value-initialized.

Parameters
otherAnother EnableSafeSharedFromThis to copy.
Note
There is no move constructor: moving from an object derived from EnableSafeSharedFromThis does not transfer its shared identity.
See also
SafeSharedPtr

◆ EnableSafeSharedFromThis() [3/3]

template<typename T , typename mutex_t = shared_mutex_t, typename read_lock_t = shared_lock_t, typename write_lock_t = unique_lock_t>
Memory::EnableSafeSharedFromThis< T, mutex_t, read_lock_t, write_lock_t >::EnableSafeSharedFromThis ( const std::enable_shared_from_this< T > &  other)
inlinenoexcept

Constructs a new EnableSafeSharedFromThis object. The private std::weak_ptr<T> member is value-initialized.

Parameters
otherA std::enable_shared_from_this to copy.
Note
There is no move constructor: moving from an object derived from EnableSafeSharedFromThis does not transfer its shared identity.
See also
SafeSharedPtr

◆ ~EnableSafeSharedFromThis()

template<typename T , typename mutex_t = shared_mutex_t, typename read_lock_t = shared_lock_t, typename write_lock_t = unique_lock_t>
Memory::EnableSafeSharedFromThis< T, mutex_t, read_lock_t, write_lock_t >::~EnableSafeSharedFromThis ( )
default

Destroys *this.

See also
SafeSharedPtr

Member Function Documentation

◆ operator=() [1/2]

template<typename T , typename mutex_t = shared_mutex_t, typename read_lock_t = shared_lock_t, typename write_lock_t = unique_lock_t>
EnableSafeSharedFromThis & Memory::EnableSafeSharedFromThis< T, mutex_t, read_lock_t, write_lock_t >::operator= ( const EnableSafeSharedFromThis< T, mutex_t, read_lock_t, write_lock_t > &  other)
inlinenoexcept

Does nothing; returns *this.

Parameters
otherAnother EnableSafeSharedFromThis to assign to *this.
Returns
*this.
Note
The private std::weak_ptr<T> member is not affected by this assignment operator.
See also
SafeSharedPtr

◆ operator=() [2/2]

template<typename T , typename mutex_t = shared_mutex_t, typename read_lock_t = shared_lock_t, typename write_lock_t = unique_lock_t>
EnableSafeSharedFromThis< T > & Memory::EnableSafeSharedFromThis< T, mutex_t, read_lock_t, write_lock_t >::operator= ( const std::enable_shared_from_this< T > &  other)
inlinenoexcept

Does nothing; returns *this.

Parameters
otherA std::enable_shared_from_this to assign to *this.
Returns
*this.
Note
The private std::weak_ptr<T> member is not affected by this assignment operator.
See also
SafeSharedPtr

◆ shared_from_this() [1/2]

template<typename T , typename mutex_t = shared_mutex_t, typename read_lock_t = shared_lock_t, typename write_lock_t = unique_lock_t>
SafeSharedPtr< T, SharedMutex, SharedLock, UniqueLock > Memory::EnableSafeSharedFromThis< T, mutex_t, read_lock_t, write_lock_t >::shared_from_this ( )
inline

Returns a SafeSharedPtr<T> that shares ownership of *this with all existing SafeSharedPtr that refer to *this.

Returns
SafeSharedPtr<T> that shares ownership of *this with pre-existing SafeSharedPtrs.

Effectively executes SafeSharedPtr<T>(weak_this), where weak_this is the private mutable std::weak_ptr<T> member of std::enable_shared_from_this base class.

Note
It is permitted to call shared_from_this only on a previously shared object, i.e. on an object managed by SafeSharedPtr (in particular, shared_from_this cannot be called in a constructor).
Ohterwise:
  • Until C++17: The behavior is undefined.
  • Since C++17: std::bad_weak_ptr is thrown (by the SafeSharedPtr constructor from a default-constructed weak_this).
Exceptions
std::bad_weak_ptrSince C++17: When enable_shared_from_this called in constructor, exception will be thrown by the SafeSharedPtr constructor from a default-constructed weak_this.
See also
SafeSharedPtr

◆ shared_from_this() [2/2]

template<typename T , typename mutex_t = shared_mutex_t, typename read_lock_t = shared_lock_t, typename write_lock_t = unique_lock_t>
SafeSharedPtr< T const > Memory::EnableSafeSharedFromThis< T, mutex_t, read_lock_t, write_lock_t >::shared_from_this ( ) const
inline

Returns a SafeSharedPtr<T const> that shares ownership of *this with all existing SafeSharedPtr that refer to *this.

Returns
SafeSharedPtr<T const> that shares ownership of *this with pre-existing SafeSharedPtrs.

Effectively executes SafeSharedPtr<T>(weak_this), where weak_this is the private mutable std::weak_ptr<T> member of std::enable_shared_from_this base class.

Note
It is permitted to call shared_from_this only on a previously shared object, i.e. on an object managed by SafeSharedPtr (in particular, shared_from_this cannot be called in a constructor).
Ohterwise:
  • Until C++17: The behavior is undefined.
  • Since C++17: std::bad_weak_ptr is thrown (by the SafeSharedPtr constructor from a default-constructed weak_this).
Exceptions
std::bad_weak_ptrSince C++17: When enable_shared_from_this called in constructor, exception will be thrown by the SafeSharedPtr constructor from a default-constructed weak_this.
See also
SafeSharedPtr

◆ weak_from_this() [1/2]

template<typename T , typename mutex_t = shared_mutex_t, typename read_lock_t = shared_lock_t, typename write_lock_t = unique_lock_t>
SafeWeakPtr< T > Memory::EnableSafeSharedFromThis< T, mutex_t, read_lock_t, write_lock_t >::weak_from_this ( )
inline

Returns a SafeWeakPtr<T> that tracks ownership of *this by all existing SafeSharedPtr that refer to *this.

Returns
SafeWeakPtr<T> that shares ownership of *this with pre-existing SafeSharedPtrs;
Note
This is a copy of the the private mutable std::weak_ptr member that is part of std::enable_shared_from_this.
See also
SafeSharedPtr

◆ weak_from_this() [2/2]

template<typename T , typename mutex_t = shared_mutex_t, typename read_lock_t = shared_lock_t, typename write_lock_t = unique_lock_t>
SafeWeakPtr< T const > Memory::EnableSafeSharedFromThis< T, mutex_t, read_lock_t, write_lock_t >::weak_from_this ( ) const
inline

Returns a SafeWeakPtr<T const> that tracks ownership of *this by all existing SafeSharedPtr that refer to *this.

Returns
SafeWeakPtr<T const> that shares ownership of *this with pre-existing SafeSharedPtrs;
Note
This is a copy of the the private mutable std::weak_ptr member that is part of std::enable_shared_from_this.
See also
SafeSharedPtr

Friends And Related Function Documentation

◆ SafeSharedPtr

template<typename T , typename mutex_t = shared_mutex_t, typename read_lock_t = shared_lock_t, typename write_lock_t = unique_lock_t>
template<typename Y , typename M , typename R , typename W >
friend class SafeSharedPtr
friend

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