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

Wrapper to std::weak_ptr to provide weak reference for SafeSharedPtr. More...

#include <SafeSharedPtr.hpp>

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...
 
using element_type = typename SafeSharedPtr< T >::element_type
 Same element_type of SafeSharedPtr. More...
 

Public Member Functions

constexpr SafeWeakPtr () noexcept=default
 Default constructor. Constructs empty weak_ptr. More...
 
template<typename Y >
 SafeWeakPtr (const SafeWeakPtr< Y, SharedMutex, SharedLock, UniqueLock > &other)
 Constructs new SafeWeakPtr that shares an object with other. More...
 
template<typename Y >
 SafeWeakPtr (const SafeSharedPtr< Y, SharedMutex, SharedLock, UniqueLock > &other)
 Constructs new SafeWeakPtr that shares an object with other. More...
 
 ~SafeWeakPtr ()=default
 Destroys the weak_ptr object. Results in no effect to the managed object. More...
 
SafeWeakPtr< T > & operator= (const SafeWeakPtr< T > &other) noexcept
 Replaces the managed object with the one managed by other. More...
 
template<typename Y >
SafeWeakPtr< T > & operator= (const SafeWeakPtr< Y, SharedMutex, SharedLock, UniqueLock > &other) noexcept
 Replaces the managed object with the one managed by other. More...
 
void reset () noexcept
 Releases the ownership of the managed object. After the call *this manages no object. More...
 
void swap (SafeWeakPtr< T > &other) noexcept
 Exchanges the contents of *this and other. More...
 
long use_count () const noexcept
 Returns the number of SafeSharedPtr instances that share ownership of the managed object, or 0 if the managed object has already been deleted, i.e. *this is empty. More...
 
bool expired () const noexcept
 Checks whether the referenced object was already deleted. More...
 
SafeSharedPtr< T > lock () const noexcept
 Creates a SafeSharedPtr that manages the referenced object. More...
 
template<typename Y , typename M , typename R , typename W >
bool owner_before (const SafeWeakPtr< Y, M, R, W > &other) const
 Provides owner-based ordering of weak pointers. More...
 
template<typename Y , typename M , typename R , typename W >
bool owner_before (const SafeSharedPtr< Y, M, R, W > &other) const
 Provides owner-based ordering of weak pointers. More...
 

Friends

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

Related Functions

(Note that these are not member functions.)

template<typename T , typename SharedMutex = Memory::shared_mutex_t, typename SharedLock = Memory::shared_lock_t, typename UniqueLock = Memory::unique_lock_t>
void swap (Memory::SafeWeakPtr< T, SharedMutex, SharedLock, UniqueLock > &lhs, Memory::SafeWeakPtr< T, SharedMutex, SharedLock, UniqueLock > &rhs) noexcept
 Specializes the std::swap algorithm. More...
 

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::SafeWeakPtr< T, mutex_t, read_lock_t, write_lock_t >

Wrapper to std::weak_ptr to provide weak reference for SafeSharedPtr.

Template Parameters
TType of the object held by SafeWeakPtr.
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.

Same API as std::weak_ptr.
See https://en.cppreference.com/w/cpp/memory/weak_ptr for more details of functionalities with std::weak_ptr.

Note
Before C++17, for the purposes of the description below, a pointer type Y* requires that Y* must be implicitly convertible to T*.
Since C++17, for the purposes of the description below, a pointer type Y* is said to be compatible with a pointer type T* if either Y* is convertible to T* or Y is the array type U[N] and T is U cv [] (where cv is some set of cv-qualifiers).
Since C++17, default deleter called on destructor will use delete[] if T is an arry type;
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::SafeWeakPtr< 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::SafeWeakPtr< 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::SafeWeakPtr< T, mutex_t, read_lock_t, write_lock_t >::UniqueLock = write_lock_t

Type alias for template write_lock_t.

◆ element_type

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::SafeWeakPtr< T, mutex_t, read_lock_t, write_lock_t >::element_type = typename SafeSharedPtr<T>::element_type

Same element_type of SafeSharedPtr.

Constructor & Destructor Documentation

