You.i Engine
CYISharedArray< YI_ARRAY_TYPE > Class Template Reference

Detailed Description

template<typename YI_ARRAY_TYPE>
class CYISharedArray< YI_ARRAY_TYPE >

The CYISharedArray is a smart pointer that takes shared ownership of a dynamically allocated array of objects, and destroys the array when going out of scope using the delete [] operator only when no other CYISharedArray are referencing the array.

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

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

Usage example:

#define MAX_DATA 3;
class MyClassA
{
private:
public:
MyClassA() : m_data(new float[MAX_DATA])
{
dataCopy[0] = 1.618f;
dataCopy[1] = 3.141f;
dataCopy[2] = -9.8f;
}
const CYISharedArray<float> &GetData() const
{
return m_data;
}
};
class MyClassB
{
private:
public:
MyClassB()
{
}
void SetData(const CYISharedArray<float> &rData)
{
m_data = rData;
if (m_data)
{
m_data[0] *= 2.0f
m_data[1] *= 2.0f
m_data[2] *= 2.0f
}
}
void Print()
{
if (m_data)
{
printf("%f %f %f\n", m_data[0], m_data[1], m_data[2]);
}
}
};
void main()
{
CYIScopedArray<MyClassB> pB(new MyClassB);
{
CYIScopedArray<MyClassA> pA(new MyClassA);
pB->SetData(pA->GetData()); // share the m_data between pB, while pB multiplies every values by 2.0
} // m_data is not deleted since it pB still has a reference for it
pB->Print(); // will print '3.236 6.282 -19.6'
}

Like the CYISharedPtr, 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
{
}
};
Remarks
CYISharedArray is thread-safe, with exceptions. See CYISharedPtr for details.
See also
CYISharedPtr
CYIScopedPtr
CYIScopedArray
CYIWeakPtr
CYIWeakArray

#include <smartptr/YiSharedArray.h>

Public Types

typedef CYISharedPtr< YI_ARRAY_TYPE > CYISharedArray< YI_ARRAY_TYPE >YI_UNSPECIFIED_BOOL_TYPE
 

Public Member Functions

 CYISharedArray ()
 
 CYISharedArray (YI_ARRAY_TYPE array[])
 
template<typename YI_FUNCTION_OBJECT >
 CYISharedArray (YI_ARRAY_TYPE array[], YI_FUNCTION_OBJECT functor)
 
 CYISharedArray (const CYISharedArray< YI_ARRAY_TYPE > &rOther)
 
template<typename YI_OTHER_ARRAY_TYPE >
 CYISharedArray (const CYISharedArray< YI_OTHER_ARRAY_TYPE > &rOther)
 
 CYISharedArray (const CYIWeakArray< YI_ARRAY_TYPE > &rOther)
 
template<typename YI_OTHER_ARRAY_TYPE >
 CYISharedArray (const CYIWeakArray< YI_OTHER_ARRAY_TYPE > &rOther)
 
void Reset ()
 
YI_ARRAY_TYPE & operator* () const
 
bool operator! () const
 
 operator YI_UNSPECIFIED_BOOL_TYPE () const
 
CYISharedArray< YI_ARRAY_TYPE > & operator= (const CYISharedArray< YI_ARRAY_TYPE > &rOther)
 
template<typename YI_OTHER_PTR_TYPE >
CYISharedArray< YI_ARRAY_TYPE > & operator= (const CYISharedArray< YI_OTHER_PTR_TYPE > &rOther)
 
CYISharedArray< YI_ARRAY_TYPE > & operator= (const CYIWeakArray< YI_ARRAY_TYPE > &rOther)
 
template<typename YI_OTHER_PTR_TYPE >
CYISharedArray< YI_ARRAY_TYPE > & operator= (const CYIWeakArray< YI_OTHER_PTR_TYPE > &rOther)
 
YI_ARRAY_TYPE * Get () const
 
bool IsNull () const
 
bool IsUnique () const
 
uint32_t GetUseCount () const
 
void Swap (CYISharedArray< YI_ARRAY_TYPE > &rPtr)
 
YI_ARRAY_TYPE & operator[] (int32_t nIndex)
 
const YI_ARRAY_TYPE & operator[] (int32_t nIndex) const
 
CYIWeakArray< YI_ARRAY_TYPE > ToWeakArray () const
 

Static Public Member Functions

static const CYIRuntimeTypeInfoGetClassTypeInfo ()
 

Friends

template<typename YI_OTHER_PTR_TYPE >
class CYIWeakArray
 
