/* UnwindProtect.m * * Copyright Niels Möller 1995 * * Freely distributable under the terms and conditions of the * GNU General Public License. */ #include /* I'm not sure if I should hardcode StackFrame here or not. */ #ifndef FRAME_STACK #define FRAME_STACK [self class] #endif FRAME_STACK @implementation UnwindProtect - init { [ [self initDontFree] cleanupBySending: @selector(rawFree) to: self]; return self; } - initDontFree { [super init]; frame = NULL; return self; } - cleanupByCalling: ( void (*)(void)) function { frame_id newFrame = [FRAME_STACK pushProtectCall: function]; if (!frame) frame = newFrame; return self; } - cleanupBySending: (SEL) message to: rec { frame_id newFrame = [FRAME_STACK pushProtect: rec send: message]; if (!frame) frame = newFrame; return self; } - (void) cleanup { /* This cleanup may send me the free message, so copy the * instance variable first. */ frame_id oldFrame = frame; frame = NULL; if (oldFrame) { [FRAME_STACK unwind: oldFrame]; [FRAME_STACK freeFrame: oldFrame]; } } @end /* UnwindProtect */