You.i Engine
CYIScopedArray< YI_ARRAY_TYPE, YI_DELETE_FUNCTOR > Class Template Reference

Detailed Description

template<typename YI_ARRAY_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedArrayDeleteFunctor<YI_ARRAY_TYPE>>
class CYIScopedArray< YI_ARRAY_TYPE, YI_DELETE_FUNCTOR >

The CYIScopedArray is a smart pointer that takes sole ownership of a dynamically allocated array of objects, and destroys the array when going out of scope using the delete [] operator.

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

Very similar to the CYIScopedPtr while deleting its internal pointer with the delete[] operator. It also provides the operator[] for your convenience.

Usage example:

void Func()
{
CYIScopedArray<float> fData(new float[3]);
fData[0] = 1.618f;
fData[1] = 3.141f;
fData[2] = -9.8f;
}

Like the CYIScopedPtr, the delete function object can be overridden

#define MAX_THREAD 100
class MyCustomDeleteFunctor
{
public:
void operator()(MyThread services[])
{
for (int32_t i = 0; i < MAX_THREAD; ++i)
{
if (services[i]->IsRunning())
{
services[i]->Stop();
}
}
delete [] services;
}
};
class MyClass
{
private:
public:
MyClass() : m_services(new MyThread[MAX_THREAD])
{
for (int32_t i = 0; i < MAX_THREAD; ++i)
{
services[i]->Start();
}
}
virtual ~MyClass
{
}
};
Warning
CYIScopedArray is not thread-safe
See also
CYIScopedPtr
CYISharedPtr
CYISharedArray
CYIWeakPtr
CYIWeakArray

#include <smartptr/YiScopedArray.h>

Public Member Functions

 CYIScopedArray (YI_ARRAY_TYPE array[]=nullptr)
 
 CYIScopedArray (YI_ARRAY_TYPE array[], const YI_DELETE_FUNCTOR &deleter)
 
 CYIScopedArray (CYIScopedArray &&other)
 
 CYIScopedArray (YI_ARRAY_TYPE array[], YI_DELETE_FUNCTOR &&deleter)
 
template<typename YI_ARRAY_OTHER_TYPE , typename YI_OTHER_DELETER >
 CYIScopedArray (CYIScopedArray< YI_ARRAY_OTHER_TYPE, YI_OTHER_DELETER > &&other)
 
void operator= (YI_ARRAY_TYPE pPtr[])
 
CYIScopedArrayoperator= (CYIScopedArray &&other)
 
template<typename YI_PTR_OTHER_TYPE , typename YI_OTHER_DELETER >
CYIScopedArrayoperator= (CYIScopedArray< YI_PTR_OTHER_TYPE, YI_OTHER_DELETER > &&other)
 
YI_ARRAY_TYPE & operator* () const
 
bool operator! () const
 
 operator bool () const
 
void Reset (YI_ARRAY_TYPE pPtr[]=nullptr)
 
YI_ARRAY_TYPE * Get () const
 
YI_ARRAY_TYPE * Take ()
 
bool IsNull () const
 
void Swap (CYIScopedArray< YI_ARRAY_TYPE, YI_DELETE_FUNCTOR > &rPtr)
 
YI_ARRAY_TYPE & operator[] (int32_t nIndex)
 
const YI_ARRAY_TYPE & operator[] (int32_t nIndex) const
 

Constructor & Destructor Documentation

template<typename YI_ARRAY_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedArrayDeleteFunctor<YI_ARRAY_TYPE>>
CYIScopedArray< YI_ARRAY_TYPE, YI_DELETE_FUNCTOR >::CYIScopedArray ( YI_ARRAY_TYPE  array[] = nullptr)
inlineexplicit

Constructs CYIScopedArray and sets its internal array to the argument

Parameters
arraythe pointer to take ownership of
template<typename YI_ARRAY_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedArrayDeleteFunctor<YI_ARRAY_TYPE>>
CYIScopedArray< YI_ARRAY_TYPE, YI_DELETE_FUNCTOR >::CYIScopedArray ( YI_ARRAY_TYPE  array[],
const YI_DELETE_FUNCTOR &  deleter 
)
inline

Constructs CYIScopedArray and sets its internal pointer to pPtr

Parameters
arraythe pointer to take ownership of
deleterthe functor to use to delete array
template<typename YI_ARRAY_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedArrayDeleteFunctor<YI_ARRAY_TYPE>>
CYIScopedArray< YI_ARRAY_TYPE, YI_DELETE_FUNCTOR >::CYIScopedArray ( CYIScopedArray< YI_ARRAY_TYPE, YI_DELETE_FUNCTOR > &&  other)
inline

Constructs CYIScopedArray and sets its internal pointer to other's pointer

Parameters
otherthe CYIScopedArray to move (it will point to null after this constructor completes)
template<typename YI_ARRAY_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedArrayDeleteFunctor<YI_ARRAY_TYPE>>
CYIScopedArray< YI_ARRAY_TYPE, YI_DELETE_FUNCTOR >::CYIScopedArray ( YI_ARRAY_TYPE  array[],
YI_DELETE_FUNCTOR &&  deleter 
)
inline

