BUCommonMacros.h 6.8 KB

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