Michael Baltaks avatar Michael Baltaks committed 4ca16a4

Use macros to support ARC in a backwards compatible way, as well as support new location for clang in Makefile

Comments (0)

Files changed (3)

+//
+//  ARCMacros.h
+//  InnerBand
+//
+// For an explanation of why these work, see:
+//
+//     http://raptureinvenice.com/arc-support-without-branches/
+//
+//  Created by John Blanco on 1/28/12.
+//  Copyright (c) 2012 Rapture In Venice. All rights reserved.
+//
+// NOTE: __bridge_tranfer is not included here because releasing would be inconsistent.
+//       Avoid it unless you're using ARC exclusively or managing it with __has_feature(objc_arc).
+//
+
+#if !defined(__clang__) || __clang_major__ < 3
+    #ifndef __bridge
+        #define __bridge
+    #endif
+
+    #ifndef __bridge_retain
+        #define __bridge_retain
+    #endif
+
+    #ifndef __bridge_retained
+        #define __bridge_retained
+    #endif
+
+    #ifndef __autoreleasing
+        #define __autoreleasing
+    #endif
+
+    #ifndef __strong
+        #define __strong
+    #endif
+
+    #ifndef __unsafe_unretained
+        #define __unsafe_unretained
+    #endif
+
+    #ifndef __weak
+        #define __weak
+    #endif
+#endif
+
+#if __has_feature(objc_arc)
+    #define SAFE_ARC_PROP_RETAIN strong
+    #define SAFE_ARC_RETAIN(x) (x)
+    #define SAFE_ARC_RELEASE(x)
+    #define SAFE_ARC_AUTORELEASE(x) (x)
+    #define SAFE_ARC_BLOCK_COPY(x) (x)
+    #define SAFE_ARC_BLOCK_RELEASE(x)
+    #define SAFE_ARC_SUPER_DEALLOC()
+    #define SAFE_ARC_AUTORELEASE_POOL_START() @autoreleasepool {
+    #define SAFE_ARC_AUTORELEASE_POOL_END() }
+#else
+    #define SAFE_ARC_PROP_RETAIN retain
+    #define SAFE_ARC_RETAIN(x) ([(x) retain])
+    #define SAFE_ARC_RELEASE(x) ([(x) release])
+    #define SAFE_ARC_AUTORELEASE(x) ([(x) autorelease])
+    #define SAFE_ARC_BLOCK_COPY(x) (Block_copy(x))
+    #define SAFE_ARC_BLOCK_RELEASE(x) (Block_release(x))
+    #define SAFE_ARC_SUPER_DEALLOC() ([super dealloc])
+    #define SAFE_ARC_AUTORELEASE_POOL_START() NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+    #define SAFE_ARC_AUTORELEASE_POOL_END() [pool release];
+#endif

ISO8601DateFormatter.m

 
 #import <Foundation/Foundation.h>
 #import "ISO8601DateFormatter.h"
