CsPointer  2.0.0
CsPointer::CsWeakPointer< T > Class Template Reference

Stores a weak pointer to a potentially shared object. More...

Public Typedefs

using element_type = typename std::weak_ptr< T >::element_type
 
using pointer = element_type *
 
using Pointer = pointer
 
using ElementType = element_type
 

Public Member Functions

 CsWeakPointer ()
 
template<typename U >
 CsWeakPointer (const CsWeakPointer< U > &p) noexcept
 
template<typename U >
 CsWeakPointer (const CsSharedPointer< U > &p) noexcept
 
 ~CsWeakPointer () = default
 
 CsWeakPointer (const CsWeakPointer &other) = default
 
CsWeakPointer & operator= (const CsWeakPointer &other) = default
 
 CsWeakPointer (CsWeakPointer &&other) = default
 
CsWeakPointer & operator= (CsWeakPointer &&other) = default
 
template<typename U >
CsWeakPointer & operator= (const CsSharedPointer< U > &p) noexcept
 
bool operator! () const
 
 operator bool () const noexcept
 
void clear () noexcept
 
bool expired () const noexcept
 
CsSharedPointer< T > lock () const noexcept
 
bool is_null () const noexcept
 
template<typename U >
bool owner_before (const CsSharedPointer< U > &p) const noexcept
 
template<typename U >
bool owner_before (const CsWeakPointer< U > &p) const noexcept
 
void reset () noexcept
 
void swap (CsWeakPointer &other) noexcept
 
CsSharedPointer< T > toStrongRef () const noexcept
 
long use_count () const noexcept
 

Related Functions

These are not member functions

bool operator!= (const CsWeakPointer< T1 > &ptr1, const CsSharedPointer< T2 > &ptr2)
 
bool operator!= (const CsSharedPointer< T1 > &ptr1, const CsWeakPointer< T2 > &ptr2)
 
bool operator== (const CsWeakPointer< T1 > &ptr1, const CsSharedPointer< T2 > &ptr2)
 
bool operator== (const CsSharedPointer< T1 > &ptr1, const CsWeakPointer< T2 > &ptr2)
 

Detailed Description

template<typename T>
class CsPointer::CsWeakPointer< T >

The CsWeakPointer class stores a weak pointer to a potentially shared object. A weak pointer is a smart pointer which does take ownership or manage the lifetime of the shared object. A CsWeakPointer can only be created by calling CsSharedPointer::toWeakRef() or using the constructor in CsWeakPointer which accepts a CsSharedPointer.

To access the object which CsWeakPointer points to, convert it to CsSharedPointer and ensure the shared pointer is not a nullptr. If the shared pointer is not a nullptr, the object will not be deleted while the shared pointer is in scope.

See also
CsSharedPointer

Member Typedef Documentation

template<typename T >
CsWeakPointer::element_type

Typedef for std::weak_ptr<T>::element_type.

template<typename T >
CsWeakPointer::ElementType

Typedef for element_type.

template<typename T >
CsWeakPointer::pointer

Typedef for element_type *.

template<typename T >
CsWeakPointer::Pointer

Typedef for pointer.

Constructor & Destructor Documentation

template<typename T >
CsWeakPointer::CsWeakPointer ( )
inline

Creates a new CsWeakPointer which contains a nullptr.

template<typename T >
template<typename U >
CsWeakPointer::CsWeakPointer ( const CsWeakPointer< U > &  p)
inlinenoexcept

Creates a new CsWeakPointer from p. The template parameter U must inherit from T.

template<typename T >
template<typename U >
CsWeakPointer::CsWeakPointer ( const CsSharedPointer< U > &  p)
inlinenoexcept

Constructs a new CsWeakPointer from the shared pointer p.

template<typename T >
CsWeakPointer::~CsWeakPointer ( )
default

Destroys this CsWeakPointer object. The object this pointer points to will not be deleted.

template<typename T >
CsWeakPointer::CsWeakPointer ( const CsWeakPointer< T > &  other)
default

Copy constructs a new CsWeakPointer from other.

template<typename T >
CsWeakPointer::CsWeakPointer ( CsWeakPointer< T > &&  other)
default

Move constructs a new CsWeakPointer from other.

Member Function Documentation

