Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 153 lines (136 sloc) 6.283 kB
dc5be1c Initial check-in.
uli authored
1 //
2 // UKHelperMacros.h
3 //
4 // Created by Uli Kusterer on 09.08.07.
1572f3a @uliwitness Added a KEYPATH macro and more comments.
authored
5 // Copyright 2007-2014 Uli Kusterer.
720b0e7 @uliwitness Added licensing information to the source files.
authored
6 //
7 // This software is provided 'as-is', without any express or implied
8 // warranty. In no event will the authors be held liable for any damages
9 // arising from the use of this software.
10 //
11 // Permission is granted to anyone to use this software for any purpose,
12 // including commercial applications, and to alter it and redistribute it
13 // freely, subject to the following restrictions:
14 //
15 // 1. The origin of this software must not be misrepresented; you must not
16 // claim that you wrote the original software. If you use this software
17 // in a product, an acknowledgment in the product documentation would be
18 // appreciated but is not required.
19 //
20 // 2. Altered source versions must be plainly marked as such, and must not be
21 // misrepresented as being the original software.
22 //
23 // 3. This notice may not be removed or altered from any source
24 // distribution.
25 //
dc5be1c Initial check-in.
uli authored
26
720b0e7 @uliwitness Added licensing information to the source files.
authored
27 /*
28 You'd generally #import this file in your project's prefix header.
29 */
dc5be1c Initial check-in.
uli authored
30
31 //
32 // Use UKLog() instead of NSLog() to output debug-only messages that you don't
33 // want in release builds. You'll have to add DEBUG=1 to the preprocessor macros
34 // in your target's compiler settings for the Development build configuration.
35 //
36 // This prefixes the method or function name to the message.
37 //
38
5afa0d7 @uliwitness Merge a few compatibility ifdefs in for people who redefine their log…
authored
39 #ifndef UKLog
dc5be1c Initial check-in.
uli authored
40 #if DEBUG
41 #define UKLog(args...) NSLog( @"%s: %@", __PRETTY_FUNCTION__, [NSString stringWithFormat: args])
42 #else
5afa0d7 @uliwitness Merge a few compatibility ifdefs in for people who redefine their log…
authored
43 #define UKLog(args...) while(0) // stubbed out
dc5be1c Initial check-in.
uli authored
44 #endif
5afa0d7 @uliwitness Merge a few compatibility ifdefs in for people who redefine their log…
authored
45 #endif // UKLog
dc5be1c Initial check-in.
uli authored
46
47
1572f3a @uliwitness Added a KEYPATH macro and more comments.
authored
48 // A pointer value that is guaranteed to cause a crash when a message is sent to it.
49 // Handy for assigning to instance variables in dealloc to catch talking to released objects.
4b07924 @uliwitness Added UKInvalidPointer constant that fills a pointer with 0x55... lik…
authored
50 #if __LP64__
51 #define UKInvalidPointer ((id)0x5555555555555555)
52 #else
53 #define UKInvalidPointer ((id)0x55555555)
54 #endif
55
56
dc5be1c Initial check-in.
uli authored
57 // The following use the same syntax as the ones in GNUstep. Just cuz that's
58 // the closest we have to a standard for stuff like this.
59 //
60 // Create a pool around some code by doing:
61 // CREATE_AUTORELEASE_POOL(myPool);
62 // // Use the pool.
63 // DESTROY(myPool);
64 //
65 // ASSIGN() is a neat macro to use inside mutators, DESTROY() is a shorthand
66 // that lets you release an object and clear its variable in one go.
67 //
68 // The do/while(0) stuff is just there so the macro behaves just like any other
69 // function call, as far as if/else etc. are concerned.
70
71 #define CREATE_AUTORELEASE_POOL(pool) NSAutoreleasePool* (pool) = [[NSAutoreleasePool alloc] init]
72
1572f3a @uliwitness Added a KEYPATH macro and more comments.
authored
73 #ifndef ASSIGN // SenTest declares its own macro of this name.
74 // ASSIGN(ivar,val)
75 // Releases the current value in ivar and assigns val to it, retaining it. Doesn't release
76 // ivar's value until the new value has been assigned.
dc5be1c Initial check-in.
uli authored
77 #define ASSIGN(targ,newval) do {\
f8692e7 Added PROPERTY(), fixed DESTROY() to clear first, to avoid accidental…
uli authored
78 NSObject* __UKHELPERMACRO_OLDTARG = (NSObject*)(targ);\
dc5be1c Initial check-in.
uli authored
79 (targ) = [(newval) retain];\
80 [__UKHELPERMACRO_OLDTARG release];\
81 } while(0)
5afa0d7 @uliwitness Merge a few compatibility ifdefs in for people who redefine their log…
authored
82 #endif // !defined(ASSIGN)
ba018c7 @uliwitness Merge of fix for SenTest users that lets them use the ASSIGN function…
authored
83 #define UKAssign(targ,newval) do {\
84 NSObject* __UKHELPERMACRO_OLDTARG = (NSObject*)(targ);\
85 (targ) = [(newval) retain];\
86 [__UKHELPERMACRO_OLDTARG release];\
87 } while(0)
1572f3a @uliwitness Added a KEYPATH macro and more comments.
authored
88
89 // ASSIGNMUTABLECOPY(ivar,val)
90 // Releases the current value in ivar and assigns [val mutableCopy] to it. Doesn't release
91 // ivar's value until the new value has been assigned.
a1d334c @uliwitness Added ASSIGNMUTABLECOPY() to UKHelperMacros.
authored
92 #define ASSIGNMUTABLECOPY(targ,newval) do {\
93 NSObject* __UKHELPERMACRO_OLDTARG = (NSObject*)(targ);\
94 (targ) = [(newval) mutableCopy];\
95 [__UKHELPERMACRO_OLDTARG release];\
96 } while(0)
1572f3a @uliwitness Added a KEYPATH macro and more comments.
authored
97 // ASSIGNCOPY(ivar,val)
98 // Releases the current value in ivar and assigns [val copy] to it. Doesn't release
99 // ivar's value until the new value has been assigned.
15726fe @uliwitness Added ASSIGNCOPY (e.g. for strings)
authored
100 #define ASSIGNCOPY(targ,newval) do {\
101 NSObject* __UKHELPERMACRO_OLDTARG = (NSObject*)(targ);\
102 (targ) = [(newval) copy];\
103 [__UKHELPERMACRO_OLDTARG release];\
104 } while(0)
105
1572f3a @uliwitness Added a KEYPATH macro and more comments.
authored
106 // DESTROY(ivar)
107 // Releases the variable 'ivar' and sets its value to NIL even *before* the release.
4b07924 @uliwitness Added UKInvalidPointer constant that fills a pointer with 0x55... lik…
authored
108 #define DESTROY(targ) do {\
31ccfaf @uliwitness Added ASSIGNCOPY and ASSIGNMUTABLECOPY.
authored
109 NSObject* __UKHELPERMACRO_OLDTARG = (NSObject*)(targ);\
4b07924 @uliwitness Added UKInvalidPointer constant that fills a pointer with 0x55... lik…
authored
110 (targ) = nil;\
31ccfaf @uliwitness Added ASSIGNCOPY and ASSIGNMUTABLECOPY.
authored
111 [__UKHELPERMACRO_OLDTARG release];\
112 } while(0)
1572f3a @uliwitness Added a KEYPATH macro and more comments.
authored
113 // DESTROY_DEALLOC(ivar)
114 // Releases the variable 'ivar' and sets its value to UKInvalidPointer even *before* the release.
4b07924 @uliwitness Added UKInvalidPointer constant that fills a pointer with 0x55... lik…
authored
115 #define DESTROY_DEALLOC(targ) do {\
f8692e7 Added PROPERTY(), fixed DESTROY() to clear first, to avoid accidental…
uli authored
116 NSObject* __UKHELPERMACRO_OLDTARG = (NSObject*)(targ);\
4b07924 @uliwitness Added UKInvalidPointer constant that fills a pointer with 0x55... lik…
authored
117 (targ) = UKInvalidPointer;\
f8692e7 Added PROPERTY(), fixed DESTROY() to clear first, to avoid accidental…
uli authored
118 [__UKHELPERMACRO_OLDTARG release];\
dc5be1c Initial check-in.
uli authored
119 } while(0)
f8692e7 Added PROPERTY(), fixed DESTROY() to clear first, to avoid accidental…
uli authored
120
1572f3a @uliwitness Added a KEYPATH macro and more comments.
authored
121 // PROPERTY(propertyName) --> @"propertyName"
f8692e7 Added PROPERTY(), fixed DESTROY() to clear first, to avoid accidental…
uli authored
122 // The following macro is for specifying property (ivar) names to KVC or KVO methods.
123 // These methods generally take strings, but strings don't get checked for typos
124 // by the compiler. If you write PROPERTY(fremen) instead of PROPERTY(frame),
125 // the compiler will immediately complain that it doesn't know the selector
126 // 'fremen', and thus point out the typo. For this to work, you need to make
127 // sure the warning -Wunknown-selector is on.
128 //
1572f3a @uliwitness Added a KEYPATH macro and more comments.
authored
129 // Thanks to the NO ? trick goes to Rob Rix. This means that the NSStringFromSelector()
130 // is compiled (and causes errors with invalid selectors) but is never used at runtime
131 // (so performance of debug and non-debug builds stays identical).
f8692e7 Added PROPERTY(), fixed DESTROY() to clear first, to avoid accidental…
uli authored
132
133 #if DEBUG
1572f3a @uliwitness Added a KEYPATH macro and more comments.
authored
134 #define PROPERTY(propName) (YES ? @ # propName : NSStringFromSelector(@selector(propName)))
f8692e7 Added PROPERTY(), fixed DESTROY() to clear first, to avoid accidental…
uli authored
135 #else
136 #define PROPERTY(propName) @#propName
137 #endif
1572f3a @uliwitness Added a KEYPATH macro and more comments.
authored
138
139
140 // KEYPATH(obj,key.path.on.object) --> @"key.path.on.object"
141 // The following macro is like PROPERTY, but for entire key paths. Thanks to
142 // Rob Rix for donating this. The else case essentially returns NIL cast to NSString*,
143 // but in the process of determining the type happens to validate our path as a side effect.
144 // Since the else case is unreachable, this has the same runtime effect as specifying a string
145 // constant, but it is type-checked.
146 // You only pass in 'obj' to allow determining the type. It doesn't actually call anything.
147
148 #if DEBUG
149 #define KEYPATH(OBJ, PATH) (YES ? @ # PATH : (__typeof__(^ NSString * { (void)((__typeof__(OBJ))nil).PATH; return nil; }())) nil)
150 #else
151 #define KEYPATH(OBJ, PATH) (@ # PATH)
152 #endif
Something went wrong with that request. Please try again.