You.i Engine
CYIWeakPtr< YI_PTR_TYPE > Class Template Reference

Detailed Description

template<typename YI_PTR_TYPE>
class CYIWeakPtr< YI_PTR_TYPE >

The CYIWeakPtr is a smart pointer that does not take any ownership of a dynamically allocated object. It will never delete the object. When the object is deleted by the last strong reference, CYIWeakPtr will automatically point to nullptr.

Deprecated:
This class has been deprecated and may be removed in a future release. The std::weak_ptr class should be used instead.

CYIWeakPtr can serve as an object lifetime tracker while retaining the ability to access the object while being alive.

Consider the following example:

class MyClass
{
private:
CYIWeakPtr<MyData> m_pWeakPtr;
public:
MyClass(const CYISharedPtr<MyData> &pStrongPtr) : m_pWeakPtr(pStrongPtr.ToWeakPtr()) // creating a weak reference
{
}
int32_t GetValue() const
{
CYISharedPtr<MyData> pStrongPtr = m_pWeakPtr.ToStrong(); // lock the ref count if it exists
if (pStrongPtr) // checking if the object is still alive.
{
return pStrongPtr->GetValue();
}
else // pointer deletion has been detected
{
return -1;
}
}
};
Remarks
CYIWeakPtr is thread-safe, with exceptions. See CYISharedPtr for details.
See also
CYISharedArray
CYISharedPtr
CYIWeakArray
CYIScopedPtr
CYIScopedArray

#include <smartptr/YiSharedPtr.h>

Inheritance diagram for CYIWeakPtr< YI_PTR_TYPE >:

Public Member Functions

 CYIWeakPtr ()
 
 CYIWeakPtr (const CYIWeakPtr< YI_PTR_TYPE > &rOther)
 
template<typename YI_OTHER_PTR_TYPE >
 CYIWeakPtr (const CYIWeakPtr< YI_OTHER_PTR_TYPE > &rOther)
 
 CYIWeakPtr (const CYISharedPtr< YI_PTR_TYPE > &rOther)
 
 CYIWeakPtr (const std::shared_ptr< YI_PTR_TYPE > &rOther)
 
template<typename YI_OTHER_PTR_TYPE >
 CYIWeakPtr (const std::weak_ptr< YI_OTHER_PTR_TYPE > &rOther)
 
template<typename YI_OTHER_PTR_TYPE >
 CYIWeakPtr (const CYISharedPtr< YI_OTHER_PTR_TYPE > &rOther)
 
 ~CYIWeakPtr ()
 
void Reset ()
 
YI_PTR_TYPE * Get () const
 
bool IsNull () const
 
uint32_t GetUseCount () const
 
void Swap (CYIWeakPtr< YI_PTR_TYPE > &rPtr)
 
CYISharedPtr< YI_PTR_TYPE > ToStrongPtr () const
 
bool operator! () const
 
 operator bool () const
 
template<typename YI_OTHER_PTR_TYPE >
 operator std::weak_ptr< YI_OTHER_PTR_TYPE > () const
 
CYIWeakPtr< YI_PTR_TYPE > & operator= (const CYIWeakPtr< YI_PTR_TYPE > &rOther)
 
template<typename YI_OTHER_PTR_TYPE >
CYIWeakPtr< YI_PTR_TYPE > & operator= (const CYIWeakPtr< YI_OTHER_PTR_TYPE > &rOther)
 
CYIWeakPtr< YI_PTR_TYPE > & operator= (const CYISharedPtr< YI_PTR_TYPE > &rOther)
 
template<typename YI_OTHER_PTR_TYPE >
CYIWeakPtr< YI_PTR_TYPE > & operator= (const CYISharedPtr< YI_OTHER_PTR_TYPE > &rOther)
 
template<typename YI_OTHER_PTR_TYPE >
CYIWeakPtr< YI_PTR_TYPE > & operator= (const std::shared_ptr< YI_OTHER_PTR_TYPE > &rOther)
 

Friends

template<typename YI_OTHER_PTR_TYPE >
class CYISharedPtr
 
template<typename YI_OTHER_PTR_TYPE >
class CYIWeakPtr
 

Constructor & Destructor Documentation

template<typename YI_PTR_TYPE>
CYIWeakPtr< YI_PTR_TYPE >::CYIWeakPtr ( )

Constructs CYIWeakPtr and sets its internal object to nullptr

template<typename YI_PTR_TYPE>
CYIWeakPtr< YI_PTR_TYPE >::CYIWeakPtr ( const CYIWeakPtr< YI_PTR_TYPE > &  rOther)

Copy-Constructor that constructs CYIWeakPtr and sets its internal object to the provided parameter

Parameters
rOtherthe data to take shared ownership
template<typename YI_PTR_TYPE>
template<typename YI_OTHER_PTR_TYPE >
CYIWeakPtr< YI_PTR_TYPE >::CYIWeakPtr ( const CYIWeakPtr< YI_OTHER_PTR_TYPE > &  rOther)

Constructs CYIWeakPtr and sets its internal object to the provided parameter

Parameters
rOtherthe data to take shared ownership
template<typename YI_PTR_TYPE>
CYIWeakPtr< YI_PTR_TYPE >::CYIWeakPtr ( const CYISharedPtr< YI_PTR_TYPE > &  rOther)

Constructs CYIWeakPtr and sets its internal object to the provided parameter

Parameters
rOtherthe data to take shared ownership
template<typename YI_PTR_TYPE>
CYIWeakPtr< YI_PTR_TYPE >::CYIWeakPtr ( const std::shared_ptr< YI_PTR_TYPE > &  rOther)

Constructs CYIWeakPtr and sets its internal object to the provided parameter

Parameters
rOtherthe data to take shared ownership
template<typename YI_PTR_TYPE>
template<typename YI_OTHER_PTR_TYPE >
CYIWeakPtr< YI_PTR_TYPE >::CYIWeakPtr ( const std::weak_ptr< YI_OTHER_PTR_TYPE > &  rOther)

Constructs CYIWeakPtr and sets its internal object to the provided parameter

Parameters
rOtherthe data to take shared ownership
template<typename YI_PTR_TYPE>
template<typename YI_OTHER_PTR_TYPE >
CYIWeakPtr< YI_PTR_TYPE >::CYIWeakPtr ( const CYISharedPtr< YI_OTHER_PTR_TYPE > &  rOther)

Constructs CYIWeakPtr and sets its internal object to the provided parameter

Parameters
rOtherthe data to take shared ownership
template<typename YI_PTR_TYPE>
CYIWeakPtr< YI_PTR_TYPE >::~CYIWeakPtr ( )

Destructor that will never delete the object

See also
~CYISharedPtr()

Member Function Documentation

template<typename YI_PTR_TYPE>
YI_PTR_TYPE* CYIWeakPtr< YI_PTR_TYPE >::Get ( ) const

Accesses the internal object without taking ownership. Mostly for legacy code compatibility.

Example:

void my_legacy_function(MyData *pPtr)
{
...
}
void Func(CYIWeakPtr<MyData> &pData)
{
my_legacy_function(pData.Get());
}
Returns
Returns the internal object
Warning
The use of this function should be avoided since the pointer returned is non-reference-counted. Here is the intended way to access the pointer:
void Func(CYIWeakPtr<MyData> &pData)
{
// first, protect yourself by locking the instance
CYISharedPtr<MyData> pPtr(pData); // could have used pData.ToStrongPtr()
if (!pPtr.IsNull()) // make sure it didn't turn null on us
{
//now the pointer is safe to use
pPtr->m_myString = "Safe to store";
}
}
template<typename YI_PTR_TYPE>
uint32_t CYIWeakPtr< YI_PTR_TYPE >::GetUseCount ( ) const

Returns the number of CYISharedPtr<T> objects that manage the object T.

template<typename YI_PTR_TYPE>
bool CYIWeakPtr< YI_PTR_TYPE >::IsNull ( ) const

Syntactic sugar for the operator!()

Example:

void Func(CYISharedPtr<MyData> pStrongPtr)
{
CYIWeakPtr<MyData> pData(pStrongPtr);
if (pData.IsNull()) // will be false
{
}
...
pData.Reset();
if (pData.IsNull()) // will be true since the internal object is now null.
{
}
}
Returns
Returns true if the internal object is null, false otherwise.
See also
operator!()
template<typename YI_PTR_TYPE>
CYIWeakPtr< YI_PTR_TYPE >::operator bool ( ) const

This function tests if the internal object is not set to nullptr.

Example:

void Func(CYISharedPtr<MyData> pStrongPtr)
{
CYIWeakPtr<MyData> pData(pStrongPtr);
if (pData) // will be true
{
}
...
pData.Reset();
if (pData) // will be false since the internal object is now null.
{
}
}
Returns
Returns true if the internal object is null, false otherwise.
See also
operator!()
template<typename YI_PTR_TYPE>
template<typename YI_OTHER_PTR_TYPE >
CYIWeakPtr< YI_PTR_TYPE >::operator std::weak_ptr< YI_OTHER_PTR_TYPE > ( ) const

This function is used to implicitely convert from CYIWeakPtr to std::weak_ptr for backwards compatibility.

template<typename YI_PTR_TYPE>
bool CYIWeakPtr< YI_PTR_TYPE >::operator! ( ) const

This function tests if the internal object is set to nullptr.

Example:

void Func(CYISharedPtr<MyData> pStrongPtr)
{
CYIWeakPtr<MyData> pData(pStrongPtr);
if (!pData) // will be false
{
}
...
pData.Reset();
if (!pData) // will be true since the internal object is now null.
{
}
}
Returns
Returns true if the internal object is null, false otherwise.
See also
operator!()
template<typename YI_PTR_TYPE>
CYIWeakPtr<YI_PTR_TYPE>& CYIWeakPtr< YI_PTR_TYPE >::operator= ( const CYIWeakPtr< YI_PTR_TYPE > &  rOther)

This function resets the internal object to the one provided as a parameter.

Parameters
rOtherthe pointer to share ownership of its object with.
Returns
Returns itself to allow daisy-chaining
template<typename YI_PTR_TYPE>
template<typename YI_OTHER_PTR_TYPE >
CYIWeakPtr<YI_PTR_TYPE>& CYIWeakPtr< YI_PTR_TYPE >::operator= ( const CYIWeakPtr< YI_OTHER_PTR_TYPE > &  rOther)

This function resets the internal object to the one provided as a parameter.

Parameters
rOtherthe pointer to share ownership of its object with.
Returns
Returns itself to allow daisy-chaining
template<typename YI_PTR_TYPE>
CYIWeakPtr<YI_PTR_TYPE>& CYIWeakPtr< YI_PTR_TYPE >::operator= ( const CYISharedPtr< YI_PTR_TYPE > &  rOther)

This function resets the internal object to the one provided as a parameter.

Parameters
rOtherthe pointer to share ownership of its object with.
Returns
Returns itself to allow daisy-chaining
template<typename YI_PTR_TYPE>
template<typename YI_OTHER_PTR_TYPE >
CYIWeakPtr<YI_PTR_TYPE>& CYIWeakPtr< YI_PTR_TYPE >::operator= ( const CYISharedPtr< YI_OTHER_PTR_TYPE > &  rOther)

This function resets the internal object to the one provided as a parameter.

Parameters
rOtherthe pointer to share ownership of its object with.
Returns
Returns itself to allow daisy-chaining
template<typename YI_PTR_TYPE>
template<typename YI_OTHER_PTR_TYPE >
CYIWeakPtr<YI_PTR_TYPE>& CYIWeakPtr< YI_PTR_TYPE >::operator= ( const std::shared_ptr< YI_OTHER_PTR_TYPE > &  rOther)

This function resets the internal object to the one provided as a parameter.

Parameters
rOtherthe pointer to share ownership of its object with.
Returns
Returns itself to allow daisy-chaining
template<typename YI_PTR_TYPE>
void CYIWeakPtr< YI_PTR_TYPE >::Reset ( )

This function resets its internal object to nullptr. It will never delete the object

See also
CYISharedPtr::Reset()
template<typename YI_PTR_TYPE>
void CYIWeakPtr< YI_PTR_TYPE >::Swap ( CYIWeakPtr< YI_PTR_TYPE > &  rPtr)

Swap function that will move the weak reference of a object to a different CYIWeakPtr that could be living in a completely different scope.

Example:

class MyClass
{
private:
public:
MyClass() : m_pData(new MyData())
{
}
void Swap(CYIWeakPtr<MyData> &pData)
{
m_pData.Swap(pData); // instance from the class swapped with instance from this function
}
};
Parameters
rPtrthe CYIScopedPtr to swap ownership with
template<typename YI_PTR_TYPE>
CYISharedPtr<YI_PTR_TYPE> CYIWeakPtr< YI_PTR_TYPE >::ToStrongPtr ( ) const

This function generates a strong reference to the object. A strong reference takes ownership and will delete the object if it is the last reference to go out-of-scope.

Example :

void Func(const CYIWeakPtr<MyData> &rData)
{
CYISharedPtr<MyData> strongPtr = rData.ToStrongPtr(); // grabs ownership
...
} // data could be deleted if strongPtr is the only and last reference to the internal object
Returns
Returns a strong reference to the object.
See also
CYIWeakPtr

Friends And Related Function Documentation

template<typename YI_PTR_TYPE>
template<typename YI_OTHER_PTR_TYPE >
friend class CYISharedPtr
friend
template<typename YI_PTR_TYPE>
template<typename YI_OTHER_PTR_TYPE >
friend class CYIWeakPtr
friend

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