Constructs CYIScopedArray and sets its internal pointer to array

Parameters
arraythe pointer to take ownership of
deleterthe deleter to use to delete array
template<typename YI_ARRAY_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedArrayDeleteFunctor<YI_ARRAY_TYPE>>
template<typename YI_ARRAY_OTHER_TYPE , typename YI_OTHER_DELETER >
CYIScopedArray< YI_ARRAY_TYPE, YI_DELETE_FUNCTOR >::CYIScopedArray ( CYIScopedArray< YI_ARRAY_OTHER_TYPE, YI_OTHER_DELETER > &&  other)
inline

Constructs CYIScopedArray and sets its internal pointer to other's pointer. The pointer type and deleter type of other must be convertible to the types of this CYIScopedArray.

Parameters
otherthe CYIScopedArray to move (it will point to null after this constructor completes)

Member Function Documentation

template<typename YI_ARRAY_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedArrayDeleteFunctor<YI_ARRAY_TYPE>>
YI_ARRAY_TYPE* CYIScopedArray< YI_ARRAY_TYPE, YI_DELETE_FUNCTOR >::Get ( ) const
inline

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

Example:

void my_legacy_function(MyData *pPtr)
{
...
}
void Func()
{
CYIScopedPtr<MyData> pData(new MyData);
my_legacy_function(pData.Get());
}
Returns
Returns the internal pointer
template<typename YI_ARRAY_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedArrayDeleteFunctor<YI_ARRAY_TYPE>>
bool CYIScopedArray< YI_ARRAY_TYPE, YI_DELETE_FUNCTOR >::IsNull ( ) const
inline

Syntactic sugar for the operator!()

Example:

void Func()
{
CYIScopedPtr<MyData> pData(new MyData);
if (pData.IsNull()) // will be false
{
}
...
pData.Reset();
if (pData.IsNull()) // will be true since the internal pointer is now null.
{
}
}
Returns
Returns true if the internal pointer is null, false otherwise.
template<typename YI_ARRAY_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedArrayDeleteFunctor<YI_ARRAY_TYPE>>
CYIScopedArray< YI_ARRAY_TYPE, YI_DELETE_FUNCTOR >::operator bool ( ) const
inline

This operator tests if the instance is not null. Example:

void Func()
{
CYIScopedArray<MyData> pData(new MyData[5]);
if (pData) // will be true
{
}
...
pData.Reset();
if (pData) // will be false since the internal pointer is now null.
{
}
}
Returns
Returns true if the internal pointer is not null, false otherwise.
template<typename YI_ARRAY_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedArrayDeleteFunctor<YI_ARRAY_TYPE>>
bool CYIScopedArray< YI_ARRAY_TYPE, YI_DELETE_FUNCTOR >::operator! ( ) const
inline

This operator tests if the instance is null

Example:

void Func()
{
CYIScopedPtr<MyData> pData(new MyData);
if (!pData) // will be false
{
}
...
pData.Reset();
if (!pData) // will be true since the internal pointer is now null.
{
}
}
Returns
Returns true if the internal pointer is null, false otherwise.
template<typename YI_ARRAY_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedArrayDeleteFunctor<YI_ARRAY_TYPE>>
YI_ARRAY_TYPE& CYIScopedArray< YI_ARRAY_TYPE, YI_DELETE_FUNCTOR >::operator* ( ) const
inline

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

Example:

void Func()
{
CYIScopedPtr<MyData> pData(new MyData());
(*pData).SetValue(42);
...
}
Returns
Returns dereferenced internal pointer
template<typename YI_ARRAY_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedArrayDeleteFunctor<YI_ARRAY_TYPE>>
void CYIScopedArray< YI_ARRAY_TYPE, YI_DELETE_FUNCTOR >::operator= ( YI_ARRAY_TYPE  pPtr[])
inline

This function deletes the currently assigned pointer using the default or provided delete function object, and then take ownership of the new pointer.

Can also be used to prematurely delete the internal pointer by assigning to nullptr.

Example:

void Func()
{
CYIScopedPtr<MyData> pData(new MyData()); // first instance
...
pData = new MyData(); // first instance has been delete and has been replaced by a second instance.
if (pData.IsNull()) // will be false
{
...
}
...
pData = nullptr; // second instance now deleted.
if (pData.IsNull()) // will be true
{
...
}
}
Parameters
pPtrthe new object to take ownership. pPtr will be deleted in CYIScopedPtr destructor using the default or provided delete function object.
template<typename YI_ARRAY_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedArrayDeleteFunctor<YI_ARRAY_TYPE>>
CYIScopedArray& CYIScopedArray< YI_ARRAY_TYPE, YI_DELETE_FUNCTOR >::operator= ( CYIScopedArray< YI_ARRAY_TYPE, YI_DELETE_FUNCTOR > &&  other)
inline

This function deletes the currently assigned pointer using the default or provided delete function object, and then moves the pointer of other into this scoped pointer instance.

