PageRenderTime 7ms CodeModel.GetById 2ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llcommon/llrefcount.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 89 lines | 47 code | 11 blank | 31 comment | 2 complexity | 5c4b65905e4cd4940c33c2f8d1e423f8 MD5 | raw file
 1/** 
 2 * @file llrefcount.h
 3 * @brief Base class for reference counted objects for use with LLPointer
 4 *
 5 * $LicenseInfo:firstyear=2002&license=viewerlgpl$
 6 * Second Life Viewer Source Code
 7 * Copyright (C) 2010, Linden Research, Inc.
 8 * 
 9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation;
12 * version 2.1 of the License only.
13 * 
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * Lesser General Public License for more details.
18 * 
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 * 
23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
24 * $/LicenseInfo$
25 */
26#ifndef LLREFCOUNT_H
27#define LLREFCOUNT_H
28
29#include <boost/noncopyable.hpp>
30
31#define LL_REF_COUNT_DEBUG 0
32#if LL_REF_COUNT_DEBUG
33class LLMutex ;
34#endif
35
36//----------------------------------------------------------------------------
37// RefCount objects should generally only be accessed by way of LLPointer<>'s
38// see llthread.h for LLThreadSafeRefCount
39//----------------------------------------------------------------------------
40
41class LL_COMMON_API LLRefCount
42{
43protected:
44	LLRefCount(const LLRefCount& other);
45	LLRefCount& operator=(const LLRefCount&);
46	virtual ~LLRefCount(); // use unref()
47	
48public:
49	LLRefCount();
50
51#if LL_REF_COUNT_DEBUG
52	void ref() const ;
53	S32 unref() const ;
54#else
55	inline void ref() const
56	{ 
57		mRef++; 
58	} 
59
60	inline S32 unref() const
61	{
62		llassert(mRef >= 1);
63		if (0 == --mRef) 
64		{
65			delete this; 
66			return 0;
67		}
68		return mRef;
69	}	
70#endif
71
72	//NOTE: when passing around a const LLRefCount object, this can return different results
73	// at different types, since mRef is mutable
74	S32 getNumRefs() const
75	{
76		return mRef;
77	}
78
79private: 
80	mutable S32	mRef; 
81
82#if LL_REF_COUNT_DEBUG
83	LLMutex*  mMutexp ;
84	mutable U32  mLockedThreadID ;
85	mutable BOOL mCrashAtUnlock ; 
86#endif
87};
88
89#endif