The CYISharedPtr is a smart pointer that takes a shared ownership of a dynamically allocated object. The last instance of CYISharedPtr going out-of-scope will clean up the memory thus avoiding memory leaks.
CYISharedPtr is a very powerful smart pointer for cases where the it isn't clear on who should be cleaning up the memory. It is not as fast as the CYIScopedPointer due to its thread-safety characteristic, but will be very useful in certain cases. As guidance, if this particular instance is never copied and never leaves your class, you should consider using a CYIScopedPtr for performance gain instead.
In the following example ownership is not necessarily clear:
The problem can easily be solved using the CYISharedPtr.
Here is how it can be done:
CYISharedPtr also has an optional custom delete function object that can be overridden to fit your needs. Here is how to use this feature:
#include <smartptr/YiSharedPtr.h>

Public Member Functions | |
| CYISharedPtr () | |
| CYISharedPtr (YI_PTR_TYPE *pData) | |
| CYISharedPtr (YI_PTR_TYPE *pData, typename CYICallbackDeleter< YI_PTR_TYPE >::YI_CALLBACK pCallback) | |
| template<typename YI_FUNCTION_OBJECT > | |
| CYISharedPtr (YI_PTR_TYPE *pData, YI_FUNCTION_OBJECT functor) | |
| template<typename YI_OTHER_TYPE > | |
| CYISharedPtr (const std::shared_ptr< YI_OTHER_TYPE > &pShared) | |
| template<typename YI_OTHER_PTR_TYPE > | |
| CYISharedPtr (const CYISharedPtr< YI_OTHER_PTR_TYPE > &rOther) | |
| CYISharedPtr (const CYIWeakPtr< YI_PTR_TYPE > &rOther)=delete | |
| template<typename YI_OTHER_PTR_TYPE > | |
| CYISharedPtr (const CYIWeakPtr< YI_OTHER_PTR_TYPE > &rOther)=delete | |
| ~CYISharedPtr () | |
| void | Reset () |
| YI_PTR_TYPE & | operator* () const |
| YI_PTR_TYPE * | operator-> () const |
| bool | operator! () const |
| operator bool () const | |
| template<typename YI_OTHER_PTR_TYPE > | |
| operator std::shared_ptr< YI_OTHER_PTR_TYPE > () const | |
| template<typename YI_OTHER_PTR_TYPE > | |
| CYISharedPtr< YI_PTR_TYPE > & | operator= (const std::shared_ptr< YI_OTHER_PTR_TYPE > &rOther) |
| template<typename YI_OTHER_PTR_TYPE > | |
| CYISharedPtr< YI_PTR_TYPE > & | operator= (const CYISharedPtr< YI_OTHER_PTR_TYPE > &rOther) |
| CYISharedPtr< YI_PTR_TYPE > & | operator= (const CYIWeakPtr< YI_PTR_TYPE > &rOther)=delete |
| template<typename YI_OTHER_PTR_TYPE > | |
| CYISharedPtr< YI_PTR_TYPE > & | operator= (const CYIWeakPtr< YI_OTHER_PTR_TYPE > &rOther)=delete |
| YI_PTR_TYPE * | Get () const |
| bool | IsNull () const |
| bool | IsUnique () const |
| uint32_t | GetUseCount () const |
| void | Swap (CYISharedPtr< YI_PTR_TYPE > &rPtr) |
| CYIWeakPtr< YI_PTR_TYPE > | ToWeakPtr () const |
| template<typename YI_OTHER_PTR_TYPE > | |
| CYISharedPtr< YI_OTHER_PTR_TYPE > | StaticCast () const |
| template<typename YI_OTHER_PTR_TYPE > | |
| CYISharedPtr< YI_OTHER_PTR_TYPE > | DynamicCast () const |
| const CYIRuntimeTypeInfo * | GetRuntimeTypeInfo () const |
Static Public Member Functions | |
| static const CYIRuntimeTypeInfo & | GetClassTypeInfo () |
Friends | |
| template<typename YI_OTHER_PTR_TYPE > | |
| class | CYIWeakPtr |
| template<typename YI_OTHER_PTR_TYPE > | |
| class | CYISharedPtr |
| template<typename YI_OTHER_PTR_TYPE > | |
| class | CYIEnableSharedFromThis |
| CYISharedPtr< YI_PTR_TYPE >::CYISharedPtr | ( | ) |
Constructs CYISharedPtr and sets its internal object to nullptr
|
explicit |
Constructs CYISharedPtr and sets its internal object to the provided parameter
| pData | the data to take shared ownership |
|
explicit |
Constructs CYISharedPtr and sets its internal object to the provided parameter while using a user defined deletion callback.
|
explicit |
Constructs CYISharedPtr and sets its internal object to the provided parameter while using a user defined deletion functor (function object).
| CYISharedPtr< YI_PTR_TYPE >::CYISharedPtr | ( | const std::shared_ptr< YI_OTHER_TYPE > & | pShared | ) |
Constructs CYISharedPtr and sets its internal object to pShared. The ownership is shared with pShared.
| CYISharedPtr< YI_PTR_TYPE >::CYISharedPtr | ( | const CYISharedPtr< YI_OTHER_PTR_TYPE > & | rOther | ) |
Constructs CYISharedPtr and sets its internal object from another CYISharedPtr of a different type
| rOther | the pointer to take shared ownership of its object |
|
delete |
Constructs CYISharedPtr and sets its internal object from another CYIWeakPtr of the same type
| rOther | the pointer to take shared ownership of its object |
|
delete |
Constructs CYISharedPtr and sets its internal object from another CYIWeakPtr of a different type
| rOther | the pointer to take shared ownership of its object |
| CYISharedPtr< YI_PTR_TYPE >::~CYISharedPtr | ( | ) |
Destructor will only delete m_pData if this instance is the last CYISharedPtr to have a reference to our object
| CYISharedPtr<YI_OTHER_PTR_TYPE> CYISharedPtr< YI_PTR_TYPE >::DynamicCast | ( | ) | const |
Performs a dynamic cast that shares the reference count. This function can be used for up-casting/down-casting from a base/derived class. If the desired type is incompatible with this CYISharedPtr, it will return a NULL CYISharedPtr.
Code Example :
| YI_PTR_TYPE* CYISharedPtr< YI_PTR_TYPE >::Get | ( | ) | const |
Accesses the internal object without taking ownership. Mostly for legacy code compatibility.
Example:
|
static |
A function that returns the YiRTTI type of the type of this shared pointer. The value pointed-to by this shared pointer is ignored.
| const CYIRuntimeTypeInfo* CYISharedPtr< YI_PTR_TYPE >::GetRuntimeTypeInfo | ( | ) | const |
Convenience function that returns the YiRTTI type of the object. Returns a null pointer if this shared pointer is null.
Instead of calling this:
you can call this instead:
| uint32_t CYISharedPtr< YI_PTR_TYPE >::GetUseCount | ( | ) | const |
Returns the number of CYISharedPtr<T> objects that manage the object T.
| bool CYISharedPtr< YI_PTR_TYPE >::IsNull | ( | ) | const |
Syntactic sugar for the operator!()
Example:
| bool CYISharedPtr< YI_PTR_TYPE >::IsUnique | ( | ) | const |
Helper function to determine if this particular instance is the last reference
If this function returns true, it also means that the object will be deleted if this instance goes out-of-scope.
| CYISharedPtr< YI_PTR_TYPE >::operator bool | ( | ) | const |
This operator tests if the instance is not null.
Example:
| CYISharedPtr< YI_PTR_TYPE >::operator std::shared_ptr< YI_OTHER_PTR_TYPE > | ( | ) | const |
| bool CYISharedPtr< YI_PTR_TYPE >::operator! | ( | ) | const |
This operator tests if the instance is null
Example:
| YI_PTR_TYPE& CYISharedPtr< YI_PTR_TYPE >::operator* | ( | ) | const |
Dereferences the internal object. If the internal object is null, the behavior is undefined.
Example:
| YI_PTR_TYPE* CYISharedPtr< YI_PTR_TYPE >::operator-> | ( | ) | const |
Directly accesses the internal object's members. If the internal object is null, the behavior is undefined.
Example:
| CYISharedPtr<YI_PTR_TYPE>& CYISharedPtr< 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.
If the previous instance was the last reference of its object, the previous object will be promptly deleted.
| rOther | the pointer to share ownership of its object with. |
| CYISharedPtr<YI_PTR_TYPE>& CYISharedPtr< YI_PTR_TYPE >::operator= | ( | const CYISharedPtr< YI_OTHER_PTR_TYPE > & | rOther | ) |
This function resets the internal object to the one provided as a parameter.
If the previous instance was the last reference of its object, the previous object will be promptly deleted.
| rOther | the pointer to share ownership of its object with. |
|
delete |
This function resets the internal object to the one provided as a parameter.
If the previous instance was the last reference of its object, the previous object will be promptly deleted.
| rOther | the pointer to share ownership of its object with. |
|
delete |
This function resets the internal object to the one provided as a parameter.
If the previous instance was the last reference of its object, the previous object will be promptly deleted.
| rOther | the pointer to share ownership of its object with. |
| void CYISharedPtr< YI_PTR_TYPE >::Reset | ( | ) |
This function resets the internal object to nullptr.
If the previous instance was the last reference of its object, the previous object will be promptly deleted.
| CYISharedPtr<YI_OTHER_PTR_TYPE> CYISharedPtr< YI_PTR_TYPE >::StaticCast | ( | ) | const |
Performs a static cast that shares the reference count. This function can be use for up-casting/down-casting from a base/derived class.
Code Example :
| void CYISharedPtr< YI_PTR_TYPE >::Swap | ( | CYISharedPtr< YI_PTR_TYPE > & | rPtr | ) |
Swap function that will move the ownership of a object to a different CYISharedPtr that could be living in a completely different scope.
Example:
| rPtr | the CYISharedPtr to swap ownership with |
| CYIWeakPtr<YI_PTR_TYPE> CYISharedPtr< YI_PTR_TYPE >::ToWeakPtr | ( | ) | const |
This function generates a weak reference to the object. A weak reference does not take ownership and will never delete the object. It is the user responsibility to make sure that the weak pointer is still valid (not null) when using it.
Example :
|
friend |
|
friend |