]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/refcount.h
Security-179.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / refcount.h
1 /*
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All Rights Reserved.
3 *
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
8 * using this file.
9 *
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.
16 */
17
18
19 /*
20 Based on code donated by Perry Kiehtreiber
21 */
22 #ifndef _SECURITY_REFCOUNT_H_
23 #define _SECURITY_REFCOUNT_H_
24
25 #include <Security/threading.h>
26
27 namespace Security
28 {
29
30 //
31 // RefCount/RefPointer - a simple reference counting facility.
32 //
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.
35 //
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.
40 //
41 // There is no (thread safe) way to determine whether you are the only thread holding
42 // a pointer to a particular RefCount object.
43 //
44
45
46 //
47 // Base class for reference counted objects
48 //
49 class RefCount {
50 public:
51 RefCount() : mRefCount(0) { }
52
53 protected:
54 template <class T> friend class RefPointer;
55
56 void ref() const { ++mRefCount; }
57 unsigned int unref() const { return --mRefCount; }
58
59 private:
60 mutable AtomicCounter<unsigned int> mRefCount;
61 };
62
63
64 //
65 // A pointer type supported by reference counts.
66 // T must be derived from RefCount.
67 //
68 template <class T>
69 class RefPointer {
70 public:
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; }
74
75 ~RefPointer() { release(); }
76
77 RefPointer& operator = (const RefPointer& p) { setPointer(p.ptr); return *this; }
78 RefPointer& operator = (T * p) { setPointer(p); return *this; }
79
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; }
85
86 protected:
87 void release() { if (ptr && ptr->unref() == 0) delete ptr; }
88 void setPointer(T *p) { if (p) p->ref(); release(); ptr = p; }
89
90 T *ptr;
91 };
92
93 template <class T>
94 bool operator <(const RefPointer<T> &r1, const RefPointer<T> &r2)
95 {
96 T *p1 = r1.get(), *p2 = r2.get();
97 return p1 && p2 ? *p1 < *p2 : p1 < p2;
98 }
99
100 template <class T>
101 bool operator ==(const RefPointer<T> &r1, const RefPointer<T> &r2)
102 {
103 T *p1 = r1.get(), *p2 = r2.get();
104 return p1 && p2 ? *p1 == *p2 : p1 == p2;
105 }
106
107 template <class T>
108 bool operator !=(const RefPointer<T> &r1, const RefPointer<T> &r2)
109 {
110 T *p1 = r1.get(), *p2 = r2.get();
111 return p1 && p2 ? *p1 != *p2 : p1 != p2;
112 }
113
114 } // end namespace Security
115
116 #endif // !_SECURITY_REFCOUNT_H_