]>
git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/refcount.h
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 Based on code donated by Perry Kiehtreiber
22 #ifndef _SECURITY_REFCOUNT_H_
23 #define _SECURITY_REFCOUNT_H_
25 #include <Security/threading.h>
31 // RefCount/RefPointer - a simple reference counting facility.
33 // To make an object reference-counted, derive it from RefCount. To track refcounted
34 // objects, use RefPointer<TheType>, where TheType must be derived from RefCount.
36 // RefCount is thread safe - any number of threads can hold and manipulate references
37 // in parallel. It does however NOT protect the contents of your object - just the
38 // reference count itself. If you need to share your object contents, you must engage
39 // in appropriate locking yourself.
41 // There is no (thread safe) way to determine whether you are the only thread holding
42 // a pointer to a particular RefCount object.
47 // Base class for reference counted objects
51 RefCount() : mRefCount(0) { }
54 template <class T
> friend class RefPointer
;
56 void ref() const { ++mRefCount
; }
57 unsigned int unref() const { return --mRefCount
; }
60 mutable AtomicCounter
<unsigned int> mRefCount
;
65 // A pointer type supported by reference counts.
66 // T must be derived from RefCount.
71 RefPointer() : ptr(0) {} // default to NULL pointer
72 RefPointer(const RefPointer
& p
) { if (p
) p
->ref(); ptr
= p
.ptr
; }
73 RefPointer(T
*p
) { if (p
) p
->ref(); ptr
= p
; }
75 ~RefPointer() { release(); }
77 RefPointer
& operator = (const RefPointer
& p
) { setPointer(p
.ptr
); return *this; }
78 RefPointer
& operator = (T
* p
) { setPointer(p
); return *this; }
80 // dereference operations
81 T
* get () const { return ptr
; } // mimic auto_ptr
82 operator T
* () const { return ptr
; }
83 T
* operator -> () const { return ptr
; }
84 T
& operator * () const { return *ptr
; }
87 void release() { if (ptr
&& ptr
->unref() == 0) delete ptr
; }
88 void setPointer(T
*p
) { if (p
) p
->ref(); release(); ptr
= p
; }
94 bool operator <(const RefPointer
<T
> &r1
, const RefPointer
<T
> &r2
)
96 T
*p1
= r1
.get(), *p2
= r2
.get();
97 return p1
&& p2
? *p1
< *p2
: p1
< p2
;
101 bool operator ==(const RefPointer
<T
> &r1
, const RefPointer
<T
> &r2
)
103 T
*p1
= r1
.get(), *p2
= r2
.get();
104 return p1
&& p2
? *p1
== *p2
: p1
== p2
;
108 bool operator !=(const RefPointer
<T
> &r1
, const RefPointer
<T
> &r2
)
110 T
*p1
= r1
.get(), *p2
= r2
.get();
111 return p1
&& p2
? *p1
!= *p2
: p1
!= p2
;
114 } // end namespace Security
116 #endif // !_SECURITY_REFCOUNT_H_