Skip to content
This repository has been archived by the owner on Jun 2, 2018. It is now read-only.

Commit

Permalink
Prefix category methods
Browse files Browse the repository at this point in the history
See issue #130.
  • Loading branch information
a2 authored and zwaldowski committed Nov 7, 2013
1 parent 28b5f33 commit 92bc00e
Show file tree
Hide file tree
Showing 93 changed files with 1,663 additions and 1,574 deletions.
16 changes: 8 additions & 8 deletions BlocksKit/A2BlockDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
@param selectorsForPropertyNames A dictionary with property names as keys and
selector strings as objects.
*/
+ (void) linkDataSourceMethods: (NSDictionary *) selectorsForPropertyNames;
+ (void)bk_linkDataSourceMethods:(NSDictionary *)selectorsForPropertyNames;

/** Synthesizes multiple properties and links them to the appropriate selector
in the delegate protocol.
Expand All @@ -50,7 +50,7 @@
@param selectorsForPropertyNames A dictionary with property names as keys and
selectors strings as objects.
*/
+ (void) linkDelegateMethods: (NSDictionary *) selectorsForPropertyNames;
+ (void)bk_linkDelegateMethods:(NSDictionary *)selectorsForPropertyNames;

/** Synthesizes multiple properties and links them to the appropriate selector
in the given protocol.
Expand All @@ -63,33 +63,33 @@
@param selectorsForPropertyNames A dictionary with property names as keys and
selector strings as objects.
*/
+ (void) linkProtocol: (Protocol *) protocol methods: (NSDictionary *) selectorsForPropertyNames;
+ (void)bk_linkProtocol:(Protocol *)protocol methods:(NSDictionary *)selectorsForPropertyNames;

/** @name Delegate replacement properties */

/** Registers a dynamic data source replacement using the property name
`dataSource` and the protocol name `FooBarDataSource` for an instance of
`FooBar`. */
+ (void) registerDynamicDataSource;
+ (void)bk_registerDynamicDataSource;

/** Registers a dynamic delegate replacement using the property name `delegate`
and the protocol name `FooBarDelegate` for an instance of `FooBar`. */
+ (void) registerDynamicDelegate;
+ (void)bk_registerDynamicDelegate;

/** Registers a dynamic data source replacement using the given property name
and the protocol name `FooBarDataSource` for an instance of `FooBar`.
@param dataSourceName The name of the class' data source property. Must not be
nil.
*/
+ (void) registerDynamicDataSourceNamed: (NSString *) dataSourceName;
+ (void)bk_registerDynamicDataSourceNamed:(NSString *)dataSourceName;

/** Registers a dynamic delegate replacement using the given property name and
the protocol name `FooBarDelegate` for an instance of `FooBar`.
@param delegateName The name of the class' delegate property. Must not be nil.
*/
+ (void) registerDynamicDelegateNamed: (NSString *) delegateName;
+ (void)bk_registerDynamicDelegateNamed:(NSString *)delegateName;

/** Registers a dynamic protocol implementation replacement
using the given property name and the given protocol.
Expand All @@ -98,6 +98,6 @@
as `safeDelegate`. Must not be nil.
@param protocol A properly encoded protocol. Must not be NULL.
*/
+ (void) registerDynamicDelegateNamed: (NSString *) delegateName forProtocol: (Protocol *) protocol;
+ (void)bk_registerDynamicDelegateNamed:(NSString *)delegateName forProtocol:(Protocol *)protocol;

