//
//  main.m
//

#import <Foundation/Foundation.h>
#import <objc/runtime.h>

@interface Foo : NSObject {
    NSString *name_;
}
@end

@implementation Foo
- (id)initWithName:(NSString *)name
{
    self = [super init];
    if (!self) return nil;
    
    name_ = [name copy];
    NSLog(@"Created %@", name_);
    
    return self;
}

- (void)dealloc
{
    NSLog(@"Destroying %@", name_);
    [name_ release];
    [super dealloc];
}
@end

static char key;

int main (int argc, const char * argv[])
{
    NSAutoreleasePool *pool;
    Foo *target;
    Foo *payload;
    Foo *payload2;
    
    pool = [[NSAutoreleasePool alloc] init];
    
    target = [[Foo alloc] initWithName:@"target"];
    payload = [[Foo alloc] initWithName:@"payload"];
    payload2 = [[Foo alloc] initWithName:@"second payload"];
    
    NSLog(@"Associating payload with target");
    objc_setAssociatedObject(target, &key, payload, OBJC_ASSOCIATION_RETAIN);
    
    NSLog(@"Releasing payload");
    [payload release];
    
    NSLog(@"Associating second payload");
    objc_setAssociatedObject(target, &key, payload2, OBJC_ASSOCIATION_RETAIN);
    
    NSLog(@"Releasing second payload");
    [payload2 release];
    
    NSLog(@"Releasing target");
    [target release];
    
    [pool release];
    
    return 0;
}

// Here's the output that this generates:
//
// 2011-12-03 23:03:10.737 MacTest[3953:707] Created target
// 2011-12-03 23:03:10.740 MacTest[3953:707] Created payload
// 2011-12-03 23:03:10.742 MacTest[3953:707] Created second payload
// 2011-12-03 23:03:10.744 MacTest[3953:707] Associating payload with target
// 2011-12-03 23:03:10.745 MacTest[3953:707] Releasing payload
// 2011-12-03 23:03:10.746 MacTest[3953:707] Associating second payload
// 2011-12-03 23:03:10.748 MacTest[3953:707] Destroying payload
// 2011-12-03 23:03:10.749 MacTest[3953:707] Releasing second payload
// 2011-12-03 23:03:10.750 MacTest[3953:707] Releasing target
// 2011-12-03 23:03:10.751 MacTest[3953:707] Destroying target
// 2011-12-03 23:03:10.752 MacTest[3953:707] Destroying second payload
