BUCommonMacros.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // BUCommonMacros.h
  3. // BUAdSDK
  4. //
  5. // Created by 崔亚楠 on 2018/10/23.
  6. // Copyright © 2018年 bytedance. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. #import <libkern/OSAtomic.h>
  10. #import <os/lock.h>
  11. #import "BULogMacros.h"
  12. FOUNDATION_EXPORT NSString * const BUSDKVersion;
  13. /// 标记是否是开发状态,正式上线发版时置 0
  14. #define DevEnv 0
  15. /** String **/
  16. #define BUEmptyString (@"");
  17. #define BUSafeString(__string) ((__string && [__string isKindOfClass:[NSString class]]) ? __string :@"")
  18. #define BUSafeDictionary(__aDictionary) ((__aDictionary && [__aDictionary isKindOfClass:[NSDictionary class]]) ? __aDictionary :@{})
  19. /** VALID CHECKING**/
  20. #define BUCheckValidString(__string) (__string && [__string isKindOfClass:[NSString class]] && [__string length])
  21. #define BUCheckValidNumber(__aNumber) (__aNumber && [__aNumber isKindOfClass:[NSNumber class]])
  22. #define BUCheckValidArray(__aArray) (__aArray && [__aArray isKindOfClass:[NSArray class]] && [__aArray count])
  23. #define BUCheckValidDictionary(__aDictionary) (__aDictionary && [__aDictionary isKindOfClass:[NSDictionary class]] && [__aDictionary count])
  24. #define BUCheckValidDate(__aDate) (__aDate && [__aDate isKindOfClass:[NSDate class]])
  25. #define BUCheckValidData(__aData) (__aData && [__aData isKindOfClass:[NSData class]])
  26. /** Color String**/
  27. #define BUColorString(__string) [UIColor bu_colorWithHexString:(__string)]
  28. /*********************************************************************************************************/
  29. //强弱引用转换,用于解决代码块(block)与强引用对象之间的循环引用问题
  30. #ifndef bu_weakify
  31. #if __has_feature(objc_arc)
  32. #define bu_weakify(object) __weak __typeof__(object) weak##object = object;
  33. #else
  34. #define bu_weakify(object) __block __typeof__(object) block##object = object;
  35. #endif
  36. #endif
  37. #ifndef bu_strongify
  38. #if __has_feature(objc_arc)
  39. #define bu_strongify(object) __typeof__(object) object = weak##object;
  40. #else
  41. #define bu_strongify(object) __typeof__(object) object = block##object;
  42. #endif
  43. #endif
  44. /*********************************************************************************************************/
  45. #ifndef BUisEmptyString
  46. #define BUisEmptyString(str) (!str || ![str isKindOfClass:[NSString class]] || str.length == 0)
  47. #endif
  48. #ifndef BUIsEmptyArray
  49. #define BUIsEmptyArray(array) (!array || ![array isKindOfClass:[NSArray class]] || array.count == 0)
  50. #endif
  51. #ifndef BUIsEmptyDictionary
  52. #define BUIsEmptyDictionary(dict) (!dict || ![dict isKindOfClass:[NSDictionary class]] || ((NSDictionary *)dict).count == 0)
  53. #endif
  54. #ifndef BUMinX
  55. #define BUMinX(view) CGRectGetMinX(view.frame)
  56. #endif
  57. #ifndef BUMinY
  58. #define BUMinY(view) CGRectGetMinY(view.frame)
  59. #endif
  60. #ifndef BUMaxX
  61. #define BUMaxX(view) CGRectGetMaxX(view.frame)
  62. #endif
  63. #ifndef BUMaxY
  64. #define BUMaxY(view) CGRectGetMaxY(view.frame)
  65. #endif
  66. #ifndef BUWidth
  67. #define BUWidth(view) view.frame.size.width
  68. #endif
  69. #ifndef BUHeight
  70. #define BUHeight(view) view.frame.size.height
  71. #endif
  72. #ifndef BUScreenWidth
  73. #define BUScreenWidth [[UIScreen mainScreen] bounds].size.width
  74. #endif
  75. #ifndef BUScreenHeight
  76. #define BUScreenHeight [[UIScreen mainScreen] bounds].size.height
  77. #endif
  78. #ifndef BUMINScreenSide
  79. #define BUMINScreenSide MIN([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)
  80. #endif
  81. #ifndef BUMAXScreenSide
  82. #define BUMAXScreenSide MAX([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)
  83. #endif
  84. #define BUIsNotchScreen bu_is_notch_screen()
  85. #define BUiPhoneX BUIsNotchScreen
  86. #define kBUDefaultNavigationBarHeight (BUiPhoneX?88:64) // 导航条高度
  87. #define kBUSafeTopMargin (BUiPhoneX?24:0)
  88. #define kBUDefaultStautsBarHeight (BUiPhoneX?44:20) // 状态栏高度
  89. #define BUOnePixel (1.0f/[[UIScreen mainScreen] scale])
  90. ///全局队列
  91. #ifndef BUDispatchGetGlobalQueue
  92. #define BUDispatchGetGlobalQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
  93. #endif
  94. #ifndef BUDispatchGetHighQueue
  95. #define BUDispatchGetHighQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)
  96. #endif
  97. //单例
  98. #define BU_SINGLETION(...) \
  99. + (instancetype)sharedInstance NS_SWIFT_NAME(shared());
  100. #define BU_DEF_SINGLETION(...) \
  101. + (instancetype)sharedInstance \
  102. { \
  103. static dispatch_once_t once; \
  104. static id __singletion; \
  105. dispatch_once(&once,^{__singletion = [[self alloc] init];}); \
  106. return __singletion; \
  107. }
  108. FOUNDATION_EXPORT void bu_safe_dispatch_sync_main_queue(void (^block)(void));
  109. FOUNDATION_EXPORT void bu_safe_dispatch_async_main_queue(void (^block)(void));
  110. FOUNDATION_EXPORT id BU_JSONObjectByRemovingKeysWithNullValues(id JSONObject);
  111. FOUNDATION_EXPORT BOOL bu_is_notch_screen(void);
  112. FOUNDATION_EXPORT UIEdgeInsets portraitAdSafeInsets(void);
  113. #pragma mark - 锁
  114. #define BU_USE_OS_UNFAIR_LOCK (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_10_0)
  115. #ifndef BU_LOCK_DECLARE
  116. #if BU_USE_OS_UNFAIR_LOCK
  117. #define BU_LOCK_DECLARE(lock) os_unfair_lock lock
  118. #else
  119. #define BU_LOCK_DECLARE(lock) os_unfair_lock lock API_AVAILABLE(ios(10.0), tvos(10), watchos(3), macos(10.12)); \
  120. OSSpinLock lock##_deprecated;
  121. #endif
  122. #endif
  123. #ifndef BU_LOCK_INIT
  124. #if BU_USE_OS_UNFAIR_LOCK
  125. #define BU_LOCK_INIT(lock) lock = OS_UNFAIR_LOCK_INIT
  126. #else
  127. #define BU_LOCK_INIT(lock) if (@available(iOS 10, tvOS 10, watchOS 3, macOS 10.12, *)) lock = OS_UNFAIR_LOCK_INIT; \
  128. else lock##_deprecated = OS_SPINLOCK_INIT;
  129. #endif
  130. #endif
  131. #ifndef BU_LOCK
  132. #if BU_USE_OS_UNFAIR_LOCK
  133. #define BU_LOCK(lock) os_unfair_lock_lock(&lock)
  134. #else
  135. #define BU_LOCK(lock) if (@available(iOS 10, tvOS 10, watchOS 3, macOS 10.12, *)) os_unfair_lock_lock(&lock); \
  136. else OSSpinLockLock(&lock##_deprecated);
  137. #endif
  138. #endif
  139. #ifndef BU_UNLOCK
  140. #if BU_USE_OS_UNFAIR_LOCK
  141. #define BU_UNLOCK(lock) os_unfair_lock_unlock(&lock)
  142. #else
  143. #define BU_UNLOCK(lock) if (@available(iOS 10, tvOS 10, watchOS 3, macOS 10.12, *)) os_unfair_lock_unlock(&lock); \
  144. else OSSpinLockUnlock(&lock##_deprecated);
  145. #endif
  146. #endif
  147. @protocol BUDictionarify <NSObject>
  148. @required
  149. - (NSDictionary *)toDictionary;
  150. @end
  151. // 对枚举值进行日志字符串转换, 例如对于一个枚举值 1表示激励视频广告的意思, 将返回: 激励视频广告(value:1)
  152. FOUNDATION_EXPORT NSString *NSStringLogFromBUAdEnumItem(NSInteger enumItem, NSDictionary *dic, NSString *defaultValue);
  153. // 对枚举值进行字符串转换 例如对于一个枚举值 1表示rewarded_ad的字符串, 将返回: rewarded_ad
  154. FOUNDATION_EXPORT NSString *NSStringFromBUAdEnumItem(NSInteger enumItem, NSDictionary *dic, NSString *defaultValue);