FBMacros.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * Copyright (c) 2015-present, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. */
  9. // Typedef to help with storing constant strings for enums.
  10. #if __has_feature(objc_arc)
  11. typedef __unsafe_unretained NSString* FBLiteralString;
  12. #else
  13. typedef NSString* FBLiteralString;
  14. #endif
  15. /*! Returns 'value' or nil if 'value' is an empty string */
  16. #define FBTransferEmptyStringToNil(value) ([value isEqual:@""] ? nil : value)
  17. /*! Returns 'value1' or 'value2' if 'value1' is an empty string */
  18. #define FBFirstNonEmptyValue(value1, value2) ^{ \
  19. id value1computed = value1; \
  20. return (value1computed == nil || [value1computed isEqual:@""] ? value2 : value1computed); \
  21. }()
  22. /*! Returns 'value' or NSNull if 'value' is nil */
  23. #define FBValueOrNull(value) ((value) ?: [NSNull null])
  24. /*!
  25. Returns name of class property as a string
  26. previously used [class new] errors out on certain classes because new will be declared unavailable
  27. Instead we are casting into a class to get compiler support with property name.
  28. */
  29. #define FBStringify(class, property) ({if(NO){[((class *)nil) property];} @#property;})
  30. /*! Creates weak type for given 'arg' */
  31. #define FBWeakify(arg) typeof(arg) __weak wda_weak_##arg = arg
  32. /*! Creates strong type for FBWeakify-ed 'arg' */
  33. #define FBStrongify(arg) \
  34. _Pragma("clang diagnostic push") \
  35. _Pragma("clang diagnostic ignored \"-Wshadow\"") \
  36. typeof(arg) arg = wda_weak_##arg \
  37. _Pragma("clang diagnostic pop")
  38. /*! Returns YES if current system version satisfies the given codition */
  39. #define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
  40. #define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
  41. #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
  42. #define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
  43. #define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
  44. /*! Converts the given number of milliseconds into seconds */
  45. #define FBMillisToSeconds(ms) ((ms) / 1000.0)
  46. /*! Converts boolean value to its string representation */
  47. #define FBBoolToString(b) ((b) ? @"true" : @"false")