]>
git.saurik.com Git - apple/security.git/blob - Keychain/Refs.h
a3464d218f12206888d042cc9825e8b6f54e7c6f
2 * Copyright (c) 2000-2001 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.
25 #include <Security/handleobject.h>
31 namespace KeychainCore
34 class ReferencedObject
: public RefCount
37 ReferencedObject() : mHandle(0) {}
38 virtual ~ReferencedObject() {}
40 void addedRef(CSSM_HANDLE handle
) { mHandle
= handle
; }
41 void removedRef(CSSM_HANDLE handle
) { mHandle
= 0; }
42 CSSM_HANDLE
handle() const { return mHandle
; }
51 class RefObject
: public HandleObject
, public RefCount
54 RefObject(ReferencedObject
&object
) : mObject(&object
)
57 mObject
->addedRef(reinterpret_cast<CSSM_HANDLE
>(HandleObject::handle()));
60 void ref() const { RefCount::ref(); }
61 unsigned int unref() const { return RefCount::unref(); }
63 RefPointer
<ReferencedObject
> mObject
;
67 inline void ReferencedObject::killRef()
69 delete &killHandle
<RefObject
>(mHandle
);
74 template <class _Object
, class _ObjectImpl
, class _Handle
, OSStatus _ErrorCode
>
78 static _Handle
handle(const _Object
&object
)
83 _Handle handle
= reinterpret_cast<_Handle
>(object
->handle()); // Return the existing handle if it exists
90 RefObject
*ref
= new RefObject(*object
);
92 return reinterpret_cast<_Handle
>(ref
->HandleObject::handle());
95 static void retain(_Handle handle
)
96 { findHandle
<RefObject
>(CSSM_HANDLE(handle
), _ErrorCode
).ref(); }
98 static void release(_Handle handle
)
100 RefObject
&ref
= findHandle
<RefObject
>(CSSM_HANDLE(handle
), _ErrorCode
);
101 if (ref
.unref() == 0)
104 ref
.mObject
->removedRef(CSSM_HANDLE(handle
));
106 delete &killHandle
<RefObject
>(CSSM_HANDLE(handle
), _ErrorCode
);
110 static _Object
required(_Handle handle
)
112 RefObject
&ref
= findHandle
<RefObject
>(CSSM_HANDLE(handle
), _ErrorCode
);
114 MacOSError::throwMe(_ErrorCode
);
115 _ObjectImpl
*impl
= dynamic_cast<_ObjectImpl
*>(&(*ref
.mObject
));
117 MacOSError::throwMe(_ErrorCode
);
118 return _Object(impl
);
122 }; // end namespace KeychainCore
124 } // end namespace Security