This commit is contained in:
Ingy döt Net 2013-06-05 21:47:54 +00:00
parent 1f1ad49427
commit 6f050a029e
2496 changed files with 37609 additions and 3031 deletions

View file

@ -0,0 +1,42 @@
text
thing(void)
{
return "delegate implementation";
}
text
operation(record delegator)
{
text s;
if (r_key(delegator, "delegate")) {
if (r_key(r_query(delegator, "delegate"), "thing")) {
s = __text(call(r_query(r_query(delegator, "delegate"), "thing")));
} else {
s = "default implementation";
}
} else {
s = "default implementation";
}
return s;
}
integer
main(void)
{
record delegate, delegator;
o_text(operation(delegator));
o_byte('\n');
r_link(delegator, "delegate", delegate);
o_text(operation(delegator));
o_byte('\n');
r_put(delegate, "thing", thing);
o_text(operation(delegator));
o_byte('\n');
return 0;
}

View file

@ -0,0 +1,76 @@
#import <Foundation/Foundation.h>
@interface Delegator : NSObject {
id delegate;
}
- (id)delegate;
- (void)setDelegate:(id)obj;
- (NSString *)operation;
@end
@implementation Delegator
- (id)delegate {
return delegate;
}
- (void)setDelegate:(id)obj {
delegate = obj; // Weak reference
}
- (NSString *)operation {
if ([delegate respondsToSelector:@selector(thing)])
return [delegate thing];
return @"default implementation";
}
@end
// Any object may implement these
@interface NSObject (DelegatorDelegating)
- (NSString *)thing;
@end
@interface Delegate : NSObject
// Don't need to declare -thing because any NSObject has this method
@end
@implementation Delegate
- (NSString *)thing {
return @"delegate implementation";
}
@end
// Example usage
// Memory management ignored for simplification
int main() {
// Without a delegate:
Delegator *a = [[Delegator alloc] init];
NSLog(@"%d\n", [[a operation] isEqualToString:@"default implementation"]);
// With a delegate that does not implement thing:
[a setDelegate:@"A delegate may be any object"];
NSLog(@"%d\n", [[a operation] isEqualToString:@"delegate implementation"]);
// With a delegate that implements "thing":
Delegate *d = [[Delegate alloc] init];
[a setDelegate:d];
NSLog(@"%d\n", [[a operation] isEqualToString:@"delegate implementation"]);
return 0;
}

View file

@ -0,0 +1,45 @@
#import <Foundation/Foundation.h>
// Formal protocol for the delegate
@protocol DelegatorDelegatingProtocol
- (NSString *)thing;
@end
@interface Delegator : NSObject
@property (weak) id delegate;
- (NSString *)operation;
@end
@implementation Delegator
- (NSString *)operation {
if ([self.delegate respondsToSelector: @selector(thing)])
return [self.delegate thing];
return @"default implementation";
}
@end
@interface Delegate : NSObject
<DelegatorDelegatingProtocol>
@end
@implementation Delegate
- (NSString *)thing { return @"delegate implementation"; }
@end
// Example usage with Automatic Reference Counting
int main() {
@autoreleasepool {
// Without a delegate:
Delegator *a = [Delegator new];
NSLog(@"%@", [a operation]); // prints "default implementation"
// With a delegate that does not implement thing:
a.delegate = @"A delegate may be any object";
NSLog(@"%@", [a operation]); // prints "default implementation"
// With a delegate that implements "thing":
Delegate *d = [Delegate new];
a.delegate = d;
NSLog(@"%@", [a operation]); // prints "delegate implementation"
}
return 0;
}