FLEXRuntimeUtility.h 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // FLEXRuntimeUtility.h
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 6/8/14.
  6. // Copyright (c) 2020 FLEX Team. 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 *)safeClassNameForObject:(id)object;
  49. + (NSString *)safeDescriptionForObject:(id)object;
  50. + (NSString *)safeDebugDescriptionForObject:(id)object;
  51. + (BOOL)safeObject:(id)object isKindOfClass:(Class)cls;
  52. + (BOOL)safeObject:(id)object respondsToSelector:(SEL)sel;
  53. // Property Helpers
  54. + (BOOL)tryAddPropertyWithName:(const char *)name
  55. attributes:(NSDictionary<NSString *, NSString *> *)attributePairs
  56. toClass:(__unsafe_unretained Class)theClass;
  57. + (NSArray<NSString *> *)allPropertyAttributeKeys;
  58. // Method Helpers
  59. + (NSArray *)prettyArgumentComponentsForMethod:(Method)method;
  60. // Method Calling/Field Editing
  61. + (id)performSelector:(SEL)selector onObject:(id)object;
  62. + (id)performSelector:(SEL)selector
  63. onObject:(id)object
  64. withArguments:(NSArray *)arguments
  65. error:(NSError * __autoreleasing *)error;
  66. + (NSString *)editableJSONStringForObject:(id)object;
  67. + (id)objectValueFromEditableJSONString:(NSString *)string;
  68. + (NSValue *)valueForNumberWithObjCType:(const char *)typeEncoding fromInputString:(NSString *)inputString;
  69. + (void)enumerateTypesInStructEncoding:(const char *)structEncoding
  70. usingBlock:(void (^)(NSString *structName,
  71. const char *fieldTypeEncoding,
  72. NSString *prettyTypeEncoding,
  73. NSUInteger fieldIndex,
  74. NSUInteger fieldOffset))typeBlock;
  75. + (NSValue *)valueForPrimitivePointer:(void *)pointer objCType:(const char *)type;
  76. #pragma mark - Metadata Helpers
  77. + (NSString *)readableTypeForEncoding:(NSString *)encodingString;
  78. @end