You.i Engine
CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR > Class Template Reference

Detailed Description

template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedDeleteFunctor<YI_PTR_TYPE>>
class CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >

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:

struct MyClassData
{
int32_t m_nData1;
float m_fData2;
}
class MyClass
{
private:
public:
MyClass() : m_pData(new MyClassData())
{
}
int32_t GetData1() const // constant function, so the data will not be detached
{
return m_pData->m_nData1;
}
float GetData2() const // constant function, so the data will not be detached
{
return m_pData->m_fData2;
}
void SetData1(int32_t nData) // non-constant function, so the data will be detached
{
m_pData->m_nData1 = nData;
}
void SetData2(float fData) // non-constant function, so the data will be detached
{
m_pData->m_fData2 = fData;
}
};
void main()
{
MyClass m1;
MyClass m2 = m1; // shallow copy of m_pData is made
int32_t nData = m1.GetData1();
float fData = m2.GetData2();
m2.SetData2(3.0f); //Detach() is called, copy-on-write is performed. m1 and m2 are now 2 distinct copies
}
Remarks
CYICowPtr is thread-safe

#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
 
CYIReferenceCounterm_pRefCounter
 
YI_PTR_TYPE * m_pData
 

Friends

template<typename YI_OTHER_PTR_TYPE , typename YI_OTHER_DELETE_FUNCTOR >
class CYICowPtr
 

Member Typedef Documentation

template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedDeleteFunctor<YI_PTR_TYPE>>
typedef YI_PTR_TYPE* CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >YI_UNSPECIFIED_BOOL_TYPE

Constructor & Destructor Documentation

template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedDeleteFunctor<YI_PTR_TYPE>>
CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::CYICowPtr ( )
inline

Constructs CYICowPtr and sets its internal object to nullptr

template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedDeleteFunctor<YI_PTR_TYPE>>
CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::CYICowPtr ( YI_PTR_TYPE *  pData)
inlineexplicit

Constructs CYICowPtr and sets its internal object to the provided parameter

Parameters
pDatathe data to track reference count
template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedDeleteFunctor<YI_PTR_TYPE>>
CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::CYICowPtr ( const CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR > &  rOther)
inline

Copy-constructor that constructs CYICowPtr and sets its internal object from another CYICowPtr of the same type

Parameters
rOtherthe pointer to track reference count
template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedDeleteFunctor<YI_PTR_TYPE>>
template<typename YI_OTHER_PTR_TYPE , typename YI_OTHER_DELETE_FUNCTOR >
CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::CYICowPtr ( const CYICowPtr< YI_OTHER_PTR_TYPE, YI_OTHER_DELETE_FUNCTOR > &  rOther)
inline

Constructs CYICowPtr and sets its internal object from another CYICowPtr of a different type

Warning
Make sure the types are compatible since the implementation is making a C-Style cast
Parameters
rOtherthe pointer track reference count
template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedDeleteFunctor<YI_PTR_TYPE>>
CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::~CYICowPtr ( )
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

Member Function Documentation

template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedDeleteFunctor<YI_PTR_TYPE>>
void CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::Deref ( )
inlineprotected
template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedDeleteFunctor<YI_PTR_TYPE>>
void CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::Detach ( )
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.

template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedDeleteFunctor<YI_PTR_TYPE>>
const YI_PTR_TYPE* CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::Get ( ) const
inline

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

Example:

void my_legacy_function(MyData *pPtr)
{
...
}
void Func()
{
CYICowPtr<MyData> pData(new MyData);
my_legacy_function(pData.Get());
}
Returns
Returns the internal object
template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedDeleteFunctor<YI_PTR_TYPE>>
int32_t CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::GetRefCount ( ) const
inline

Gets the reference count. If the object is nullptr, this function will return 0

template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedDeleteFunctor<YI_PTR_TYPE>>
bool CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::IsNull ( ) const
inline

Syntactic sugar for the operator!()

Example:

