📄 sharedptr.h
字号:
/* $Id: sharedptr.h,v 1.4 2004/01/02 15:42:43 mbn Exp $
**
** ClanLib Game SDK
** Copyright (C) 2003 The ClanLib Team
** For a total list of contributers see the file CREDITS.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this library; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
**
*/
//! clanCore="System"
//! header=core.h
#ifndef clanlib_sharedptr_header
#define clanlib_sharedptr_header
//: Shared pointer class (automatically deletes data when all CL_SharedPtrs to data are gone).
//- !group=Core/System!
//- !header=core.h!
//- <p> Use CL_SharedPtr when you have data that's intended to be shared between
//- multiple users without each user needing an individual copy. </p>
//- <p> Note that CL_SharedPtr uses a very simple reference counting system, so is susecptible
//- to circular loop issues. </p>
template <typename T, typename U = T> class CL_SharedPtr
{
//! Construction:
public:
//: Constructs a CL_SharedPtr.
//param t: A pointer which was the return value of a 'new' call.
//- <p> After the CL_SharedPtr has been constructed based on a passed pointer,
//- the CL_SharedPtr takes full control over that data. The original pointer shouldn't
//- be used to access or delete the data anymore; instead, use the CL_SharedPtr. </p>
CL_SharedPtr() : ptr(0) {}
CL_SharedPtr(const CL_SharedPtr<T, U>& other) : ptr(other.ptr), pRefCnt(other.pRefCnt) {increment();}
CL_SharedPtr(T* t) : ptr(t), pRefCnt(new unsigned int(1)) {}
~CL_SharedPtr() {decrement();}
//! Attributes:
public:
//: Returns true if this CL_SharedPtr is not dereferencable.
bool is_null() const {if (ptr == 0) return true; return false;}
//: Returns number of references (including this one) to the data cache.
//- <p> Returns 0 if this pointer is null. </p>
int ref_cnt() const {if (ptr == 0) return 0; return *pRefCnt;}
//: Gives access to the pointer itself.
//- <p> Be careful not to keep the returned pointer around after doing any
//- non-const operations on the CL_LazyCopyPtr; it could be invalid
//- after that.</p>
U* get() {return (U*)ptr;}
const U* get() const {return (const U*)ptr;}
//: Pointer equality check operator.
//- <p> This will return true if the CL_SharedPtrs point to the same data. It doesn't
//- check the data itself for equality. </p>
bool operator==(const T* other) const {return other == ptr;}
bool operator==(const CL_SharedPtr<T, U>& other) const {return other.ptr == ptr;}
//! Operations:
public:
//: Copy assignment operator.
//param t: A pointer which was the return value of a 'new' call.
//- <p> Once the assignment statement is finished when assigning a passed pointer,
//- the CL_SharedPtr takes full control over that data. The original pointer shouldn't
//- be used to access or delete the data anymore; instead, use the CL_SharedPtr. </p>
CL_SharedPtr<T, U>& operator=(const CL_SharedPtr<T, U>& other)
{
if (&other != this)
{
decrement();
ptr = other.ptr;
pRefCnt = other.pRefCnt;
increment();
}
return *this;
}
CL_SharedPtr<T, U>& operator=(T* t)
{
decrement();
ptr = t;
pRefCnt = new unsigned int(1);
return *this;
}
//: Dereferencing operator.
U& operator*() {return *((U*)ptr);}
U const& operator*() const {return *((const U*)ptr);}
//: Indirect member access operator.
U* operator->() {return (U*)ptr;}
U const* operator->() const {return (const U*)ptr;}
//! Implementation:
private:
//Pointer to new-allocated data, or else 0
T* ptr;
//Pointer to new-allocated reference count (invalid to deref if ptr == 0)
unsigned int* pRefCnt;
//Increases the ref count
void increment()
{
if (ptr != 0)
++(*pRefCnt);
}
//Decreases the ref count, deletes entry and sets ptr to 0 if refcnt reaches zero
void decrement()
{
if (ptr != 0)
{
if (--(*pRefCnt) == 0)
{
delete ptr;
delete pRefCnt;
ptr = 0;
}
}
}
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -