使用Loki心得

在使用SmartPtr時, 若不改變 StoragePolicy , 使用原本的預設 RefCounted
在DLL下使用, 會造成Count的值會提前移除, 原因是因為SmallObject
我不知道怎麼改, 所以我的作法就是改成我的 QSyncerRefCounted
其實我只是把RefCounted重寫一下並移除SmallObject 就好了

typedef Loki::SmartPtr CommonDataPt;

template
class QSyncerRefCounted
{
public:
QSyncerRefCounted()
: pCount_(new int)
{
assert(pCount_!=0);
*pCount_ = 1;
}

QSyncerRefCounted(const QSyncerRefCounted& rhs)
: pCount_(rhs.pCount_)
{}

// MWCW lacks template friends, hence the following kludge
template
QSyncerRefCounted(const QSyncerRefCounted& rhs)
: pCount_(reinterpret_cast(rhs).pCount_)
{}

P Clone(const P& val)
{
++*pCount_;
return val;
}

bool Release(const P&)
{
if (!--*pCount_)
{
delete pCount_;
pCount_ = NULL;
return true;
}
return false;
}

void Swap(QSyncerRefCounted& rhs)
{ std::swap(pCount_, rhs.pCount_); }

enum { destructiveCopy = false };

private:
// Data
int* pCount_;
};

沒有留言: