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()
{
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
|
| | 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) |
| |
| CYIScopedPtr & | operator= (CYIScopedPtr &&other) |
| |
| CYIScopedPtr & | operator= (std::unique_ptr< YI_PTR_TYPE, YI_DELETE_FUNCTOR > &&other) |
| |
| template<typename YI_PTR_OTHER_TYPE , typename YI_OTHER_DELETER > |
| CYIScopedPtr & | operator= (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) |
| |