Diligent Engine
 
Loading...
Searching...
No Matches
Diligent::RefCntAutoPtr< T > Class Template Reference

Template class that implements reference counting. More...

#include <RefCntAutoPtr.hpp>

Detailed Description

template<typename T>
class Diligent::RefCntAutoPtr< T >

Template class that implements reference counting.

The main advantage of RefCntAutoPtr over the std::shared_ptr is that you can attach the same raw pointer to different smart pointers.

For instance, the following code will crash since p will be released twice:

 auto *p = new char;
 std::shared_ptr<char> pTmp1(p);
 std::shared_ptr<char> pTmp2(p);
 ...

This code, in contrast, works perfectly fine:

ObjectBase *pRawPtr(new ObjectBase);
RefCntAutoPtr<ObjectBase> pSmartPtr1(pRawPtr);
RefCntAutoPtr<ObjectBase> pSmartPtr2(pRawPtr);
...

Other advantage is that weak pointers remain valid until the object is alive, even if all smart pointers were destroyed:

RefCntWeakPtr<ObjectBase> pWeakPtr(pSmartPtr1);
pSmartPtr1.Release();
auto pSmartPtr3 = pWeakPtr.Lock();
..

Weak pointers can also be attached directly to the object:

RefCntWeakPtr<ObjectBase> pWeakPtr(pRawPtr);