FLEXRuntimeUtility.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // FLEXRuntimeUtility.h
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 6/8/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXRuntimeConstants.h"
  9. #define PropertyKey(suffix) kFLEXPropertyAttributeKey##suffix : @""
  10. #define PropertyKeyGetter(getter) kFLEXPropertyAttributeKeyCustomGetter : NSStringFromSelector(@selector(getter))
  11. #define PropertyKeySetter(setter) kFLEXPropertyAttributeKeyCustomSetter : NSStringFromSelector(@selector(setter))
  12. /// Takes: min iOS version, property name, target class, property type, and a list of attributes
  13. #define FLEXRuntimeUtilityTryAddProperty(iOS_atLeast, name, cls, type, ...) ({ \
  14. if (@available(iOS iOS_atLeast, *)) { \
  15. NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithDictionary:@{ \
  16. kFLEXPropertyAttributeKeyTypeEncoding : @(type), \
  17. __VA_ARGS__ \
  18. }]; \
  19. [FLEXRuntimeUtility \
  20. tryAddPropertyWithName:#name \
  21. attributes:attrs \
  22. toClass:cls \
  23. ]; \
  24. } \
  25. })
  26. /// Takes: min iOS version, property name, target class, property type, and a list of attributes
  27. #define FLEXRuntimeUtilityTryAddNonatomicProperty(iOS_atLeast, name, cls, type, ...) \
  28. FLEXRuntimeUtilityTryAddProperty(iOS_atLeast, name, cls, @encode(type), PropertyKey(NonAtomic), __VA_ARGS__);
  29. /// Takes: min iOS version, property name, target class, property type (class name), and a list of attributes
  30. #define FLEXRuntimeUtilityTryAddObjectProperty(iOS_atLeast, name, cls, type, ...) \
  31. FLEXRuntimeUtilityTryAddProperty(iOS_atLeast, name, cls, FLEXEncodeClass(type), PropertyKey(NonAtomic), __VA_ARGS__);
  32. @interface FLEXRuntimeUtility : NSObject
  33. // General Helpers
  34. + (BOOL)pointerIsValidObjcObject:(const void *)pointer;
  35. /// Unwraps raw pointers to objects stored in NSValue, and re-boxes C strings into NSStrings.
  36. + (id)potentiallyUnwrapBoxedPointer:(id)returnedObjectOrNil type:(const FLEXTypeEncoding *)returnType;
  37. /// Some fields have a name in their encoded string (e.g. \"width\"d)
  38. /// @return the offset to skip the field name, 0 if there is no name
  39. + (NSUInteger)fieldNameOffsetForTypeEncoding:(const FLEXTypeEncoding *)typeEncoding;
  40. /// Given name "foo" and type "int" this would return "int foo", but
  41. /// given name "foo" and type "T *" it would return "T *foo"
  42. + (NSString *)appendName:(NSString *)name toType:(NSString *)typeEncoding;
  43. /// @return The class hierarchy for the given object or class,
  44. /// from the current class to the root-most class.
  45. + (NSArray<Class> *)classHierarchyOfObject:(id)objectOrClass;
  46. /// Used to describe an object in brief within an explorer row
  47. + (NSString *)summaryForObject:(id)value;
  48. + (NSString *)safeDescriptionForObject:(id)object;
  49. + (NSString *)safeDebugDescriptionForObject:(id)object;
  50. // Property Helpers
  51. + (BOOL)tryAddPropertyWithName:(const char *)name
  52. attributes:(NSDictionary<NSString *, NSString *> *)attributePairs
  53. toClass:(__unsafe_unretained Class)theClass;
  54. + (NSArray<NSString *> *)allPropertyAttributeKeys;
  55. // Method Helpers
  56. + (NSArray *)prettyArgumentComponentsForMethod:(Method)method;
  57. // Method Calling/Field Editing
  58. + (id)performSelector:(SEL)selector onObject:(id)object;
  59. + (id)performSelector:(SEL)selector
  60. onObject:(id)object
  61. withArguments:(NSArray *)arguments
  62. error:(NSError * __autoreleasing *)error;
  63. + (NSString *)editableJSONStringForObject:(id)object;
  64. + (id)objectValueFromEditableJSONString:(NSString *)string;
  65. + (NSValue *)valueForNumberWithObjCType:(const char *)typeEncoding fromInputString:(NSString *)inputString;
  66. + (void)enumerateTypesInStructEncoding:(const char *)structEncoding
  67. usingBlock:(void (^)(NSString *structName,
  68. const char *fieldTypeEncoding,
  69. NSString *prettyTypeEncoding,
  70. NSUInteger fieldIndex,
  71. NSUInteger fieldOffset))typeBlock;
  72. + (NSValue *)valueForPrimitivePointer:(void *)pointer objCType:(const char *)type;
  73. #pragma mark - Metadata Helpers
  74. + (NSString *)readableTypeForEncoding:(NSString *)encodingString;
  75. @end