template<typename T >
void CsWeakPointer::clear ( )
inlinenoexcept

Sets this CsWeakPointer object to a nullptr. The object this pointer points to will not be deleted.

template<typename T >
bool CsWeakPointer::expired ( ) const
inlinenoexcept

Equivalent to calling is_null().

template<typename T >
bool CsWeakPointer::is_null ( ) const
inlinenoexcept

Returns true if the current CsWeakPointer contains a nullptr. Since the object this weak pointer points to can be deleted at any time, a return value of true does not guarantee the object is still alive after calling this method.

template<typename T >
void CsWeakPointer::lock ( ) const
inlinenoexcept

Equivalent to calling toStrongRef().

template<typename T >
CsWeakPointer::operator bool ( ) const
inlineexplicitnoexcept

Returns true if the current CsWeakPointer is not a nullptr. This method is called when a shared pointer is used in an "if statement" or another context where a boolean value is expected. Since this method is explicit it is not possible to assign a weak pointer to a boolean.

// example 1
if (myWeakPtr) {
...
}
// example 2
bool isTest_A = myWeakPtr; // will *not* compile
bool isTest_B = (myWeakPtr != nullptr); // compiles, valid code
bool isTest_C = bool(myWeakPtr); // compiles, valid code
template<typename T >
bool CsWeakPointer::operator! ( ) const
inline

Returns true if this object is a nullptr. This method is called when a shared pointer is used in an "if statement" or another context where a boolean value is expected.

if (! myWeakPtr) {
...
}
template<typename T >
template<typename U >
CsWeakPointer & CsWeakPointer::operator= ( const CsSharedPointer< U > &  p)
inlinenoexcept

Assigns from the shared pointer p and returns a reference to this object.

template<typename T >
CsWeakPointer & CsWeakPointer::operator= ( const CsWeakPointer< T > &  other)
default

Copy assigns from other and returns a reference to this object.

template<typename T >
CsWeakPointer & CsWeakPointer::operator= ( CsWeakPointer< T > &&  other)
default

Move assigns from other and returns a reference to this object.

template<typename T >
template<typename U >
bool CsWeakPointer::owner_before ( const CsSharedPointer< U > &  p) const
inlinenoexcept

Returns true if the current pointer is less than p, otherwise returns false.

template<typename T >
template<typename U >
bool CsWeakPointer::owner_before ( const CsWeakPointer< U > &  p) const
inlinenoexcept

Returns true if the current pointer is less than p, otherwise returns false.

template<typename T >
void CsWeakPointer::reset ( )
inlinenoexcept

Equivalent to calling clear().

template<typename T >
void CsWeakPointer::swap ( CsWeakPointer< T > &  other)
inlinenoexcept

Swaps the current CsWeakPointer with other. This method is very fast and never fails.

template<typename T >
CsSharedPointer< T > CsWeakPointer::toStrongRef ( ) const
inlinenoexcept

Converts the current weak pointer to a shared pointer and returns the CsSharedPointer. This method can fail if the object the CsWeakPointer is pointing to has been deleted. To prevent undefined behavior verify the return value by calling CsSharedPointer::is_null() before dereferencing.

if (ptr != nullptr) {
std::cout << "Pointer is valid and points to an existing object";
}
template<typename T >
long CsWeakPointer::use_count ( ) const
inlinenoexcept

Returns the reference count for the current CsWeakPointer.

Friends And Related Function Documentation

bool operator!= ( const CsSharedPointer< T1 > &  ptr1,
const CsWeakPointer< T2 > &  ptr2 
)
related

Returns true if ptr1 and ptr2 do not point to the same object.

bool operator!= ( const CsWeakPointer< T1 > &  ptr1,
const CsSharedPointer< T2 > &  ptr2 
)
related

Returns true if ptr1 and ptr2 do not point to the same object.

bool operator== ( const CsSharedPointer< T1 > &  ptr1,
const CsWeakPointer< T2 > &  ptr2 
)
related

Returns true if ptr1 and ptr2 point to the same object.

bool operator== ( const CsWeakPointer< T1 > &  ptr1,
const CsSharedPointer< T2 > &  ptr2 
)
related

Returns true if ptr1 and ptr2 point to the same object.