You.i Engine
CYIWeakArray< YI_ARRAY_TYPE > Class Template Reference

Detailed Description

template<typename YI_ARRAY_TYPE>
class CYIWeakArray< YI_ARRAY_TYPE >

The CYIWeakArray is a smart pointer that does not take ownership of a dynamically allocated array of objects and will never delete the object. Instead, the CYIWeakArray will automatically point to nullptr should the array be deleted by the last CYISharedArray going out-of-scope.

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 CYIWeakPtr while providing the operator[] for your convenience.

Usage example:

#define MAX_DATA 3;
class MyClassA
{
private:
CYISharedArray<float> m_data; // strong reference
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:
CYIWeakArray<float> m_data; // weak reference
public:
MyClassB()
{
}
void SetData(const CYISharedArray<float> &rData)
{
if (rData)
{
m_data = rData.ToWeakArray();
}
}
void Print()
{
CYISharedArray<float> pStrongArray = m_data.ToStrongArray(); // lock the pointer with a strong reference.
if (pStrongArray) // check if it is null
{
// pointer is now safe to use.
printf("%f %f %f\n", pStrongArray[0], pStrongArray[1], pStrongArray[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 deleted since pA had the only reference to it.
pB->Print(); // won't print anything since the weak pointer now points to nullptr
}
Remarks
CYIWeakArray is thread-safe
See also
CYISharedArray
CYISharedPtr
CYIWeakPtr
CYIScopedArray
CYIScopedPtr

#include <smartptr/YiSharedArray.h>

Public Types

typedef CYIWeakPtr< YI_ARRAY_TYPE > CYIWeakArray< YI_ARRAY_TYPE >YI_UNSPECIFIED_BOOL_TYPE
 

Public Member Functions

 CYIWeakArray ()
 
 CYIWeakArray (const CYIWeakArray< YI_ARRAY_TYPE > &rOther)
 
template<typename YI_OTHER_ARRAY_TYPE >
 CYIWeakArray (const CYIWeakArray< YI_OTHER_ARRAY_TYPE > &rOther)
 
 CYIWeakArray (const CYISharedArray< YI_ARRAY_TYPE > &rOther)
 
template<typename YI_OTHER_ARRAY_TYPE >
 CYIWeakArray (const CYISharedArray< YI_OTHER_ARRAY_TYPE > &rOther)
 
void Reset ()
 
YI_ARRAY_TYPE * Get () const
 
bool IsNull () const
 
uint32_t GetUseCount () const
 
void Swap (CYIWeakArray< YI_ARRAY_TYPE > &rPtr)
 
bool operator! () const
 
 operator YI_UNSPECIFIED_BOOL_TYPE () const
 
CYIWeakArray< YI_ARRAY_TYPE > & operator= (const CYIWeakArray< YI_ARRAY_TYPE > &rOther)
 
template<typename YI_OTHER_PTR_TYPE >
CYIWeakArray< YI_ARRAY_TYPE > & operator= (const CYIWeakArray< YI_OTHER_PTR_TYPE > &rOther)
 
CYIWeakArray< YI_ARRAY_TYPE > & operator= (const CYISharedArray< YI_ARRAY_TYPE > &rOther)
 
template<typename YI_OTHER_PTR_TYPE >
CYIWeakArray< YI_ARRAY_TYPE > & operator= (const CYISharedArray< YI_OTHER_PTR_TYPE > &rOther)
 
CYISharedArray< YI_ARRAY_TYPE > ToStrongArray () const
 

Friends

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

Member Typedef Documentation

template<typename YI_ARRAY_TYPE>
typedef CYIWeakPtr<YI_ARRAY_TYPE> CYIWeakArray< YI_ARRAY_TYPE >::CYIWeakArray< YI_ARRAY_TYPE >YI_UNSPECIFIED_BOOL_TYPE

Constructor & Destructor Documentation

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

Constructs CYIWeakArray and sets its internal array to nullptr

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

Copy-Constructor that constructs CYIWeakArray and obtains array instance from another CYIWeakArray

Parameters
rOtherthe pointer to array
template<typename YI_ARRAY_TYPE>
template<typename YI_OTHER_ARRAY_TYPE >
CYIWeakArray< YI_ARRAY_TYPE >::CYIWeakArray ( const CYIWeakArray< YI_OTHER_ARRAY_TYPE > &  rOther)
inline

Copy-Constructor that constructs CYIWeakArray and obtains the array instance from another CYIWeakArray of a different type

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

Constructs CYIWeakArray and obtains the array instance from another CYISharedArray of the same type

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

Constructs CYIWeakArray and obtains the array instance from a 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

Member Function Documentation

template<typename YI_ARRAY_TYPE>
YI_ARRAY_TYPE* CYIWeakArray< 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(CYIWeakPtr<MyData> &pData)
{
my_legacy_function(pData.Get());
}
Returns
Returns the internal object
Warning
The use of this function should be avoided since the pointer returned is non-reference-counted. Here is the intended way to access the pointer:
void Func(CYIWeakPtr<MyData> &pData)
{
// first, protect yourself by locking the instance
CYISharedPtr<MyData> pPtr(pData); // could have used pData.ToStrongPtr()
if (!pPtr.IsNull()) // make sure it didn't turn null on us
{
//now the pointer is safe to use
pPtr->m_myString = "Safe to store";
}
}
template<typename YI_ARRAY_TYPE>
uint32_t CYIWeakArray< YI_ARRAY_TYPE >::GetUseCount ( ) const
inline

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

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

Syntactic sugar for the operator!()

Example:

void Func(CYISharedPtr<MyData> pStrongPtr)
{
CYIWeakPtr<MyData> pData(pStrongPtr);
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.
See also
operator!()
template<typename YI_ARRAY_TYPE>
CYIWeakArray< YI_ARRAY_TYPE >::operator YI_UNSPECIFIED_BOOL_TYPE ( ) const
inline

This function tests if the internal array is not set to nullptr.

Example:

void Func(CYISharedArray<MyData> pStrongArray)
{
CYIWeakArray<MyData> pData(pStrongArray);
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 null, false otherwise.
See also
operator!()
template<typename YI_ARRAY_TYPE>
bool CYIWeakArray< YI_ARRAY_TYPE >::operator! ( ) const
inline

This function tests if the internal object is set to nullptr.

Example:

void Func(CYISharedPtr<MyData> pStrongPtr)
{
CYIWeakPtr<MyData> pData(pStrongPtr);
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.
See also
operator!()
template<typename YI_ARRAY_TYPE>
CYIWeakArray<YI_ARRAY_TYPE>& CYIWeakArray< YI_ARRAY_TYPE >::operator= ( const CYIWeakArray< YI_ARRAY_TYPE > &  rOther)
inline

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

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 >
CYIWeakArray<YI_ARRAY_TYPE>& CYIWeakArray< 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.

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

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

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 >
CYIWeakArray<YI_ARRAY_TYPE>& CYIWeakArray< 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.

Parameters
rOtherthe pointer to share ownership of its array with.
Returns
Returns itself to allow daisy-chaining
template<typename YI_ARRAY_TYPE>
void CYIWeakArray< YI_ARRAY_TYPE >::Reset ( )
inline

This function resets its internal object to nullptr. It will never delete the object

See also
CYISharedPtr::Reset()
template<typename YI_ARRAY_TYPE>
void CYIWeakArray< YI_ARRAY_TYPE >::Swap ( CYIWeakArray< YI_ARRAY_TYPE > &  rPtr)
inline

Swap function that will move the weak reference of a object to a different CYIWeakPtr that could be living in a completely different scope.

Example:

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

This function generates a strong reference of the array. A strong reference takes a shared ownership of the array and will delete the array when the last reference goes out-of-scope.

Returns
Returns a weak reference of the array
See also
CYISharedArray
ToSharedPtr()

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 files: