catch_objc_arc.hpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. #import <Foundation/Foundation.h>
  11. #ifdef __has_feature
  12. #define CATCH_ARC_ENABLED __has_feature(objc_arc)
  13. #else
  14. #define CATCH_ARC_ENABLED 0
  15. #endif
  16. void arcSafeRelease( NSObject* obj );
  17. id performOptionalSelector( id obj, SEL sel );
  18. #if !CATCH_ARC_ENABLED
  19. inline void arcSafeRelease( NSObject* obj ) {
  20. [obj release];
  21. }
  22. inline id performOptionalSelector( id obj, SEL sel ) {
  23. if( [obj respondsToSelector: sel] )
  24. return [obj performSelector: sel];
  25. return nil;
  26. }
  27. #define CATCH_UNSAFE_UNRETAINED
  28. #define CATCH_ARC_STRONG
  29. #else
  30. inline void arcSafeRelease( NSObject* ){}
  31. inline id performOptionalSelector( id obj, SEL sel ) {
  32. #ifdef __clang__
  33. #pragma clang diagnostic push
  34. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  35. #endif
  36. if( [obj respondsToSelector: sel] )
  37. return [obj performSelector: sel];
  38. #ifdef __clang__
  39. #pragma clang diagnostic pop
  40. #endif
  41. return nil;
  42. }
  43. #define CATCH_UNSAFE_UNRETAINED __unsafe_unretained
  44. #define CATCH_ARC_STRONG __strong
  45. #endif
  46. #endif // TWOBLUECUBES_CATCH_OBJC_ARC_HPP_INCLUDED