void Func()
{
CYICowPtr<MyData> pData(new MyData);
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.
template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedDeleteFunctor<YI_PTR_TYPE>>
CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::operator YI_UNSPECIFIED_BOOL_TYPE ( ) const
inline

This operator tests if the instance is not null, and will work even when compiler does not support the 'bool' keyword Example:

void Func()
{
CYICowPtr<MyData> pData(new MyData);
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 not null, false otherwise.
template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedDeleteFunctor<YI_PTR_TYPE>>
bool CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::operator! ( ) const
inline

This operator tests if the instance is null

Example:

void Func()
{
CYICowPtr<MyData> pData(new MyData);
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.
template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedDeleteFunctor<YI_PTR_TYPE>>
const YI_PTR_TYPE& CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::operator* ( ) const
inline

Dereferences the internal object. If the internal object is null, the behavior is undefined.

Example:

void Func()
{
CYICowPtr<MyData> pData(new MyData());
(*pData).SetValue(42);
...
}
Returns
Returns dereferenced internal object
Remarks
This function does not call Detach()
template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedDeleteFunctor<YI_PTR_TYPE>>
YI_PTR_TYPE& CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::operator* ( )
inline

Dereferences the internal object. If the internal object is null, the behavior is undefined.

Example:

void Func()
{
CYICowPtr<MyData> pData(new MyData());
(*pData).SetValue(42);
...
}
Returns
Returns dereferenced internal object
Remarks
This function calls Detach()
template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedDeleteFunctor<YI_PTR_TYPE>>
const YI_PTR_TYPE* CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::operator-> ( ) const
inline

Directly accesses the internal object's members. If the internal object is null, the behavior is undefined.

Example:

void Func()
{
CYICowPtr<MyData> pData(new MyData());
pData->SetValue(42);
...
}
Returns
Returns the internal object
Remarks
This function does not call Detach()
template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedDeleteFunctor<YI_PTR_TYPE>>
YI_PTR_TYPE* CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::operator-> ( )
inline

Directly accesses the internal object's members. If the internal object is null, the behavior is undefined.

Example:

void Func()
{
CYICowPtr<MyData> pData(new MyData());
pData->SetValue(42);
...
}
Returns
Returns the internal object
Remarks
This function calls Detach()
template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedDeleteFunctor<YI_PTR_TYPE>>
CYICowPtr<YI_PTR_TYPE, YI_DELETE_FUNCTOR>& CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::operator= ( const CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR > &  rOther)
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.

Parameters
rOtherthe pointer track reference count
Returns
Returns itself to allow daisy-chaining
template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedDeleteFunctor<YI_PTR_TYPE>>
template<typename YI_OTHER_PTR_TYPE , typename YI_OTHER_DELETE_FUNCTOR >
CYICowPtr<YI_PTR_TYPE, YI_DELETE_FUNCTOR>& CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::operator= ( const CYICowPtr< YI_OTHER_PTR_TYPE, YI_OTHER_DELETE_FUNCTOR > &  rOther)
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.

Parameters
rOtherthe pointer track reference count
Returns
Returns itself to allow daisy-chaining
template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedDeleteFunctor<YI_PTR_TYPE>>
void CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::Ref ( )
inlineprotected
template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedDeleteFunctor<YI_PTR_TYPE>>
void CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::Reset ( )
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.

template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedDeleteFunctor<YI_PTR_TYPE>>
void CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::Swap ( CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR > &  rPtr)
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:

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

Friends And Related Function Documentation

template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedDeleteFunctor<YI_PTR_TYPE>>
template<typename YI_OTHER_PTR_TYPE , typename YI_OTHER_DELETE_FUNCTOR >
friend class CYICowPtr
friend

Member Data Documentation

template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedDeleteFunctor<YI_PTR_TYPE>>
YI_DELETE_FUNCTOR CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::DeleteFunc
protected
template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedDeleteFunctor<YI_PTR_TYPE>>
YI_PTR_TYPE* CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::m_pData
protected
template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedDeleteFunctor<YI_PTR_TYPE>>
CYIReferenceCounter* CYICowPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::m_pRefCounter
protected

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