+#import "ARCMacros.h"
 
 #ifndef DEFAULT_TIME_SEPARATOR
 #	define DEFAULT_TIME_SEPARATOR ':'
 }
 
 + (void) purgeGlobalCaches {
+#if !__has_feature(objc_arc)
 	NSMutableDictionary *oldCache = timeZonesByOffset;
+#endif
 	timeZonesByOffset = nil;
-	[oldCache release];
+	SAFE_ARC_RELEASE(oldCache);
 }
 
 - (NSCalendar *) makeCalendarWithDesiredConfiguration {
-	NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
+	NSCalendar *calendar = SAFE_ARC_AUTORELEASE([[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]);
 	calendar.firstWeekday = 2; //Monday
 	calendar.timeZone = [NSTimeZone defaultTimeZone];
 	return calendar;
 
 - (id) init {
 	if ((self = [super init])) {
-		parsingCalendar = [[self makeCalendarWithDesiredConfiguration] retain];
-		unparsingCalendar = [[self makeCalendarWithDesiredConfiguration] retain];
+		parsingCalendar = SAFE_ARC_RETAIN([self makeCalendarWithDesiredConfiguration]);
+		unparsingCalendar = SAFE_ARC_RETAIN([self makeCalendarWithDesiredConfiguration]);
 
 		format = ISO8601DateFormatCalendar;
 		timeSeparator = ISO8601DefaultTimeSeparatorCharacter;
 	return self;
 }
 - (void) dealloc {
-	[defaultTimeZone release];
+	SAFE_ARC_RELEASE(defaultTimeZone);
 
-	[unparsingFormatter release];
-	[lastUsedFormatString release];
-	[parsingCalendar release];
-	[unparsingCalendar release];
+	SAFE_ARC_RELEASE(unparsingFormatter);
+	SAFE_ARC_RELEASE(lastUsedFormatString);
+	SAFE_ARC_RELEASE(parsingCalendar);
+	SAFE_ARC_RELEASE(unparsingCalendar);
 
-	[super dealloc];
+	SAFE_ARC_SUPER_DEALLOC();
 }
 
 @synthesize defaultTimeZone;
 - (void) setDefaultTimeZone:(NSTimeZone *)tz {
 	if (defaultTimeZone != tz) {
-		[defaultTimeZone release];
-		defaultTimeZone = [tz retain];
+		SAFE_ARC_RELEASE(defaultTimeZone);
+		defaultTimeZone = SAFE_ARC_RETAIN(tz);
 
 		unparsingCalendar.timeZone = defaultTimeZone;
 	}
  *YYYY-MM-DD
  *YYYY-MM
  *YYYY
- *YY //century 
+ *YY //century
  * //Implied century: YY is 00-99
  *  YYMMDD
  *  YY-MM-DD
 - (NSDateComponents *) dateComponentsFromString:(NSString *)string timeZone:(out NSTimeZone **)outTimeZone range:(out NSRange *)outRange {
 	NSDate *now = [NSDate date];
 
-	NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
+	NSDateComponents *components = SAFE_ARC_AUTORELEASE([[NSDateComponents alloc] init]);
 	NSDateComponents *nowComponents = [parsingCalendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:now];
 
 	NSUInteger
 
 - (NSString *) replaceColonsInString:(NSString *)timeFormat withTimeSeparator:(unichar)timeSep {
 	if (timeSep != ':') {
-		NSMutableString *timeFormatMutable = [[timeFormat mutableCopy] autorelease];
+		NSMutableString *timeFormatMutable = SAFE_ARC_AUTORELEASE([timeFormat mutableCopy]);
 		[timeFormatMutable replaceOccurrencesOfString:@":"
 		                               	   withString:[NSString stringWithCharacters:&timeSep length:1U]
 	                                      	  options:NSBackwardsSearch | NSLiteralSearch
 	unparsingCalendar.timeZone = timeZone;
 
 	if (dateFormat != lastUsedFormatString) {
-		[unparsingFormatter release];
+		SAFE_ARC_RELEASE(unparsingFormatter);
 		unparsingFormatter = nil;
 
-		[lastUsedFormatString release];
-		lastUsedFormatString = [dateFormat retain];
+		SAFE_ARC_RELEASE(lastUsedFormatString);
+		lastUsedFormatString = SAFE_ARC_RETAIN(dateFormat);
 	}
 
 	if (!unparsingFormatter) {
 
 		timeString = [formatter stringForObjectValue:date];
 
-		[formatter release];
+		SAFE_ARC_RELEASE(formatter);
 	} else
 		timeString = @"";
 
-CLANG=/Developer/usr/bin/clang
+ifeq ($(wildcard /Developer/usr/bin/clang),)
+	CLANG=/usr/bin/clang
+else
+	CLANG=/Developer/usr/bin/clang
+endif
 CC=$(CLANG)
 CFLAGS+=-std=c99 -g -Werror -Wmissing-field-initializers -Wreturn-type -Wmissing-braces -Wparentheses -Wswitch -Wunused-function -Wunused-label -Wunused-variable -Wunused-value -Wshadow -Wsign-compare -Wnewline-eof -Wshorten-64-to-32 -Wundeclared-selector -Wmissing-prototypes -Wformat -Wunknown-pragmas
 LDFLAGS+=-framework Foundation
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.