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

Detailed Description

template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = typename std::unique_ptr<YI_PTR_TYPE>::deleter_type>
class CYIScopedPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >

The CYIScopedPtr is a smart pointer that takes sole ownership of a dynamically allocated object, and destroys the object when going out of scope.

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

It is a small light-weight utility class that simplifies the implementation of the resource acquisition is initialization (RAII) principle.

Consider the following example which does heap allocations, and have more than one exit points in which deallocation is necessary to avoid memory leaks:

void Func()
{
MyAdapter *pAdapter = new MyAdapter();
MyHandler *pHandler = new MyHandler();
if(!pAdapter->Init())
{
delete pAdapter;
delete pHandler;
return;
}
pHandler->Handle(pAdapter);
delete pAdapter;
delete pHandler;
}

Thanks to the CYIScopedPtr, the code can be simplified to the following:

void Func()
{
CYIScopedPtr<MyAdapter> pAdapter(new MyAdapter());
CYIScopedPtr<MyHandler> pHandler(new MyHandler());
if(!pAdapter->Init())
{
return;
}
pHandler->Handle(pAdapter);
}

CYIScopedPtr can also be used as a member variable in a class:

class MyClass
{
private:
MyData *m_pData;
public:
MyClass() : m_pData(new MyData())
{
}
virtual ~MyClass
{
if (m_pData)
{
delete m_pData
m_pData = nullptr;
}
}
};

Can be simplified to this, if your coding style allows you to allocate memory within constructor initialization:

class MyClass
{
private:
public:
MyClass() : m_pData(new MyData())
{
}
virtual ~MyClass
{
}
};

Please note that it is very unsafe to expose any CYIScopedPtr instances outside of its scope.

If you require such feature, you should consider using CYISharedPtr instead.

CYIScopedPtr also has an optional custom delete function object that can be overridden to fit your needs. Here is how to use this feature:

class MyCustomDeleteFunctor
{
public:
void operator()(MyService *pService)
{
if (pService->IsRunning())
{
pService->Stop();
}
delete pService;
}
};
class MyClass
{
private:
public:
MyClass() : m_pService(new MyService())
{
m_pService->Start();
}
virtual ~MyClass
{
}
};

You could also implement a custom delete function object for deleting array (using delete [] instead of regular delete), but it is already done for you if you use CYIScopedArray instead of CYIScopedPtr.

Warning
When using C++11, CYIScopedPtr objects can be move-constructed or move-assigned to other CYIScopedPtr instances. However, when moving a CYIScopedPtr to a less-derived type, the type of the target CYIScopedPtr must use a virtual destructor. Failure to do so can result in the wrong destructor being called (and memory being leaked.)
CYIScopedPtr is not thread-safe
See also
CYIScopedArray
CYISharedPtr
CYISharedArray
CYIWeakPtr
CYIWeakArray

#include <smartptr/YiScopedPtr.h>

Inheritance diagram for CYIScopedPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >:

Public Member Functions

 CYIScopedPtr (YI_PTR_TYPE *pPtr=nullptr)
 
 CYIScopedPtr (YI_PTR_TYPE *pPtr, const YI_DELETE_FUNCTOR &deleter)
 
 CYIScopedPtr (CYIScopedPtr &&other)
 
 CYIScopedPtr (std::unique_ptr< YI_PTR_TYPE, YI_DELETE_FUNCTOR > &&other)
 
 CYIScopedPtr (YI_PTR_TYPE *pPtr, YI_DELETE_FUNCTOR &&deleter)
 
template<typename YI_PTR_OTHER_TYPE , typename YI_OTHER_DELETER >
 CYIScopedPtr (CYIScopedPtr< YI_PTR_OTHER_TYPE, YI_OTHER_DELETER > &&other)
 
 ~CYIScopedPtr ()
 
void operator= (YI_PTR_TYPE *pPtr)
 
CYIScopedPtroperator= (CYIScopedPtr &&other)
 
CYIScopedPtroperator= (std::unique_ptr< YI_PTR_TYPE, YI_DELETE_FUNCTOR > &&other)
 
template<typename YI_PTR_OTHER_TYPE , typename YI_OTHER_DELETER >
CYIScopedPtroperator= (CYIScopedPtr< YI_PTR_OTHER_TYPE, YI_OTHER_DELETER > &&other)
 
YI_PTR_TYPE & operator* () const
 
YI_PTR_TYPE * operator-> () const
 
bool operator! () const
 
 operator bool () const
 
template<typename YI_PTR_OTHER_TYPE , typename YI_OTHER_DELETER >
 operator std::unique_ptr< YI_PTR_OTHER_TYPE, YI_OTHER_DELETER > ()
 
void Reset (YI_PTR_TYPE *pPtr=nullptr)
 
YI_PTR_TYPE * Get () const
 
YI_PTR_TYPE * Take ()
 
bool IsNull () const
 
void Swap (CYIScopedPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR > &rPtr)
 

Friends

template<typename YI_OTHER_PTR_TYPE , typename YI_OTHER_DELETER >
class CYIScopedPtr
 

Constructor & Destructor Documentation

template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = typename std::unique_ptr<YI_PTR_TYPE>::deleter_type>
CYIScopedPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::CYIScopedPtr ( YI_PTR_TYPE *  pPtr = nullptr)
inlineexplicit

Constructs CYIScopedPtr and sets its internal pointer to pPtr

Parameters
pPtrthe pointer to take ownership of
template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = typename std::unique_ptr<YI_PTR_TYPE>::deleter_type>
CYIScopedPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::CYIScopedPtr ( YI_PTR_TYPE *  pPtr,
const YI_DELETE_FUNCTOR &  deleter 
)
inline

