You.i Engine
CYIEnableSharedFromThis< YI_PTR_TYPE > Class Template Reference

Detailed Description

template<class YI_PTR_TYPE>
class CYIEnableSharedFromThis< YI_PTR_TYPE >

CYIEnableSharedFromThis<T> allows an object currently managed by a CYISharedPtr to safely generation additional CYISharedPtr internally.

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

Consider the following example, which is going to result in a crash since your class instance may be managed by another mean, such as another shared pointer.

class MyClass
{
CYISharedPtr<MyClass> GetManagedInstance()
{
return CYISharedPtr<MyClass> (this); // VERY BAD!
}
}
MyClass pClass = new MyClass();
CYISharedPtr<MyClass> pSharedClass1(pClass); // pClass is now managed
CYISharedPtr<MyClass> pSharedClass2 = pClass->GetManagedInstance(); // DANGER: pClass is managed twice, separate reference count
...
pSharedClass1.Reset(); // reference count is decreased by one, and pClass is now deleted.
pSharedClass2.Reset(); // CRASH: Double deletion.

This is where the CYIEnableSharedFromThis<T> comes into play. Inheriting from CYIEnableSharedFromThis<T> will provide convenient 'SharedFromThis()' function that will return a CYIWeakPtr<T> from the first CYISharedPtr<T> instance created. If the class is unmanaged by any CYISharedPtr<T>, 'SharedFromThis()' will return a null smart pointer.

class MyClass : CYIEnableSharedFromThis<MyClass>
{
CYISharedPtr<MyClass> GetManagedInstance()
{
SharedFromThis(); // Safe. Will return null if MyClass is unmanaged
}
}
MyClass pClass = new MyClass();
CYISharedPtr<MyClass> pSharedClass1(pClass); // pClass is now managed
CYISharedPtr<MyClass> pSharedClass2 = pClass->GetManagedInstance(); // reference count from pSharedClass1 is now increased by one
...
pSharedClass1.Reset(); // reference count is decreased by one.
pSharedClass2.Reset(); // reference count is decreased by one, and pClass is now deleted.
Warning
Calling 'SharedFromThis()' on an unmanaged pointer will always return a null smart pointer.
Calling 'SharedFromThis()' from the Constructor will always return a null smart pointer.

#include <smartptr/YiEnableSharedFromThis.h>

Inheritance diagram for CYIEnableSharedFromThis< YI_PTR_TYPE >:

Protected Member Functions

CYISharedPtr< YI_PTR_TYPE > SharedFromThis ()
 Returns a CYISharedPtr<T> which shares ownership of *this. More...
 
CYISharedPtr< YI_PTR_TYPE > SharedFromThis () const
 Returns a CYISharedPtr<T> which shares ownership of *this. More...
 
 CYIEnableSharedFromThis ()
 
 CYIEnableSharedFromThis (const CYIEnableSharedFromThis &other)
 
 ~CYIEnableSharedFromThis ()
 
CYIEnableSharedFromThisoperator= (const CYIEnableSharedFromThis &other)
 

Constructor & Destructor Documentation

template<class YI_PTR_TYPE >
CYIEnableSharedFromThis< YI_PTR_TYPE >::CYIEnableSharedFromThis ( )
protected
template<class YI_PTR_TYPE >
CYIEnableSharedFromThis< YI_PTR_TYPE >::CYIEnableSharedFromThis ( const CYIEnableSharedFromThis< YI_PTR_TYPE > &  other)
protected
template<class YI_PTR_TYPE >
CYIEnableSharedFromThis< YI_PTR_TYPE >::~CYIEnableSharedFromThis ( )
protected

Member Function Documentation

template<class YI_PTR_TYPE >
CYIEnableSharedFromThis& CYIEnableSharedFromThis< YI_PTR_TYPE >::operator= ( const CYIEnableSharedFromThis< YI_PTR_TYPE > &  other)
protected
template<class YI_PTR_TYPE >
CYISharedPtr<YI_PTR_TYPE> CYIEnableSharedFromThis< YI_PTR_TYPE >::SharedFromThis ( )
protected

Returns a CYISharedPtr<T> which shares ownership of *this.

Warning
Will return null if the pointer is unmanaged.
template<class YI_PTR_TYPE >
CYISharedPtr<YI_PTR_TYPE> CYIEnableSharedFromThis< YI_PTR_TYPE >::SharedFromThis ( ) const
protected

Returns a CYISharedPtr<T> which shares ownership of *this.

Warning
Will return null if the pointer is unmanaged.

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