1 /*
2  *  Created by Phil on 1/08/2012.
3  *  Copyright 2012 Two Blue Cubes Ltd. All rights reserved.
4  *
5  *  Distributed under the Boost Software License, Version 1.0. (See accompanying
6  *  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7  */
8 #ifndef TWOBLUECUBES_CATCH_OBJC_ARC_HPP_INCLUDED
9 #define TWOBLUECUBES_CATCH_OBJC_ARC_HPP_INCLUDED
10 
11 #import <Foundation/Foundation.h>
12 
13 #ifdef __has_feature
14 #define CATCH_ARC_ENABLED __has_feature(objc_arc)
15 #else
16 #define CATCH_ARC_ENABLED 0
17 #endif
18 
19 void arcSafeRelease( NSObject* obj );
20 id performOptionalSelector( id obj, SEL sel );
21 
22 #if !CATCH_ARC_ENABLED
arcSafeRelease(NSObject * obj)23 inline void arcSafeRelease( NSObject* obj ) {
24     [obj release];
25 }
performOptionalSelector(id obj,SEL sel)26 inline id performOptionalSelector( id obj, SEL sel ) {
27     if( [obj respondsToSelector: sel] )
28         return [obj performSelector: sel];
29     return nil;
30 }
31 #define CATCH_UNSAFE_UNRETAINED
32 #define CATCH_ARC_STRONG
33 #else
arcSafeRelease(NSObject *)34 inline void arcSafeRelease( NSObject* ){}
performOptionalSelector(id obj,SEL sel)35 inline id performOptionalSelector( id obj, SEL sel ) {
36 #ifdef __clang__
37 #pragma clang diagnostic push
38 #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
39 #endif
40     if( [obj respondsToSelector: sel] )
41         return [obj performSelector: sel];
42 #ifdef __clang__
43 #pragma clang diagnostic pop
44 #endif
45     return nil;
46 }
47 #define CATCH_UNSAFE_UNRETAINED __unsafe_unretained
48 #define CATCH_ARC_STRONG __strong
49 #endif
50 
51 #endif // TWOBLUECUBES_CATCH_OBJC_ARC_HPP_INCLUDED
52