Constructs CYIScopedPtr and sets its internal pointer to pPtr

Parameters
pPtrthe pointer to take ownership of
deleterthe functor to use to delete pPtr
template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = typename std::unique_ptr<YI_PTR_TYPE>::deleter_type>
CYIScopedPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::CYIScopedPtr ( CYIScopedPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR > &&  other)
inline

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

Parameters
otherthe CYIScopedPtr to move (it will point to null after this constructor completes)
template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = typename std::unique_ptr<YI_PTR_TYPE>::deleter_type>
CYIScopedPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::CYIScopedPtr ( std::unique_ptr< YI_PTR_TYPE, YI_DELETE_FUNCTOR > &&  other)
inline

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

Parameters
otherthe CYIScopedPtr to move (it will point to null after this constructor completes)
template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = typename std::unique_ptr<YI_PTR_TYPE>::deleter_type>
CYIScopedPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::CYIScopedPtr ( YI_PTR_TYPE *  pPtr,
YI_DELETE_FUNCTOR &&  deleter 
)
inline

Constructs CYIScopedPtr and sets its internal pointer to pPtr

Parameters
pPtrthe pointer to take ownership of
deleterthe deleter to use to delete pPtr
template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = typename std::unique_ptr<YI_PTR_TYPE>::deleter_type>
template<typename YI_PTR_OTHER_TYPE , typename YI_OTHER_DELETER >
CYIScopedPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::CYIScopedPtr ( CYIScopedPtr< YI_PTR_OTHER_TYPE, YI_OTHER_DELETER > &&  other)
inline

Constructs CYIScopedPtr 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 CYIScopedPtr.

Parameters
otherthe CYIScopedPtr to move (it will point to null after this constructor completes)
template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = typename std::unique_ptr<YI_PTR_TYPE>::deleter_type>
CYIScopedPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::~CYIScopedPtr ( )
inline

Destructor will delete pPtr using the default or provided delete function object

Member Function Documentation

template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = typename std::unique_ptr<YI_PTR_TYPE>::deleter_type>
YI_PTR_TYPE* CYIScopedPtr< YI_PTR_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_PTR_TYPE, typename YI_DELETE_FUNCTOR = typename std::unique_ptr<YI_PTR_TYPE>::deleter_type>
bool CYIScopedPtr< YI_PTR_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_PTR_TYPE, typename YI_DELETE_FUNCTOR = typename std::unique_ptr<YI_PTR_TYPE>::deleter_type>
CYIScopedPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::operator bool ( ) const
inline

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

void Func()
{
CYIScopedPtr<MyData> pData(new MyData);
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_PTR_TYPE, typename YI_DELETE_FUNCTOR = typename std::unique_ptr<YI_PTR_TYPE>::deleter_type>
template<typename YI_PTR_OTHER_TYPE , typename YI_OTHER_DELETER >
CYIScopedPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::operator std::unique_ptr< YI_PTR_OTHER_TYPE, YI_OTHER_DELETER > ( )
inline
template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = typename std::unique_ptr<YI_PTR_TYPE>::deleter_type>
bool CYIScopedPtr< YI_PTR_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_PTR_TYPE, typename YI_DELETE_FUNCTOR = typename std::unique_ptr<YI_PTR_TYPE>::deleter_type>
YI_PTR_TYPE& CYIScopedPtr< YI_PTR_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_PTR_TYPE, typename YI_DELETE_FUNCTOR = typename std::unique_ptr<YI_PTR_TYPE>::deleter_type>
YI_PTR_TYPE* CYIScopedPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::operator-> ( ) const
inline

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

Example:

void Func()
{
CYIScopedPtr<MyData> pData(new MyData());
pData->SetValue(42);
...
}
\return Returns the internal pointer
template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = typename std::unique_ptr<YI_PTR_TYPE>::deleter_type>
void CYIScopedPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::operator= ( YI_PTR_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_PTR_TYPE, typename YI_DELETE_FUNCTOR = typename std::unique_ptr<YI_PTR_TYPE>::deleter_type>
CYIScopedPtr& CYIScopedPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::operator= ( CYIScopedPtr< YI_PTR_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_PTR_TYPE, typename YI_DELETE_FUNCTOR = typename std::unique_ptr<YI_PTR_TYPE>::deleter_type>
CYIScopedPtr& CYIScopedPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::operator= ( std::unique_ptr< YI_PTR_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_PTR_TYPE, typename YI_DELETE_FUNCTOR = typename std::unique_ptr<YI_PTR_TYPE>::deleter_type>
template<typename YI_PTR_OTHER_TYPE , typename YI_OTHER_DELETER >
CYIScopedPtr& CYIScopedPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::operator= ( CYIScopedPtr< 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_PTR_TYPE, typename YI_DELETE_FUNCTOR = typename std::unique_ptr<YI_PTR_TYPE>::deleter_type>
void CYIScopedPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::Reset ( YI_PTR_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_PTR_TYPE, typename YI_DELETE_FUNCTOR = typename std::unique_ptr<YI_PTR_TYPE>::deleter_type>
void CYIScopedPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR >::Swap ( CYIScopedPtr< YI_PTR_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_PTR_TYPE, typename YI_DELETE_FUNCTOR = typename std::unique_ptr<YI_PTR_TYPE>::deleter_type>
YI_PTR_TYPE* CYIScopedPtr< YI_PTR_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
{
...
}
}

Friends And Related Function Documentation

template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = typename std::unique_ptr<YI_PTR_TYPE>::deleter_type>
template<typename YI_OTHER_PTR_TYPE , typename YI_OTHER_DELETER >
friend class CYIScopedPtr
friend

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