TAAE2: non-locking AEManagedValue

sketch:

@interface AECapturedValue : NSObject
{
    void *_ptr;
}
@property (nonatomic, assign) UInt32 flags;
@property (nonatomic) AECapturedValue *previousValue;
@end

@implementation AECapturedValue

- (void) setPtr:(void *)ptr
{
    if (_ptr != ptr)
    {
        if (_ptr != nil)
        {
            //release depending on type
        }

        _ptr = ptr;
    }
}

- (void) dealloc
{
    // ensure proper release of non-objects
    self.ptr = nil;
}


void *AECapturedValueGetPtr(void *objectPtr)
{
    __unsafe_unretained AECapturedValue *object =
    (__bridge __unsafe_unretained AECapturedValue *)objectPtr;

    // indicate that we are now using this value
    object->_flags |= 1;
    return object->_ptr;
}

- (void) purge
{
    // purge previous value if possible
if (self.flags | 0x01)
    { self.previousValue = nil; }

// otherwise propagate purge message down the chain
    [self.previousValue purge];
}

@end



@interface AEDynamicValue : NSObject
{
    AECapturedValue *mValue;
}
@end

@implementation AEDynamicValue

- (void) setPtr:(void *)ptr
{
    AECapturedValue *value = [AECapturedValue new];
    value.ptr = ptr;
    [self setValue:value];
    [self purge];
}

- (void) setValue:(AECapturedValue *)value
{
    value.previousValue = mValue;
    mValue = value;
}

- (void) purge
{
    [mValue purge];
}

void *AEDynamicValueGetPtr(void *objectPtr)
{
    __unsafe_unretained AEDynamicValue *object =
    (__bridge __unsafe_unretained AEDynamicValue *)objectPtr;
    return AECapturedValueGetPtr((__bridge void *)object->mValue);
}

@end

Comments

Sign In or Register to comment.