-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFMTDefines.h
147 lines (117 loc) · 5.85 KB
/
FMTDefines.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
/*
Copyright (c) 2010 Filip Krikava
Permission is hereby granted, free of charge, to any person obtaining a copy
of this sofFMTare and associated documentation files (the "SofFMTare"), to deal
in the SofFMTare without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the SofFMTare, and to permit persons to whom the SofFMTare is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the SofFMTare.
THE SOFFMTARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFFMTARE OR THE USE OR OTHER DEALINGS IN
THE SOFFMTARE.
*/
// following assertions were taken from: GMTDefines.h from the google-mac-toolbox:
// http://code.google.com/p/google-toolbox-for-mac/
#ifndef FMTDevLog
#ifndef NDEBUG
#define FMTDevLog(...) NSLog(__VA_ARGS__)
#else
#define FMTDevLog(...) do { } while (0)
#endif // NDEBUG
#endif // FMTDevLog
// TODO: rename to NSStr
#ifndef FMTStr
#define FMTStr(fmt,...) [NSString stringWithFormat:fmt,##__VA_ARGS__]
#endif // FMTStr
#define FMTStrc(cstr) [NSString stringWithCString:(cstr) encoding:NSUTF8StringEncoding]
#ifndef FMTTraceLog
#ifndef NTRACE
#define FMTTraceLog(...) NSLog(@"%@: %@",FMTStr(@"[\%s:\%s:\%d]",__PRETTY_FUNCTION__,__FILE__,__LINE__),FMTStr(__VA_ARGS__))
#define FMTTrace() NSLog(@"[\%s:\%s:\%d]",__PRETTY_FUNCTION__,__FILE__,__LINE__)
#endif // NTRACE
#endif // FMTTraceLog
#ifndef FMTAssert
#if !defined(NS_BLOCK_ASSERTIONS)
#define FMTAssert(condition, ...) \
do { \
if (!(condition)) { \
[[NSAssertionHandler currentHandler] \
handleFailureInFunction:[NSString stringWithUTF8String:__PRETTY_FUNCTION__] \
file:[NSString stringWithUTF8String:__FILE__] \
lineNumber:__LINE__ \
description:__VA_ARGS__]; \
} \
} while(0)
#define FMTAssertNotNil(var) FMTAssert(var != nil, FMTStr(@"Variabe %@ must not be nil", @#var));
#else // !defined(NS_BLOCK_ASSERTIONS)
#define FMTAssert(condition, ...) do { } while (0)
#endif // !defined(NS_BLOCK_ASSERTIONS)
#endif // FMTAssert
#ifndef FMTFail
#define FMTFail(...) FMTAssert(NO,##__VA_ARGS__)
#endif // FMTFail
/// This macro implements the various methods needed to make a safe singleton.
///
/// This Singleton pattern was taken from:
/// http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/chapter_3_section_10.html
///
/// Sample usage:
///
/// SINGLETON_BOILERPLATE(SomeUsefulManager, sharedSomeUsefulManager)
/// (with no trailing semicolon)
///
/// This code here is based on Foundation/GTMObjectSingleton.h from google-toolbox-for-mac
///
#ifndef SINGLETON_BOILERPLATE
#define SINGLETON_BOILERPLATE(_object_name_, _shared_obj_name_) SINGLETON_BOILERPLATE_FULL(_object_name_, _shared_obj_name_, init)
#endif // SINGLETON_BOILERPLATE
#ifndef SINGLETON_BOILERPLATE_FULL
#define SINGLETON_BOILERPLATE_FULL(_object_name_, _shared_obj_name_, _init_) \
static _object_name_ *z##_shared_obj_name_ = nil; \
+ (_object_name_ *)_shared_obj_name_ { \
@synchronized(self) { \
if (z##_shared_obj_name_ == nil) { \
/* Note that 'self' may not be the same as _object_name_ */ \
/* first assignment done in allocWithZone but we must reassign in case init fails */ \
z##_shared_obj_name_ = [[self alloc] _init_]; \
FMTAssert((z##_shared_obj_name_ != nil), @"didn't catch singleton allocation"); \
} \
} \
return z##_shared_obj_name_; \
} \
+ (id)allocWithZone:(NSZone *)zone { \
@synchronized(self) { \
if (z##_shared_obj_name_ == nil) { \
z##_shared_obj_name_ = [super allocWithZone:zone]; \
return z##_shared_obj_name_; \
} \
} \
\
/* We can't return the shared instance, because it's been init'd */ \
FMTAssert(NO, @"use the singleton API, not alloc+init"); \
return nil; \
} \
- (id)retain { \
return self; \
} \
- (NSUInteger)retainCount { \
return NSUIntegerMax; \
} \
- (void)release { \
} \
- (id)autorelease { \
return self; \
} \
- (id)copyWithZone:(NSZone *) __unused zone { \
return self; \
} \
#endif // SINGLETON_BOILERPLATE_FULL
#ifndef FMTGetErrorDescription
#define FMTGetErrorDescription(error) [[(error) userInfo] objectForKey:NSLocalizedDescriptionKey]
#endif // FMTGetErrorDescription