◆ SafeWeakPtr() [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::SafeWeakPtr< T, mutex_t, read_lock_t, write_lock_t >::SafeWeakPtr ( )
constexprdefaultnoexcept

Default constructor. Constructs empty weak_ptr.

◆ SafeWeakPtr() [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>
template<typename Y >
Memory::SafeWeakPtr< T, mutex_t, read_lock_t, write_lock_t >::SafeWeakPtr ( const SafeWeakPtr< Y, SharedMutex, SharedLock, UniqueLock > &  other)
inline

Constructs new SafeWeakPtr that shares an object with other.

Template Parameters
YElement type of input weak pointer.
Parameters
otherWeak pointer to share object from.

Constructs new SafeWeakPtr which shares an object managed by other. If other manages no object, *this manages no object too.

◆ SafeWeakPtr() [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>
template<typename Y >
Memory::SafeWeakPtr< T, mutex_t, read_lock_t, write_lock_t >::SafeWeakPtr ( const SafeSharedPtr< Y, SharedMutex, SharedLock, UniqueLock > &  other)
inline

Constructs new SafeWeakPtr that shares an object with other.

Template Parameters
YElement type of input shared pointer.
Parameters
otherShared pointer to share object from.

Constructs new SafeWeakPtr which shares an object managed by other. If other manages no object, *this manages no object too.

◆ ~SafeWeakPtr()

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

Destroys the weak_ptr object. Results in no effect to the managed object.

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>
SafeWeakPtr< T > & Memory::SafeWeakPtr< T, mutex_t, read_lock_t, write_lock_t >::operator= ( const SafeWeakPtr< T > &  other)
inlinenoexcept

Replaces the managed object with the one managed by other.

Parameters
otherSmart pointer to share an object with.
Returns
*this.

The object is shared with other. If other manages no object, *this manages no object too. Equivalent to SafeWeakPtr<T>(other).swap(*this).

Note
The implementation may meet the requirements without creating a temporary SafeWeakPtr object.

◆ 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>
template<typename Y >
SafeWeakPtr< T > & Memory::SafeWeakPtr< T, mutex_t, read_lock_t, write_lock_t >::operator= ( const SafeWeakPtr< Y, SharedMutex, SharedLock, UniqueLock > &  other)
inlinenoexcept

Replaces the managed object with the one managed by other.

Template Parameters
YElement type of input pointer
Parameters
otherSmart pointer to share an object with.
Returns
*this.

The object is shared with other. If other manages no object, *this manages no object too. Equivalent to SafeWeakPtr<T>(other).swap(*this).

Note
The implementation may meet the requirements without creating a temporary SafeWeakPtr object.

◆ reset()

template<typename T , typename mutex_t = shared_mutex_t, typename read_lock_t = shared_lock_t, typename write_lock_t = unique_lock_t>
void Memory::SafeWeakPtr< T, mutex_t, read_lock_t, write_lock_t >::reset ( )
inlinenoexcept

Releases the ownership of the managed object. After the call *this manages no object.

◆ swap()

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

Exchanges the contents of *this and other.

Parameters
otherAnother weak pointer to exchange the contents with.

Complexity
Constant.

◆ use_count()

template<typename T , typename mutex_t = shared_mutex_t, typename read_lock_t = shared_lock_t, typename write_lock_t = unique_lock_t>
long Memory::SafeWeakPtr< T, mutex_t, read_lock_t, write_lock_t >::use_count ( ) const
inlinenoexcept

Returns the number of SafeSharedPtr instances that share ownership of the managed object, or 0 if the managed object has already been deleted, i.e. *this is empty.

Returns
The number of SafeSharedPtr instances sharing the ownership of the managed object at the instant of the call.
Note
expired() may be faster than use_count(). This function is inherently racy, if the managed object is shared among threads that might be creating and destroying copies of the SafeSharedPtr: then, the result is reliable only if it matches the number of copies uniquely owned by the calling thread, or zero; any other value may become stale before it can be used.
See also
expired

◆ expired()

template<typename T , typename mutex_t = shared_mutex_t, typename read_lock_t = shared_lock_t, typename write_lock_t = unique_lock_t>
bool Memory::SafeWeakPtr< T, mutex_t, read_lock_t, write_lock_t >::expired ( ) const
inlinenoexcept

Checks whether the referenced object was already deleted.

Returns
true if the managed object has already been deleted, false otherwise.

Equivalent to use_count() == 0. The destructor for the managed object may not yet have been called, but this object's destruction is imminent (or may have already happened).

Note
This function is inherently racy if the managed object is shared among threads. In particular, a false result may become stale before it can be used. A true result is reliable.
See also
lock, use_count

◆ lock()

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 > Memory::SafeWeakPtr< T, mutex_t, read_lock_t, write_lock_t >::lock ( ) const
inlinenoexcept

Creates a SafeSharedPtr that manages the referenced object.

Returns
A SafeSharedPtr which shares ownership of the owned object if expired returns false. Else returns default-constructed SafeSharedPtr of type T.

Creates a new SafeSharedPtr that shares ownership of the managed object. If there is no managed object, i.e. *this is empty, then the returned SafeSharedPtr also is empty.\n Effectively returns expired() ? SafeSharedPtr<T>() : SafeSharedPtr<T>(*this), executed atomically.

Note
Both this function and the constructor of SafeSharedPtr may be used to acquire temporary ownership of the managed object referred to by a SafeWeakPtr. The difference is that the constructor of SafeSharedPtr throws an exception when its SafeWeakPtr argument is empty, while lock() constructs an empty SafeSharedPtr<T>.

◆ owner_before() [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>
template<typename Y , typename M , typename R , typename W >
bool Memory::SafeWeakPtr< T, mutex_t, read_lock_t, write_lock_t >::owner_before ( const SafeWeakPtr< Y, M, R, W > &  other) const
inline

Provides owner-based ordering of weak pointers.

Template Parameters
YType of the object managed by input pointer.
MType of the mutex used, default is shared_mutex_t.
RType of the read-lock used, default is shared_lock_t.
WType of the write-lock used, default is unique_lock_t.
Parameters
otherThe SafeWeakPtr to be compared.
Returns
true if *this precedes other, false otherwise. Common implementations compare the addresses of the control blocks.

Checks whether this SafeWeakPtr precedes other in implementation defined owner-based (as opposed to value-based) order. The order is such that two smart pointers compare equivalent only if they are both empty or if they both own the same object, even if the values of the pointers obtained by get() are different (e.g. because they point at different subobjects within the same object).
This ordering is used to make shared and weak pointers usable as keys in associative containers, typically through std::owner_less.

◆ owner_before() [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>
template<typename Y , typename M , typename R , typename W >
bool Memory::SafeWeakPtr< T, mutex_t, read_lock_t, write_lock_t >::owner_before ( const SafeSharedPtr< Y, M, R, W > &  other) const
inline

Provides owner-based ordering of weak pointers.

Template Parameters
YType of the object managed by input pointer.
MType of the mutex used, default is shared_mutex_t.
RType of the read-lock used, default is shared_lock_t.
WType of the write-lock used, default is unique_lock_t.
Parameters
otherThe SafeSharedPtr to be compared.
Returns
true if *this precedes other, false otherwise. Common implementations compare the addresses of the control blocks.

Checks whether this SafeWeakPtr precedes other in implementation defined owner-based (as opposed to value-based) order. The order is such that two smart pointers compare equivalent only if they are both empty or if they both own the same object, even if the values of the pointers obtained by get() are different (e.g. because they point at different subobjects within the same object).
This ordering is used to make shared and weak pointers usable as keys in associative containers, typically through std::owner_less.

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

◆ swap()

template<typename T , typename SharedMutex = Memory::shared_mutex_t, typename SharedLock = Memory::shared_lock_t, typename UniqueLock = Memory::unique_lock_t>
void swap ( Memory::SafeWeakPtr< T, SharedMutex, SharedLock, UniqueLock > &  lhs,
Memory::SafeWeakPtr< T, SharedMutex, SharedLock, UniqueLock > &  rhs 
)
related

Specializes the std::swap algorithm.

Template Parameters
TElement type of input shared pointers.
SharedMutexType of the mutex used, default is shared_mutex_t.
SharedLockType of the read-lock used, default is shared_lock_t.
UniqueLockType of the write-lock used, default is unique_lock_t.
Parameters
lhsShared pointer whose contents to swap.
rhsAnother shared pointer whose contents to swap.

Specializes the std::swap algorithm for Memory::SafeWeakPtr. Swaps the pointers of lhs and rhs. Calls lhs.swap(rhs).

Complexity
Constant.


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