]> git.saurik.com Git - apple/security.git/blob - Keychain/SecRuntime.h
Security-54.tar.gz
[apple/security.git] / Keychain / SecRuntime.h
1 /*
2 * Copyright (c) 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 // SecRuntime.h - CF runtime interface
20 //
21 #ifndef _SECURITY_SECRUNTIME_H_
22 #define _SECURITY_SECRUNTIME_H_
23
24 #include <CoreFoundation/CFRuntime.h>
25 #include <Security/refcount.h>
26
27
28 namespace Security
29 {
30
31 namespace KeychainCore
32 {
33
34 class SecCFObject : public RefCount
35 {
36 public:
37 virtual ~SecCFObject();
38 virtual bool equal(SecCFObject &other);
39 virtual CFHashCode hash();
40 };
41
42
43 class SecCFType : public CFRuntimeBase
44 {
45 public:
46 SecCFType(SecCFObject *obj);
47 ~SecCFType();
48
49 RefPointer<SecCFObject> mObject;
50 };
51
52
53 class CFClassBase : protected CFRuntimeClass
54 {
55 protected:
56 CFClassBase(const char *name);
57
58 const SecCFType *makeNew(SecCFObject *obj);
59 const SecCFType *handle(SecCFObject *obj);
60 SecCFObject *required(const SecCFType *type, OSStatus errorCode);
61
62 private:
63 static void finalizeType(CFTypeRef cf);
64 static Boolean equalType(CFTypeRef cf1, CFTypeRef cf2);
65 static CFHashCode hashType(CFTypeRef cf);
66
67 public:
68 CFTypeID typeId;
69 };
70
71
72 template <class Object, class APITypePtr, OSStatus ErrorCode>
73 class CFClass : public CFClassBase
74 {
75 public:
76 CFClass(const char *name) : CFClassBase(name) {}
77
78 APITypePtr handle(Object &obj)
79 {
80 return APITypePtr(CFClassBase::handle(&obj));
81 }
82
83 Object *required(APITypePtr type)
84 {
85 Object *object = dynamic_cast<Object *>(CFClassBase::required
86 (reinterpret_cast<const SecCFType *>(type), ErrorCode));
87 if (!object)
88 MacOSError::throwMe(ErrorCode);
89
90 return object;
91 }
92
93 // CF generator functions
94 APITypePtr operator () (Object *obj)
95 { return handle(*obj); }
96
97 APITypePtr operator () (const RefPointer<Object> &obj)
98 { return handle(*obj); }
99
100 Object * operator () (APITypePtr ref)
101 { return required(ref); }
102 };
103
104
105 } // end namespace KeychainCore
106
107 } // end namespace Security
108
109
110 #endif // !_SECURITY_SECRUNTIME_H_