]> git.saurik.com Git - apple/security.git/blob - Keychain/Refs.h
a3464d218f12206888d042cc9825e8b6f54e7c6f
[apple/security.git] / Keychain / Refs.h
1 /*
2 * Copyright (c) 2000-2001 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 #if 0
19 //
20 // Refs.h
21 //
22 #ifndef _H_REFS
23 #define _H_REFS
24
25 #include <Security/handleobject.h>
26 #include <set>
27
28 namespace Security
29 {
30
31 namespace KeychainCore
32 {
33
34 class ReferencedObject : public RefCount
35 {
36 public:
37 ReferencedObject() : mHandle(0) {}
38 virtual ~ReferencedObject() {}
39
40 void addedRef(CSSM_HANDLE handle) { mHandle = handle; }
41 void removedRef(CSSM_HANDLE handle) { mHandle = 0; }
42 CSSM_HANDLE handle() const { return mHandle; }
43
44 void killRef();
45
46 private:
47 CSSM_HANDLE mHandle;
48 };
49
50
51 class RefObject : public HandleObject, public RefCount
52 {
53 public:
54 RefObject(ReferencedObject &object) : mObject(&object)
55 {
56 if (mObject)
57 mObject->addedRef(reinterpret_cast<CSSM_HANDLE>(HandleObject::handle()));
58 }
59
60 void ref() const { RefCount::ref(); }
61 unsigned int unref() const { return RefCount::unref(); }
62
63 RefPointer<ReferencedObject> mObject;
64 };
65
66
67 inline void ReferencedObject::killRef()
68 {
69 delete &killHandle<RefObject>(mHandle);
70 mHandle = 0;
71 }
72
73
74 template <class _Object, class _ObjectImpl, class _Handle, OSStatus _ErrorCode>
75 class Ref
76 {
77 public:
78 static _Handle handle(const _Object &object)
79 {
80 if (!object)
81 return 0;
82
83 _Handle handle = reinterpret_cast<_Handle>(object->handle()); // Return the existing handle if it exists
84 if (handle)
85 {
86 retain(handle);
87 return handle;
88 }
89
90 RefObject *ref = new RefObject(*object);
91 ref->ref();
92 return reinterpret_cast<_Handle>(ref->HandleObject::handle());
93 }
94
95 static void retain(_Handle handle)
96 { findHandle<RefObject>(CSSM_HANDLE(handle), _ErrorCode).ref(); }
97
98 static void release(_Handle handle)
99 {
100 RefObject &ref = findHandle<RefObject>(CSSM_HANDLE(handle), _ErrorCode);
101 if (ref.unref() == 0)
102 {
103 if (ref.mObject)
104 ref.mObject->removedRef(CSSM_HANDLE(handle));
105
106 delete &killHandle<RefObject>(CSSM_HANDLE(handle), _ErrorCode);
107 }
108 }
109
110 static _Object required(_Handle handle)
111 {
112 RefObject &ref = findHandle<RefObject>(CSSM_HANDLE(handle), _ErrorCode);
113 if (!ref.mObject)
114 MacOSError::throwMe(_ErrorCode);
115 _ObjectImpl *impl = dynamic_cast<_ObjectImpl *>(&(*ref.mObject));
116 if (!impl)
117 MacOSError::throwMe(_ErrorCode);
118 return _Object(impl);
119 }
120 };
121
122 }; // end namespace KeychainCore
123
124 } // end namespace Security
125
126 #endif // _H_REFS
127 #endif