2008-03-20 14:00:31 +01:00
|
|
|
/*
|
|
|
|
* The contents of this file are subject to the Initial
|
|
|
|
* Developer's Public License Version 1.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed AS IS,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing rights
|
|
|
|
* and limitations under the License.
|
|
|
|
*
|
|
|
|
* The Original Code was created by Vlad Horsun
|
|
|
|
* for the Firebird Open Source RDBMS project.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008 Vlad Horsun <hvlad@users.sf.net>
|
|
|
|
* and all contributors signed below.
|
|
|
|
*
|
|
|
|
* All Rights Reserved.
|
|
|
|
* Contributor(s): ______________________________________.
|
|
|
|
*
|
|
|
|
* Dmitry Yemanov <dimitr@users.sf.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef COMMON_REF_COUNTED_H
|
|
|
|
#define COMMON_REF_COUNTED_H
|
|
|
|
|
|
|
|
#include "../common/classes/fb_atomic.h"
|
2008-03-24 11:36:11 +01:00
|
|
|
#include "../jrd/gdsassert.h"
|
2008-03-20 14:00:31 +01:00
|
|
|
|
|
|
|
namespace Firebird
|
|
|
|
{
|
|
|
|
class RefCounted
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual int addRef()
|
|
|
|
{
|
|
|
|
return ++m_refCnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int release()
|
|
|
|
{
|
2008-03-25 09:44:11 +01:00
|
|
|
fb_assert(m_refCnt.value() > 0);
|
2008-03-20 14:00:31 +01:00
|
|
|
const int refCnt = --m_refCnt;
|
|
|
|
if (!refCnt)
|
|
|
|
delete this;
|
|
|
|
return refCnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
RefCounted() : m_refCnt(0) {}
|
2008-03-24 11:36:11 +01:00
|
|
|
|
2008-03-24 16:18:26 +01:00
|
|
|
virtual ~RefCounted()
|
2008-03-24 11:36:11 +01:00
|
|
|
{
|
2008-03-25 09:44:11 +01:00
|
|
|
fb_assert(m_refCnt.value() == 0);
|
2008-03-24 11:36:11 +01:00
|
|
|
}
|
2008-03-20 14:00:31 +01:00
|
|
|
|
|
|
|
private:
|
2008-03-21 03:42:24 +01:00
|
|
|
AtomicCounter m_refCnt;
|
2008-03-20 14:00:31 +01:00
|
|
|
};
|
|
|
|
|
2008-03-25 09:44:11 +01:00
|
|
|
// reference counted object guard
|
|
|
|
class Reference
|
|
|
|
{
|
|
|
|
public:
|
2008-03-28 14:25:47 +01:00
|
|
|
explicit Reference(RefCounted& refCounted) :
|
2008-03-25 09:44:11 +01:00
|
|
|
r(&refCounted)
|
|
|
|
{
|
|
|
|
r->addRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
~Reference()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
r->release();
|
|
|
|
}
|
|
|
|
catch (const Exception&)
|
|
|
|
{
|
|
|
|
DtorException::devHalt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefCounted* r;
|
|
|
|
};
|
|
|
|
|
2008-03-27 11:15:05 +01:00
|
|
|
// controls reference counter of the object where points
|
|
|
|
template <typename T>
|
|
|
|
class RefPtr
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RefPtr() : ptr(0)
|
|
|
|
{ }
|
|
|
|
|
2008-03-28 14:25:47 +01:00
|
|
|
explicit RefPtr(T* p) : ptr(p)
|
2008-03-27 11:15:05 +01:00
|
|
|
{
|
|
|
|
if (ptr)
|
|
|
|
{
|
|
|
|
ptr->addRef();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~RefPtr()
|
|
|
|
{
|
|
|
|
if (ptr)
|
|
|
|
{
|
|
|
|
ptr->release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T* operator=(T* p)
|
|
|
|
{
|
|
|
|
if (ptr != p)
|
|
|
|
{
|
|
|
|
if (ptr)
|
|
|
|
{
|
|
|
|
ptr->release();
|
|
|
|
}
|
2008-03-30 22:43:36 +02:00
|
|
|
|
2008-03-27 11:15:05 +01:00
|
|
|
ptr = p;
|
2008-03-30 22:43:36 +02:00
|
|
|
|
2008-03-27 11:15:05 +01:00
|
|
|
if (ptr)
|
|
|
|
{
|
|
|
|
ptr->addRef();
|
|
|
|
}
|
|
|
|
}
|
2008-03-30 22:43:36 +02:00
|
|
|
|
2008-03-27 11:15:05 +01:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2008-03-28 14:25:47 +01:00
|
|
|
operator T*()
|
2008-03-27 11:15:05 +01:00
|
|
|
{
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2008-03-28 14:25:47 +01:00
|
|
|
T* operator->()
|
2008-03-27 11:15:05 +01:00
|
|
|
{
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T* ptr;
|
|
|
|
};
|
2008-03-21 03:42:24 +01:00
|
|
|
} // namespace
|
2008-03-20 14:00:31 +01:00
|
|
|
|
|
|
|
#endif // COMMON_REF_COUNTED_H
|