]>
Commit | Line | Data |
---|---|---|
fa7225c8 A |
1 | /* |
2 | * Copyright (c) 2016 Apple Inc. All Rights Reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * This file contains Original Code and/or Modifications of Original Code | |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. Please obtain a copy of the License at | |
10 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
11 | * file. | |
12 | * | |
13 | * The Original Code and all software distributed under the License are | |
14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
18 | * Please see the License for the specific language governing rights and | |
19 | * limitations under the License. | |
20 | * | |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
23 | ||
24 | ||
25 | /* | |
26 | * SOSPersist.h -- Utility routines for get/set in CFDictionary | |
27 | */ | |
28 | ||
29 | #ifndef _SOSPERSIST_H_ | |
30 | #define _SOSPERSIST_H_ | |
31 | ||
32 | __BEGIN_DECLS | |
33 | ||
34 | #include <utilities/SecCFRelease.h> | |
35 | #include <utilities/SecCFWrappers.h> | |
36 | #include <CoreFoundation/CoreFoundation.h> | |
37 | ||
38 | #include <stdlib.h> | |
39 | ||
40 | #include <AssertMacros.h> | |
41 | ||
42 | ||
43 | static inline bool SOSPeerGetPersistedBoolean(CFDictionaryRef persisted, CFStringRef key) { | |
44 | CFBooleanRef boolean = CFDictionaryGetValue(persisted, key); | |
45 | return boolean && CFBooleanGetValue(boolean); | |
46 | } | |
47 | ||
48 | static inline CFDataRef SOSPeerGetPersistedData(CFDictionaryRef persisted, CFStringRef key) { | |
49 | return asData(CFDictionaryGetValue(persisted, key), NULL); | |
50 | } | |
51 | ||
52 | static inline int64_t SOSPeerGetPersistedInt64(CFDictionaryRef persisted, CFStringRef key) { | |
53 | int64_t integer = 0; | |
54 | CFNumberRef number = CFDictionaryGetValue(persisted, key); | |
55 | if (number) { | |
56 | CFNumberGetValue(number, kCFNumberSInt64Type, &integer); | |
57 | } | |
58 | return integer; | |
59 | } | |
60 | ||
61 | static inline bool SOSPeerGetOptionalPersistedCFIndex(CFDictionaryRef persisted, CFStringRef key, CFIndex *value) { | |
62 | bool exists = false; | |
63 | CFNumberRef number = CFDictionaryGetValue(persisted, key); | |
64 | if (number) { | |
65 | exists = true; | |
66 | CFNumberGetValue(number, kCFNumberCFIndexType, value); | |
67 | } | |
68 | return exists; | |
69 | } | |
70 | ||
71 | static inline void SOSPersistBool(CFMutableDictionaryRef persist, CFStringRef key, bool value) { | |
72 | CFDictionarySetValue(persist, key, value ? kCFBooleanTrue : kCFBooleanFalse); | |
73 | } | |
74 | ||
75 | static inline void SOSPersistInt64(CFMutableDictionaryRef persist, CFStringRef key, int64_t value) { | |
76 | CFNumberRef number = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &value); | |
77 | CFDictionarySetValue(persist, key, number); | |
78 | CFReleaseSafe(number); | |
79 | } | |
80 | ||
81 | static inline void SOSPersistCFIndex(CFMutableDictionaryRef persist, CFStringRef key, CFIndex value) { | |
82 | CFNumberRef number = CFNumberCreate(kCFAllocatorDefault, kCFNumberCFIndexType, &value); | |
83 | CFDictionarySetValue(persist, key, number); | |
84 | CFReleaseSafe(number); | |
85 | } | |
86 | ||
87 | static inline void SOSPersistOptionalValue(CFMutableDictionaryRef persist, CFStringRef key, CFTypeRef value) { | |
88 | if (value) | |
89 | CFDictionarySetValue(persist, key, value); | |
90 | } | |
91 | ||
92 | __END_DECLS | |
93 | ||
94 | #endif /* !_SOSPERSIST_H_ */ |