Parameters
otherthe CYIScopedPtr to move (it will point to null after this function call completes)
template<typename YI_ARRAY_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedArrayDeleteFunctor<YI_ARRAY_TYPE>>
template<typename YI_PTR_OTHER_TYPE , typename YI_OTHER_DELETER >
CYIScopedArray& CYIScopedArray< YI_ARRAY_TYPE, YI_DELETE_FUNCTOR >::operator= ( CYIScopedArray< YI_PTR_OTHER_TYPE, YI_OTHER_DELETER > &&  other)
inline

This function deletes the currently assigned pointer using the default or provided delete function object, and then moves the pointer of other into this scoped pointer instance. The pointer type and deleter type of other must be convertible to the types of this CYIScopedPtr.

Parameters
otherthe CYIScopedPtr to move (it will point to null after this function call completes)
template<typename YI_ARRAY_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedArrayDeleteFunctor<YI_ARRAY_TYPE>>
YI_ARRAY_TYPE& CYIScopedArray< YI_ARRAY_TYPE, YI_DELETE_FUNCTOR >::operator[] ( int32_t  nIndex)
inline

Non-constant array element accessor/mutator for non-constant function

Example:

#define DATA_MAX 10;
void MyClass
{
private:
public:
MyClass() : m_dataArray(new MyData[DATA_MAX])
{
}
void SetValue(int32_t nIndex, float nValue) //non-constant function
{
m_dataArray[nIndex] = nValue;
}
};
Parameters
nIndexthe position of the array element to retrieve
Returns
Returns the value at the desired position
template<typename YI_ARRAY_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedArrayDeleteFunctor<YI_ARRAY_TYPE>>
const YI_ARRAY_TYPE& CYIScopedArray< YI_ARRAY_TYPE, YI_DELETE_FUNCTOR >::operator[] ( int32_t  nIndex) const
inline

Constant array element accessor for constant functions

Example:

#define DATA_MAX 10;
void MyClass
{
private:
public:
MyClass() : m_dataArray(new MyData[DATA_MAX])
{
}
void GetValue(int32_t nIndex) const //constant function
{
return m_dataArray[nIndex];
}
};
Parameters
nIndexthe position of the array element to retrieve
Returns
Returns the value at the desired position
template<typename YI_ARRAY_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedArrayDeleteFunctor<YI_ARRAY_TYPE>>
void CYIScopedArray< YI_ARRAY_TYPE, YI_DELETE_FUNCTOR >::Reset ( YI_ARRAY_TYPE  pPtr[] = nullptr)
inline

Syntactic sugar for operator=(),

This function deletes the currently assigned pointer using the default or provided delete function object, and then take ownership of the new pointer.

Can also be used to prematurely delete the internal pointer by reseting to nullptr.

Example:

void Func()
{
CYIScopedPtr<MyData> pData(new MyData()); // first instance
...
pData.Reset(new MyData()); // first instance has been delete and has been replaced by a second instance.
if (pData.IsNull()) // will be false
{
...
}
...
pData.Reset(); // second instance now deleted.
if (pData.IsNull()) // will be true
{
...
}
}
Parameters
pPtrthe new object to take ownership. pPtr will be deleted in CYIScopedPtr destructor using the default or provided delete function object.
template<typename YI_ARRAY_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedArrayDeleteFunctor<YI_ARRAY_TYPE>>
void CYIScopedArray< YI_ARRAY_TYPE, YI_DELETE_FUNCTOR >::Swap ( CYIScopedArray< YI_ARRAY_TYPE, YI_DELETE_FUNCTOR > &  rPtr)
inline

Swap function that will move the ownership of a pointer to a different CYIScopedPtr that could be living in a completely different scope.

Example:

class MyClass
{
private:
public:
MyClass() : m_pData(new MyData())
{
}
void SetValue(int32_t nValue)
{
CYIScopedPtr<MyData> pData(new MyData());
pData->SetValue(nValue);
if (pData->IsValid())
{
m_pData.Swap(pData); // instance from the class swapped with instance from this function
}
} // the instance allocated within this function prevails while the previous instance from the class has been destroyed
};
Parameters
rPtrthe CYIScopedPtr to swap ownership with
template<typename YI_ARRAY_TYPE, typename YI_DELETE_FUNCTOR = CYICheckedArrayDeleteFunctor<YI_ARRAY_TYPE>>
YI_ARRAY_TYPE* CYIScopedArray< YI_ARRAY_TYPE, YI_DELETE_FUNCTOR >::Take ( )
inline

Accesses the internal pointer while taking ownership. Mostly for legacy code compatibility. The destructor of CYIScopedPtr WILL NO LONGER delete the pointer since it has lost ownership of the object. The internal pointer of the object will be set to null. Example:

void my_legacy_function(MyData *pPtr)
{
...
delete pPtr;
}
void Func()
{
CYIScopedPtr<MyData> pData(new MyData);
my_legacy_function(pData.Take());
if (pData) // will be false
{
...
}
}

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