CoreFoundation dual-mode code macros

Since I’m doing some work on AquaticPrime using SecIdentityRefs, SecKeyRefs, etc, and since I want it to be compatible with either Garbage Collection or manual memory management, I need to handle CoreFoundation objects properly in both cases. It’s not difficult to do, but it lends itself to some nice syntactic sugar, which I’ve chosen to implement as the following macros. Hopefully these will prove useful to others as well.

// Use this when storing a passed-in object (i.e. one you haven't retained/created)
#define CFKeep(cf) CFMakeCollectable( CFRetain(cf) )

// Use this when you'll return something you've allocated/copied just to return
#define CFMakeReturnable(cf) (void) [NSMakeCollectable(cf) autorelease]

You’ll use the two items like this:

- (id) initWithIdentity: (SecIdentityRef) identity
{
    self = [super init];
    if ( self == nil )
        return ( nil );

    signingIdentity = CFKeep(identity);

    return ( self );
}

- (SecKeyRef) privateKey
{
    SecKeyRef privKey = NULL;
    OSStatus err = SecIdentityCopyPrivateKey( signingIdentity, &privKey );
    if ( err != errAuthorizationSuccess )
        return ( NULL );

    CFMakeReturnable(privKey);
    return ( privKey );
}

© 2009-2019. All rights reserved.

Powered by Hydejack v9.1.6