template<typename YI_OTHER_PTR_TYPE >
class CYISharedArray
 

Member Typedef Documentation

template<typename YI_ARRAY_TYPE>
typedef CYISharedPtr<YI_ARRAY_TYPE> CYISharedArray< YI_ARRAY_TYPE >::CYISharedArray< YI_ARRAY_TYPE >YI_UNSPECIFIED_BOOL_TYPE

Constructor & Destructor Documentation

template<typename YI_ARRAY_TYPE>
CYISharedArray< YI_ARRAY_TYPE >::CYISharedArray ( )
inline

Constructs CYISharedArray and sets its internal array to nullptr

template<typename YI_ARRAY_TYPE >
CYISharedArray< YI_ARRAY_TYPE >::CYISharedArray ( YI_ARRAY_TYPE  array[])
inline

Constructs CYISharedArray and sets its internal array to the argument

Parameters
arraythe array to take shared ownership
template<typename YI_ARRAY_TYPE>
template<typename YI_FUNCTION_OBJECT >
CYISharedArray< YI_ARRAY_TYPE >::CYISharedArray ( YI_ARRAY_TYPE  array[],
YI_FUNCTION_OBJECT  functor 
)
inline

Constructs CYISharedArray and sets its internal array to the provided parameter while using a user defined deletion functor (function object).

template<typename YI_ARRAY_TYPE>
CYISharedArray< YI_ARRAY_TYPE >::CYISharedArray ( const CYISharedArray< YI_ARRAY_TYPE > &  rOther)
inline

Copy-Constructor that constructs CYISharedArray and shares ownership of array instance from another CYISharedArray

Parameters
rOtherthe pointer to share an array ownership with
template<typename YI_ARRAY_TYPE>
template<typename YI_OTHER_ARRAY_TYPE >
CYISharedArray< YI_ARRAY_TYPE >::CYISharedArray ( const CYISharedArray< YI_OTHER_ARRAY_TYPE > &  rOther)
inline

Constructs CYISharedArray and shares ownership of array instance from another CYISharedArray of a different type

Warning
Make sure the types are compatible since the implementation is making a C-Style cast
Parameters
rOtherthe CYISharedArray to share an array ownership with
template<typename YI_ARRAY_TYPE>
CYISharedArray< YI_ARRAY_TYPE >::CYISharedArray ( const CYIWeakArray< YI_ARRAY_TYPE > &  rOther)
inline

Constructs CYISharedArray and shares ownership of array instance from a CYIWeakArray

Parameters
rOtherthe pointer to share an array ownership with
template<typename YI_ARRAY_TYPE>
template<typename YI_OTHER_ARRAY_TYPE >
CYISharedArray< YI_ARRAY_TYPE >::CYISharedArray ( const CYIWeakArray< YI_OTHER_ARRAY_TYPE > &  rOther)
inline

Constructs CYISharedArray and shares ownership of array instance from a CYIWeakArray of a different type.

Warning
Make sure the types are compatible since the implementation is making a C-Style cast
Parameters
rOtherthe CYISharedArray to share an array ownership with

Member Function Documentation

template<typename YI_ARRAY_TYPE>
YI_ARRAY_TYPE* CYISharedArray< YI_ARRAY_TYPE >::Get ( ) const
inline

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

Example:

void my_legacy_function(MyData *pPtr)
{
...
}
void Func()
{
CYISharedPtr<MyData> pData(new MyData);
my_legacy_function(pData.Get());
}
Returns
Returns the internal object
template<typename YI_ARRAY_TYPE>
static const CYIRuntimeTypeInfo& CYISharedArray< YI_ARRAY_TYPE >::GetClassTypeInfo ( )
inlinestatic

A function that returns the YiRTTI type of the type of this shared array. The values pointed-to by this shared array are ignored.

Warning
Calling this function on a shared array that has a non-YiRTTI type will not compile.
template<typename YI_ARRAY_TYPE>
uint32_t CYISharedArray< YI_ARRAY_TYPE >::GetUseCount ( ) const
inline

Returns the number of CYISharedPtr<T> objects that manage the object T.

template<typename YI_ARRAY_TYPE>
bool CYISharedArray< YI_ARRAY_TYPE >::IsNull ( ) const
inline

Syntactic sugar for the operator!()

Example:

void Func()
{
CYISharedPtr<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_ARRAY_TYPE>
bool CYISharedArray< YI_ARRAY_TYPE >::IsUnique ( ) const
inline

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.

Remarks
CYIWeakPtr does not affect the result of this function.
Returns
Returns true if this is the only reference owning the object, false otherwise.
template<typename YI_ARRAY_TYPE>
CYISharedArray< YI_ARRAY_TYPE >::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()
{
CYISharedArray<MyData> pData(new MyData[5]);
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_ARRAY_TYPE>
bool CYISharedArray< YI_ARRAY_TYPE >::operator! ( ) const
inline

This operator tests if the instance is null

Example:

void Func()
{
CYISharedPtr<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_ARRAY_TYPE>
YI_ARRAY_TYPE& CYISharedArray< YI_ARRAY_TYPE >::operator* ( ) const
inline

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

Example:

void Func()
{
CYISharedPtr<MyData> pData(new MyData());
(*pData).SetValue(42);
...
}
Returns
Returns dereferenced internal object
template<typename YI_ARRAY_TYPE>
CYISharedArray<YI_ARRAY_TYPE>& CYISharedArray< YI_ARRAY_TYPE >::operator= ( const CYISharedArray< YI_ARRAY_TYPE > &  rOther)
inline

This function resets the internal array to the one provided as a parameter.

If the previous instance was the last reference of its array, the previous array will be promptly deleted.

Parameters
rOtherthe pointer to share ownership of its array with.
Returns
Returns itself to allow daisy-chaining
template<typename YI_ARRAY_TYPE>
template<typename YI_OTHER_PTR_TYPE >
CYISharedArray<YI_ARRAY_TYPE>& CYISharedArray< YI_ARRAY_TYPE >::operator= ( const CYISharedArray< YI_OTHER_PTR_TYPE > &  rOther)
inline

This function resets the internal array to the one provided as a parameter.

If the previous instance was the last reference of its array, the previous array will be promptly deleted.

Parameters
rOtherthe pointer to share ownership of its array with.
Returns
Returns itself to allow daisy-chaining
template<typename YI_ARRAY_TYPE>
CYISharedArray<YI_ARRAY_TYPE>& CYISharedArray< YI_ARRAY_TYPE >::operator= ( const CYIWeakArray< YI_ARRAY_TYPE > &  rOther)
inline

This function resets the internal array to the one provided as a parameter.

If the previous instance was the last reference of its array, the previous array will be promptly deleted.

Parameters
rOtherthe pointer to share ownership of its array with.
Returns
Returns itself to allow daisy-chaining
template<typename YI_ARRAY_TYPE>
template<typename YI_OTHER_PTR_TYPE >
CYISharedArray<YI_ARRAY_TYPE>& CYISharedArray< YI_ARRAY_TYPE >::operator= ( const CYIWeakArray< YI_OTHER_PTR_TYPE > &  rOther)
inline

This function resets the internal array to the one provided as a parameter.

If the previous instance was the last reference of its array, the previous array will be promptly deleted.

Parameters
rOtherthe pointer to share ownership of its array with.
Returns
Returns itself to allow daisy-chaining
template<typename YI_ARRAY_TYPE>
YI_ARRAY_TYPE& CYISharedArray< YI_ARRAY_TYPE >::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>
const YI_ARRAY_TYPE& CYISharedArray< YI_ARRAY_TYPE >::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>
void CYISharedArray< YI_ARRAY_TYPE >::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_ARRAY_TYPE>
void CYISharedArray< YI_ARRAY_TYPE >::Swap ( CYISharedArray< YI_ARRAY_TYPE > &  rPtr)
inline

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

Example:

class MyClass
{
private:
public:
MyClass() : m_pData(new MyData())
{
}
{
m_pData.Swap(pData); // instance from the class swapped with instance from this function
}
};
Parameters
rPtrthe CYISharedPtr to swap ownership with
template<typename YI_ARRAY_TYPE>
CYIWeakArray<YI_ARRAY_TYPE> CYISharedArray< YI_ARRAY_TYPE >::ToWeakArray ( ) const
inline

This function generates a weak reference of the array. A weak reference does not take ownership and will never delete the array. It is the user responsibility to make sure that the weak array is still valid (not null) when using it.

Example :

void Func(const CYISharedArray<MyData> &rDataArray)
{
CYIWeakArray<MyData> weakArray = rDataArray.ToWeakArray();
}
Returns
Returns a weak reference of the array
See also
CYIWeakArray

Friends And Related Function Documentation

template<typename YI_ARRAY_TYPE>
template<typename YI_OTHER_PTR_TYPE >
friend class CYISharedArray
friend
template<typename YI_ARRAY_TYPE>
template<typename YI_OTHER_PTR_TYPE >
friend class CYIWeakArray
friend

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