-
Notifications
You must be signed in to change notification settings - Fork 0
/
QCUtilities.h
341 lines (293 loc) · 7.89 KB
/
QCUtilities.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
* QCUtilities.h
* CFRaii
*
* Copyright (c) 2009-2010 Richard A. Brown
*
* See license.txt for licensing terms and conditions.
*/
/*
* Copyright (c) 2000-2004 Apple Computer, Inc. All Rights Reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
#ifndef _QC_UTILITIES_GUARD_
#define _QC_UTILITIES_GUARD_
#include <tr1/type_traits>
#include <stdexcept>
#include <stdio.h>
#include "QCMacrosInternal.h"
BEGIN_QC_NAMESPACE
class CFRaiiException
{
CFTypeID _expectedID, _actualID;
protected:
char *_description;
public:
CFRaiiException(CFTypeID const &expected, CFTypeID const &actual)
: _expectedID(expected), _actualID(actual), _description(NULL)
{ }
virtual char const *what()
{
if (_description == NULL)
{
(void)asprintf(&_description, "actual type id was %lu; expected %lu", _actualID, _expectedID);
}
return _description;
}
virtual ~CFRaiiException()
{
if (_description != NULL)
{
free(_description);
}
}
};
//
// Traits of popular CF types
//
template <class CFType>
class CFTraits : public std::tr1::false_type
{ };
template <>
class CFTraits<CFTypeRef> : public std::tr1::true_type
{ };
#define __CFType(name) \
template <> class CFTraits<name##Ref> : public std::tr1::true_type\
{ \
public: \
static CFTypeID cfid() { return name##GetTypeID(); } \
static CFTypeID GetTypeID(name##Ref ref) { return CFGetTypeID(ref); }\
static bool check(name##Ref ref) { return GetTypeID(ref) == cfid(); } \
};
__CFType(CFNull)
__CFType(CFBoolean)
__CFType(CFNumber)
__CFType(CFString)
__CFType(CFData)
__CFType(CFURL)
__CFType(CFBundle)
__CFType(CFArray)
__CFType(CFDictionary)
__CFType(CFSet)
//
// Initialize-only self-releasing CF object handler (lightweight).
//
template <class CFType> class CFRef
{
public:
CFRef() : mRef(NULL) { }
CFRef(CFType ref) : mRef(ref) { }
~CFRef() { this->release(); }
CFRef(const CFRef &ref) : mRef(ref) {}
template <class _T> CFRef(const CFRef<_T> &ref) : mRef(ref) {}
CFRef(CFTypeRef ref)
: mRef(CFType(ref))
{
if (ref != NULL && !CFTraits<CFType>::check(ref))
{
throw CFRaiiException(CFTraits<CFType>::GetTypeID(ref), CFTraits<CFType>::cfid());
}
}
CFRef &take(CFType ref)
{ this->release(); mRef = ref; return *this; }
CFType yield()
{ CFType r = mRef; mRef = NULL; return r; }
CFRef &operator = (CFType ref)
{ if (ref) CFRetain(ref); return take(ref); }
CFRef &operator = (const CFRef &ref)
{ if (ref) CFRetain(ref); return take(ref); }
// take variant for when newly created CFType is returned
// via a ptr-to-CFType argument.
CFType *take()
{ if (mRef) CFRelease(mRef); mRef = NULL; return &mRef; }
operator CFType () const { return mRef; }
operator bool () const { return mRef != NULL; }
bool operator ! () const { return mRef == NULL; }
CFType get() const { return mRef; }
CFType &aref()
{ take(NULL); return mRef; }
CFType retain() const
{ if (mRef) CFRetain(mRef); return mRef; }
void release() const
{ if (mRef) CFRelease(mRef); }
template <class NewType>
bool is() const { return CFTraits<NewType>::check(mRef); }
template <class OldType>
static CFType check(OldType cf, OSStatus err)
{
if (cf && !CFTraits<CFType>::check(cf))
{
throw CFRaiiException(CFTraits<CFType>::GetTypeID(cf), CFTraits<CFType>::cfid());
}
return CFType(cf);
}
template <class NewType>
NewType as() const { return NewType(mRef); }
template <class NewType>
NewType as(OSStatus err) const { return CFRef<NewType>::check(mRef, err); }
private:
CFType mRef;
};
template <class CFType>
class CFCopyRef : public CFRef<CFType>
{
typedef CFRef<CFType> _Base;
public:
CFCopyRef() { }
CFCopyRef(CFType ref) : _Base(ref) { this->retain(); }
CFCopyRef(const CFCopyRef &ref) : _Base(ref) { this->retain(); }
template <class _T> CFCopyRef(const CFRef<_T> &ref) : _Base(ref) { this->retain(); }
CFCopyRef(CFTypeRef ref, OSStatus err) : _Base(ref, err) { this->retain(); }
CFCopyRef &take(CFType ref)
{ _Base::take(ref); return *this; }
CFCopyRef &operator = (CFType ref)
{ if (ref) CFRetain(ref); return take(ref); }
CFCopyRef &operator = (const CFCopyRef &ref)
{ _Base::operator = (ref); return *this; }
};
// MARK: -
// MARK: CFNumber Templates
//
// This is nasty because CFNumber does not support unsigned types, and there's really no portably-safe
// way of working around this. So the handling of unsigned numbers is "almost correct."
//
template <class Number>
class CFNumberTraits;
template <>
struct CFNumberTraits<char>
{
static const CFNumberType numberType = kCFNumberCharType;
typedef char ValueType;
};
template <>
struct CFNumberTraits<short>
{
static const CFNumberType numberType = kCFNumberShortType;
typedef short ValueType;
};
template <>
struct CFNumberTraits<int>
{
static const CFNumberType numberType = kCFNumberIntType;
typedef int ValueType;
};
template <>
struct CFNumberTraits<long>
{
static const CFNumberType numberType = kCFNumberLongType;
typedef long ValueType;
};
template <>
struct CFNumberTraits<long long>
{
static const CFNumberType numberType = kCFNumberLongLongType;
typedef long long ValueType;
};
template <>
struct CFNumberTraits<float>
{
static const CFNumberType numberType = kCFNumberFloatType;
typedef float ValueType;
};
template <>
struct CFNumberTraits<double>
{
static const CFNumberType numberType = kCFNumberDoubleType;
typedef double ValueType;
};
// don't use the variable-width number types like CFIndex
template <>
struct CFNumberTraits<unsigned char>
{
static const CFNumberType numberType = kCFNumberIntType;
typedef int ValueType;
};
template <>
struct CFNumberTraits<unsigned short>
{
static const CFNumberType numberType = kCFNumberIntType;
typedef int ValueType;
};
template <>
struct CFNumberTraits<unsigned int>
{
static const CFNumberType numberType = kCFNumberLongLongType;
typedef long long ValueType;
};
template <>
struct CFNumberTraits<unsigned long>
{
static const CFNumberType numberType = kCFNumberLongLongType;
typedef long long ValueType;
};
template <>
struct CFNumberTraits<unsigned long long>
{
static const CFNumberType numberType = kCFNumberLongLongType;
typedef long long ValueType;
};
template <class N>
bool GetCFNumberValue(CFNumberRef number, N &value)
{
if (std::tr1::is_arithmetic<N>::value)
{
return CFNumberGetValue(number
, CFNumberTraits<N>::numberType
, &value
);
}
else
{
return false;
}
}
#if 0
template <class Number>
Number cfNumber(CFNumberRef number)
{
typename CFNumberTraits<Number>::ValueType value;
if (CFNumberGetValue(number, CFNumberTraits<Number>::numberType, &value))
{
return value;
}
else
{
CFError::throwMe();
}
}
template <class Number>
Number cfNumber(CFNumberRef number, Number defaultValue)
{
typename CFNumberTraits<Number>::ValueType value;
if (CFNumberGetValue(number, CFNumberTraits<Number>::numberType, &value))
return value;
else
return defaultValue;
}
template <class Number>
CFNumberRef makeCFNumber(Number value)
{
typename CFNumberTraits<Number>::ValueType cfValue = value;
return CFNumberCreate(NULL, CFNumberTraits<Number>::numberType, &cfValue);
}
#endif
END_QC_NAMESPACE
#endif