@end
79 changes: 36 additions & 43 deletions BlocksKit/A2BlockDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ static BOOL bk_object_isKindOfClass(id obj, Class testClass)
{
BOOL isKindOfClass = NO;
Class cls = object_getClass(obj);
while (cls && !isKindOfClass)
{
while (cls && !isKindOfClass) {
isKindOfClass = (cls == testClass);
cls = class_getSuperclass(cls);
}
Expand All @@ -43,15 +42,13 @@ static SEL getterForProperty(Class cls, NSString *propertyName)
SEL getter = NULL;

objc_property_t property = class_getProperty(cls, propertyName.UTF8String);
if (property)
{
if (property) {
char *getterName = property_copyAttributeValue(property, "G");
if (getterName) getter = sel_getUid(getterName);
free(getterName);
}

if (!getter)
{
if (!getter) {
getter = NSSelectorFromString(propertyName);
}

Expand All @@ -63,26 +60,24 @@ static SEL setterForProperty(Class cls, NSString *propertyName)
SEL setter = NULL;

objc_property_t property = class_getProperty(cls, propertyName.UTF8String);
if (property)
{
if (property) {
char *setterName = property_copyAttributeValue(property, "S");
if (setterName) setter = sel_getUid(setterName);
free(setterName);
}

if (!setter)
{
unichar firstChar = [propertyName characterAtIndex: 0];
NSString *coda = [propertyName substringFromIndex: 1];
if (!setter) {
unichar firstChar = [propertyName characterAtIndex:0];
NSString *coda = [propertyName substringFromIndex:1];

setter = NSSelectorFromString([NSString stringWithFormat: @"set%c%@:", toupper(firstChar), coda]);
setter = NSSelectorFromString([NSString stringWithFormat:@"set%c%@:", toupper(firstChar), coda]);
}

return setter;
}

static inline SEL prefixedSelector(SEL selector) {
return NSSelectorFromString([@"a2_" stringByAppendingString: NSStringFromSelector(selector)]);
return NSSelectorFromString([@"a2_" stringByAppendingString:NSStringFromSelector(selector)]);
}

#pragma mark -
Expand All @@ -91,34 +86,33 @@ @implementation NSObject (A2BlockDelegate)

#pragma mark Helper

+ (NSMutableDictionary *) bk_delegateNameMap
+ (NSMutableDictionary *)bk_delegateNameMap
{
NSMutableDictionary *propertyMap = [self associatedValueForKey: _cmd];
NSMutableDictionary *propertyMap = [self bk_associatedValueForKey:_cmd];

if (!propertyMap)
{
if (!propertyMap) {
propertyMap = [NSMutableDictionary dictionary];
[self associateValue: propertyMap withKey: _cmd];
[self bk_associateValue:propertyMap withKey:_cmd];
}

return propertyMap;
}

#pragma mark Linking block properties

+ (void) linkDataSourceMethods: (NSDictionary *) dictionary
+ (void)bk_linkDataSourceMethods:(NSDictionary *)dictionary
{
[self linkProtocol: a2_dataSourceProtocol(self) methods: dictionary];
[self bk_linkProtocol:a2_dataSourceProtocol(self) methods:dictionary];
}

+ (void) linkDelegateMethods: (NSDictionary *) dictionary
+ (void)bk_linkDelegateMethods:(NSDictionary *)dictionary
{
[self linkProtocol: a2_delegateProtocol(self) methods: dictionary];
[self bk_linkProtocol:a2_delegateProtocol(self) methods:dictionary];
}

+ (void) linkProtocol: (Protocol *) protocol methods: (NSDictionary *) dictionary
+ (void)bk_linkProtocol:(Protocol *)protocol methods:(NSDictionary *)dictionary
{
[dictionary each:^(NSString *propertyName, NSString *selectorName) {
[dictionary bk_each:^(NSString *propertyName, NSString *selectorName) {
objc_property_t property = class_getProperty(self, propertyName.UTF8String);
NSCAssert2(property, @"Property \"%@\" does not exist on class %s", propertyName, class_getName(self));

Expand All @@ -145,10 +139,10 @@ + (void) linkProtocol: (Protocol *) protocol methods: (NSDictionary *) dictionar
}

IMP getterImplementation = imp_implementationWithBlock(^(NSObject *delegatingObject){
return [[delegatingObject dynamicDelegateForProtocol: protocol] blockImplementationForMethod: selector];
return [[delegatingObject bk_dynamicDelegateForProtocol:protocol] blockImplementationForMethod:selector];
});
IMP setterImplementation = imp_implementationWithBlock(^(NSObject *delegatingObject, id block){
A2DynamicDelegate *dynamicDelegate = [delegatingObject dynamicDelegateForProtocol: protocol];
A2DynamicDelegate *dynamicDelegate = [delegatingObject bk_dynamicDelegateForProtocol:protocol];

if (delegateProperty.length) {
SEL a2_setter = prefixedSelector(setterForProperty(delegatingObject.class, delegateProperty));
Expand All @@ -161,10 +155,9 @@ + (void) linkProtocol: (Protocol *) protocol methods: (NSDictionary *) dictionar
}
}

[dynamicDelegate implementMethod: selector withBlock: block];
[dynamicDelegate implementMethod:selector withBlock:block];
});


const char *getterTypes = "@@:";
BOOL success = class_addMethod(self, getter, getterImplementation, getterTypes);
NSCAssert1(success, @"Could not implement getter for \"%@\" property.", propertyName);
Expand All @@ -177,51 +170,51 @@ + (void) linkProtocol: (Protocol *) protocol methods: (NSDictionary *) dictionar

#pragma mark Dynamic Delegate Replacement

+ (void) registerDynamicDataSource
+ (void)bk_registerDynamicDataSource
{
[self registerDynamicDelegateNamed: @"dataSource" forProtocol: a2_dataSourceProtocol(self)];
[self bk_registerDynamicDelegateNamed:@"dataSource" forProtocol:a2_dataSourceProtocol(self)];
}
+ (void) registerDynamicDelegate
+ (void)bk_registerDynamicDelegate
{
[self registerDynamicDelegateNamed: @"delegate" forProtocol: a2_delegateProtocol(self)];
[self bk_registerDynamicDelegateNamed:@"delegate" forProtocol:a2_delegateProtocol(self)];
}

+ (void) registerDynamicDataSourceNamed: (NSString *) dataSourceName
+ (void)bk_registerDynamicDataSourceNamed:(NSString *)dataSourceName
{
[self registerDynamicDelegateNamed: dataSourceName forProtocol: a2_dataSourceProtocol(self)];
[self bk_registerDynamicDelegateNamed:dataSourceName forProtocol:a2_dataSourceProtocol(self)];
}
+ (void) registerDynamicDelegateNamed: (NSString *) delegateName
+ (void)bk_registerDynamicDelegateNamed:(NSString *)delegateName
{
[self registerDynamicDelegateNamed: delegateName forProtocol: a2_delegateProtocol(self)];
[self bk_registerDynamicDelegateNamed:delegateName forProtocol:a2_delegateProtocol(self)];
}

+ (void) registerDynamicDelegateNamed: (NSString *) delegateName forProtocol: (Protocol *) protocol
+ (void)bk_registerDynamicDelegateNamed:(NSString *)delegateName forProtocol:(Protocol *)protocol
{
NSString *protocolName = NSStringFromProtocol(protocol);
NSMutableDictionary *propertyMap = [self bk_delegateNameMap];
if (propertyMap[protocolName])
return;
if (propertyMap[protocolName]) return;

SEL getter = getterForProperty(self, delegateName);
SEL a2_getter = prefixedSelector(getter);
SEL setter = setterForProperty(self, delegateName);
SEL a2_setter = prefixedSelector(setter);

IMP getterImplementation = imp_implementationWithBlock(^(NSObject *delegatingObject){
return [[delegatingObject dynamicDelegateForProtocol: protocol] realDelegate];
return [[delegatingObject bk_dynamicDelegateForProtocol:protocol] realDelegate];
});

IMP setterImplementation = imp_implementationWithBlock(^(NSObject *delegatingObject, id delegate){
A2DynamicDelegate *dynamicDelegate = [delegatingObject dynamicDelegateForProtocol: protocol];
A2DynamicDelegate *dynamicDelegate = [delegatingObject bk_dynamicDelegateForProtocol:protocol];

if ([delegatingObject respondsToSelector:a2_setter]) {
id originalDelegate = objc_msgSend(delegatingObject, a2_getter, delegate);
if (!bk_object_isKindOfClass(originalDelegate, [A2DynamicDelegate class]))
objc_msgSend(delegatingObject, a2_setter, dynamicDelegate);
}

if ([delegate isEqual: dynamicDelegate])
if ([delegate isEqual:dynamicDelegate]) {
delegate = nil;
}

dynamicDelegate.realDelegate = delegate;
});
Expand Down
20 changes: 10 additions & 10 deletions BlocksKit/A2BlockInvocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
@param methodSignature An Objective-C method signature matching the block.
@return An initialized block invocation object.
*/
- (id) initWithBlock: (id) block methodSignature: (NSMethodSignature *)methodSignature;
- (id)initWithBlock:(id)block methodSignature:(NSMethodSignature *)methodSignature;

/** @name Getting the Block and Method Signatures */

Expand Down Expand Up @@ -88,13 +88,13 @@
@see argumentsRetained
*/
- (void) retainArguments;
- (void)retainArguments;

/** Returns YES if the receiver has retained its arguments, NO otherwise.
@see retainArguments
*/
- (BOOL) argumentsRetained;
- (BOOL)argumentsRetained;

/** Gets the receiver's return value.
Expand All @@ -121,7 +121,7 @@
for more information.
@see setReturnValue:
*/
- (void) getReturnValue: (void *) retLoc;
- (void)getReturnValue:(void *)retLoc;

/** Sets the receiver’s return value.
Expand All @@ -132,7 +132,7 @@
@see invoke
@see getReturnValue:
*/
- (void) setReturnValue: (void *) retLoc;
- (void)setReturnValue:(void *)retLoc;

/** Returns by indirection the receiver's argument at a specified index.
Expand All @@ -154,7 +154,7 @@
at 0 representing the first argument of the underlying block.
@see setArgument:atIndex:
*/
- (void) getArgument: (void *) argumentLocation atIndex: (NSInteger) idx;
- (void)getArgument:(void *)argumentLocation atIndex:(NSInteger)idx;

/** Sets an argument of the receiver.
Expand All @@ -175,11 +175,11 @@
@param idx An integer specifying the index of the argument, starting at 0
representing the first argument of the underlying block.
*/
- (void) setArgument: (void *) argumentLocation atIndex: (NSInteger) idx;
- (void)setArgument:(void *)argumentLocation atIndex:(NSInteger)idx;

/** Unsets all arguments in the block invocation, including releasing any
objects if argumentsRetained is YES. */
- (void) clearArguments;
- (void)clearArguments;

/** @name Dispatching an Invocation */

Expand All @@ -190,7 +190,7 @@
@see setArgument:atIndex:
@see getReturnValue:
*/
- (void) invoke;
- (void)invoke;

/** Calls the receiver's block with the arguments from the given invocation,
and sets the return value both on the receiving invocation and the given
Expand All @@ -204,6 +204,6 @@
@see setArgument:atIndex:
@see getReturnValue:
*/
- (void) invokeUsingInvocation: (NSInvocation *) inv;
- (void)invokeUsingInvocation:(NSInvocation *)inv;

@end
Loading

0 comments on commit 92bc00e

Please sign in to comment.