SDInternalMacros.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * This file is part of the SDWebImage package.
  3. * (c) Olivier Poitrey <rs@dailymotion.com>
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. #import <Foundation/Foundation.h>
  9. #import <os/lock.h>
  10. #import <libkern/OSAtomic.h>
  11. #import "SDmetamacros.h"
  12. #ifndef SD_LOCK_DECLARE
  13. #if TARGET_OS_MACCATALYST
  14. #define SD_LOCK_DECLARE(lock) os_unfair_lock lock;
  15. #else
  16. #define SD_LOCK_DECLARE(lock) os_unfair_lock lock API_AVAILABLE(ios(10.0), tvos(10), watchos(3), macos(10.12)); \
  17. OSSpinLock lock##_deprecated;
  18. #endif
  19. #endif
  20. #ifndef SD_LOCK_INIT
  21. #if TARGET_OS_MACCATALYST
  22. #define SD_LOCK_INIT(lock) lock = OS_UNFAIR_LOCK_INIT;
  23. #else
  24. #define SD_LOCK_INIT(lock) if (@available(iOS 10, tvOS 10, watchOS 3, macOS 10.12, *)) lock = OS_UNFAIR_LOCK_INIT; \
  25. else lock##_deprecated = OS_SPINLOCK_INIT;
  26. #endif
  27. #endif
  28. #ifndef SD_LOCK
  29. #if TARGET_OS_MACCATALYST
  30. #define SD_LOCK(lock) os_unfair_lock_lock(&lock);
  31. #else
  32. #define SD_LOCK(lock) if (@available(iOS 10, tvOS 10, watchOS 3, macOS 10.12, *)) os_unfair_lock_lock(&lock); \
  33. else OSSpinLockLock(&lock##_deprecated);
  34. #endif
  35. #endif
  36. #ifndef SD_UNLOCK
  37. #if TARGET_OS_MACCATALYST
  38. #define SD_UNLOCK(lock) os_unfair_lock_unlock(&lock);
  39. #else
  40. #define SD_UNLOCK(lock) if (@available(iOS 10, tvOS 10, watchOS 3, macOS 10.12, *)) os_unfair_lock_unlock(&lock); \
  41. else OSSpinLockUnlock(&lock##_deprecated);
  42. #endif
  43. #endif
  44. #ifndef SD_OPTIONS_CONTAINS
  45. #define SD_OPTIONS_CONTAINS(options, value) (((options) & (value)) == (value))
  46. #endif
  47. #ifndef SD_CSTRING
  48. #define SD_CSTRING(str) #str
  49. #endif
  50. #ifndef SD_NSSTRING
  51. #define SD_NSSTRING(str) @(SD_CSTRING(str))
  52. #endif
  53. #ifndef SD_SEL_SPI
  54. #define SD_SEL_SPI(name) NSSelectorFromString([NSString stringWithFormat:@"_%@", SD_NSSTRING(name)])
  55. #endif
  56. #ifndef weakify
  57. #define weakify(...) \
  58. sd_keywordify \
  59. metamacro_foreach_cxt(sd_weakify_,, __weak, __VA_ARGS__)
  60. #endif
  61. #ifndef strongify
  62. #define strongify(...) \
  63. sd_keywordify \
  64. _Pragma("clang diagnostic push") \
  65. _Pragma("clang diagnostic ignored \"-Wshadow\"") \
  66. metamacro_foreach(sd_strongify_,, __VA_ARGS__) \
  67. _Pragma("clang diagnostic pop")
  68. #endif
  69. #define sd_weakify_(INDEX, CONTEXT, VAR) \
  70. CONTEXT __typeof__(VAR) metamacro_concat(VAR, _weak_) = (VAR);
  71. #define sd_strongify_(INDEX, VAR) \
  72. __strong __typeof__(VAR) VAR = metamacro_concat(VAR, _weak_);
  73. #if DEBUG
  74. #define sd_keywordify autoreleasepool {}
  75. #else
  76. #define sd_keywordify try {} @catch (...) {}
  77. #endif
  78. #ifndef onExit
  79. #define onExit \
  80. sd_keywordify \
  81. __strong sd_cleanupBlock_t metamacro_concat(sd_exitBlock_, __LINE__) __attribute__((cleanup(sd_executeCleanupBlock), unused)) = ^
  82. #endif
  83. typedef void (^sd_cleanupBlock_t)(void);
  84. #if defined(__cplusplus)
  85. extern "C" {
  86. #endif
  87. void sd_executeCleanupBlock (__strong sd_cleanupBlock_t *block);
  88. #if defined(__cplusplus)
  89. }
  90. #endif