FLEXClassBuilder.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // FLEXClassBuilder.h
  3. // FLEX
  4. //
  5. // Derived from MirrorKit.
  6. // Created by Tanner on 7/3/15.
  7. // Copyright (c) 2015 Tanner Bennett. All rights reserved.
  8. //
  9. #import <Foundation/Foundation.h>
  10. @class FLEXIvarBuilder, FLEXMethodBase, FLEXProperty, FLEXProtocol;
  11. #pragma mark FLEXClassBuilder
  12. @interface FLEXClassBuilder : NSObject
  13. @property (nonatomic, readonly) Class workingClass;
  14. /// Begins constructing a class with the given name.
  15. ///
  16. /// This new class will implicitly inherits from \c NSObject with \c 0 extra bytes.
  17. /// Classes created this way must be registered with \c -registerClass before being used.
  18. + (instancetype)allocateClass:(NSString *)name;
  19. /// Begins constructing a class with the given name and superclass.
  20. /// @discussion Calls \c -allocateClass:superclass:extraBytes: with \c 0 extra bytes.
  21. /// Classes created this way must be registered with \c -registerClass before being used.
  22. + (instancetype)allocateClass:(NSString *)name superclass:(Class)superclass;
  23. /// Begins constructing a new class object with the given name and superclass.
  24. /// @discussion Pass \c nil to \e superclass to create a new root class.
  25. /// Classes created this way must be registered with \c -registerClass before being used.
  26. + (instancetype)allocateClass:(NSString *)name superclass:(Class)superclass extraBytes:(size_t)bytes;
  27. /// Begins constructing a new root class object with the given name and \c 0 extra bytes.
  28. /// @discussion Classes created this way must be registered with \c -registerClass before being used.
  29. + (instancetype)allocateRootClass:(NSString *)name;
  30. /// Use this to modify existing classes. @warning You cannot add instance variables to existing classes.
  31. + (instancetype)builderForClass:(Class)cls;
  32. /// @return Any methods that failed to be added.
  33. - (NSArray<FLEXMethodBase *> *)addMethods:(NSArray<FLEXMethodBase *> *)methods;
  34. /// @return Any properties that failed to be added.
  35. - (NSArray<FLEXProperty *> *)addProperties:(NSArray<FLEXProperty *> *)properties;
  36. /// @return Any protocols that failed to be added.
  37. - (NSArray<FLEXProtocol *> *)addProtocols:(NSArray<FLEXProtocol *> *)protocols;
  38. /// @warning Adding Ivars to existing classes is not supported and will always fail.
  39. - (NSArray<FLEXIvarBuilder *> *)addIvars:(NSArray<FLEXIvarBuilder *> *)ivars;
  40. /// Finalizes construction of a new class.
  41. /// @discussion Once a class is registered, instance variables cannot be added.
  42. /// @note Raises an exception if called on a previously registered class.
  43. - (Class)registerClass;
  44. /// Uses \c objc_lookupClass to determine if the working class is registered.
  45. @property (nonatomic, readonly) BOOL isRegistered;
  46. @end
  47. #pragma mark FLEXIvarBuilder
  48. @interface FLEXIvarBuilder : NSObject
  49. /// Consider using the \c FLEXIvarBuilderWithNameAndType() macro below.
  50. /// @param name The name of the Ivar, such as \c \@"_value".
  51. /// @param size The size of the Ivar. Usually \c sizeof(type). For objects, this is \c sizeof(id).
  52. /// @param alignment The alignment of the Ivar. Usually \c log2(sizeof(type)).
  53. /// @param encoding The type encoding of the Ivar. For objects, this is \c \@(\@encode(id)), and for others it is \c \@(\@encode(type)).
  54. + (instancetype)name:(NSString *)name size:(size_t)size alignment:(uint8_t)alignment typeEncoding:(NSString *)encoding;
  55. @property (nonatomic, readonly) NSString *name;
  56. @property (nonatomic, readonly) NSString *encoding;
  57. @property (nonatomic, readonly) size_t size;
  58. @property (nonatomic, readonly) uint8_t alignment;
  59. @end
  60. #define FLEXIvarBuilderWithNameAndType(nameString, type) [FLEXIvarBuilder \
  61. name:nameString \
  62. size:sizeof(type) \
  63. alignment:log2(sizeof(type)) \
  64. typeEncoding:@(@encode(type)) \
  65. ]