PKMacros.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //
  2. // PKMacros.h
  3. // Download
  4. //
  5. // Created by Pavel on 30/05/15.
  6. // Copyright (c) 2015 Katunin. All rights reserved.
  7. //
  8. #ifndef Download_PKMacros_h
  9. #define Download_PKMacros_h
  10. #pragma mark - Block helpers
  11. #define BlockSafeRun(block_, ...) do { if ((block_) != NULL) (block_)(__VA_ARGS__); } while (NO)
  12. #define BlockSafeRunEx(defaultValue_, block_, ...) (((block_) != NULL) ? (block_)(__VA_ARGS__) : (defaultValue_))
  13. #define BlockSafeRunOnTargetQueue(queue, block, ...) do { if ((block) != NULL) dispatch_async(queue, ^{ (block)(__VA_ARGS__); }); } while (0)
  14. #define BlockSafeRunOnMainQueue(block, ...) BlockSafeRunOnTargetQueue(dispatch_get_main_queue(), (block), __VA_ARGS__)
  15. #if __has_feature(objc_arc)
  16. #define BlockWeakObject(o) __typeof__(o) __weak
  17. #define BlockWeakSelf BlockWeakObject(self)
  18. #define BlockStrongObject(o) __typeof__(o) __strong
  19. #define BlockStrongSelf BlockStrongObject(self)
  20. #define WeakifySelf BlockWeakSelf ___weakSelf___ = self; do {} while (0)
  21. #define StrongifySelf BlockStrongSelf self = ___weakSelf___; do {} while (0)
  22. #endif // __has_feature(objc_arc)
  23. #define SafeObjClassCast(destClass_, resultObj_, originalObj_) \
  24. destClass_ *resultObj_ = (destClass_ *)originalObj_;\
  25. NSAssert2((resultObj_) == nil || [(resultObj_) isKindOfClass:[destClass_ class]],\
  26. @"Incorrect cast: original object (%@) could not be casted to the destination class (%@)", \
  27. (originalObj_), NSStringFromClass([destClass_ class]))
  28. #define SafeObjProtocolCast(destProtocol_, resultObj_, originalObj_) \
  29. id <destProtocol_> resultObj_ = (id <destProtocol_>)originalObj_;\
  30. NSAssert2((resultObj_) == nil || [(resultObj_) conformsToProtocol:@protocol(destProtocol_)],\
  31. @"Incorrect cast: original object (%@) could not be casted to the destination protocol (%@)", \
  32. (originalObj_), NSStringFromProtocol(@protocol(destProtocol_)))
  33. #endif // Download_PKMacros_h