The CYICowPtr is a smart pointer that performs a shallow-copy when duplicated, and a deep-copy only when a copy is altered.
COW stands for 'Copy-on-write'. Copy-on-write is an optimization strategy that prevents unnecessary allocation and will only perform an allocation on a copy that has been altered, or explicitly requested to be detached.
Constant access to the pointer are deemed to be reading the pointer and will never perform a deep-copy, while non-constant access to the pointer will always assume that the pointer value is going to be changed, and a deep-copy will be performed. Therefore, const-correctness is essential for CYICowPtr to work properly
Here is essentially how to properly use the CYICowPtr:
#include <smartptr/YiCowPtr.h>
Public Types | |
| typedef YI_PTR_TYPE * | CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >YI_UNSPECIFIED_BOOL_TYPE |
Public Member Functions | |
| CYICowPtr () | |
| CYICowPtr (YI_PTR_TYPE *pData) | |
| CYICowPtr (const CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR > &rOther) | |
| template<typename YI_OTHER_PTR_TYPE , typename YI_OTHER_DELETE_FUNCTOR > | |
| CYICowPtr (const CYICowPtr< YI_OTHER_PTR_TYPE, YI_OTHER_DELETE_FUNCTOR > &rOther) | |
| ~CYICowPtr () | |
| void | Detach () |
| void | Reset () |
| const YI_PTR_TYPE & | operator* () const |
| YI_PTR_TYPE & | operator* () |
| const YI_PTR_TYPE * | operator-> () const |
| YI_PTR_TYPE * | operator-> () |
| bool | operator! () const |
| operator YI_UNSPECIFIED_BOOL_TYPE () const | |
| CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR > & | operator= (const CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR > &rOther) |
| template<typename YI_OTHER_PTR_TYPE , typename YI_OTHER_DELETE_FUNCTOR > | |
| CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR > & | operator= (const CYICowPtr< YI_OTHER_PTR_TYPE, YI_OTHER_DELETE_FUNCTOR > &rOther) |
| const YI_PTR_TYPE * | Get () const |
| bool | IsNull () const |
| int32_t | GetRefCount () const |
| void | Swap (CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR > &rPtr) |
Protected Member Functions | |
| void | Ref () |
| void | Deref () |
Protected Attributes | |
| YI_DELETE_FUNCTOR | DeleteFunc |
| CYIReferenceCounter * | m_pRefCounter |
| YI_PTR_TYPE * | m_pData |
Friends | |
| template<typename YI_OTHER_PTR_TYPE , typename YI_OTHER_DELETE_FUNCTOR > | |
| class | CYICowPtr |
| typedef YI_PTR_TYPE* CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >YI_UNSPECIFIED_BOOL_TYPE |
|
inline |
Constructs CYICowPtr and sets its internal object to nullptr
|
inlineexplicit |
Constructs CYICowPtr and sets its internal object to the provided parameter
| pData | the data to track reference count |
|
inline |
|
inline |
Destructor will only delete the internal pointer if and only if this instance is the last CYICowPtr to have a reference to our object
|
inlineprotected |
|
inline |
This function requests a deep-copy. It will only perform a deep copy if and only if the internal pointer is valid and also if and only if the reference count is greater than one.
|
inline |
Accesses the internal object without taking ownership. Mostly for legacy code compatibility.
Example:
|
inline |
Gets the reference count. If the object is nullptr, this function will return 0
|
inline |
Syntactic sugar for the operator!()
Example:
|
inline |
This operator tests if the instance is not null, and will work even when compiler does not support the 'bool' keyword Example:
|
inline |
This operator tests if the instance is null
Example:
|
inline |
Dereferences the internal object. If the internal object is null, the behavior is undefined.
Example:
|
inline |
Dereferences the internal object. If the internal object is null, the behavior is undefined.
Example:
|
inline |
Directly accesses the internal object's members. If the internal object is null, the behavior is undefined.
Example:
|
inline |
Directly accesses the internal object's members. If the internal object is null, the behavior is undefined.
Example:
|
inline |
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 track reference count |
|
inline |
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 track reference count |
|
inlineprotected |
|
inline |
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.
|
inline |
Swap function that will move the ownership of a object to a different CYICowPtr that could be living in a completely different scope.
Example:
| rPtr | the CYIScopedPtr to swap ownership with |
|
friend |
|
protected |
|
protected |
|
protected |