--- parser3/src/main/pa_pool.C 2001/01/30 11:51:07 1.4 +++ parser3/src/main/pa_pool.C 2003/11/20 16:34:26 1.62 @@ -1,33 +1,57 @@ -/* - $Id: pa_pool.C,v 1.4 2001/01/30 11:51:07 paf Exp $ +/** @file + Parser: pool class. + + Copyright (c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com) + Author: Alexandr Petrosian (http://paf.design.ru) */ -#include +static const char * const IDENT_POOL_C="$Date: 2003/11/20 16:34:26 $"; #include "pa_pool.h" +#include "pa_exception.h" +#include "pa_common.h" +#include "pa_sapi.h" +#include "pa_charset.h" + +// Pool + +Pool::Pool(){} + +static void cleanup(Pool::Cleanup item, int) { + if(item.cleanup) + item.cleanup(item.data); +} +Pool::~Pool() { + //__asm__("int3"); + //_asm int 3; + //fprintf(stderr, "cleanups: %d\n", cleanups.size()); + // cleanups first, because they use some object's memory pointers + cleanups.for_each(cleanup, 0); +} -Pool::Pool() : - fglobal_exception(0), - flocal_exception(0) { +void Pool::register_cleanup(void (*cleanup) (void *), void *data) { + cleanups+=Cleanup(cleanup, data); } -Pool::~Pool() { +static void unregister_cleanup(Pool::Cleanup& item, void* cleanup_data) { + if(item.data==cleanup_data) + item.cleanup=0; } +void Pool::unregister_cleanup(void *cleanup_data) { + cleanups.for_each_ref(::unregister_cleanup, cleanup_data); +} + +// Pooled -Exception *Pool::set_global_exception(Exception *e) { - Exception *r=fglobal_exception; - fglobal_exception=e; - return r; -} -void Pool::restore_global_exception(Exception *e) { - fglobal_exception=e; -} - -Exception *Pool::set_local_exception(Exception *e) { - Exception *r=flocal_exception; - flocal_exception=e; - return r; +static void cleanup(void *data) { + static_cast(data)->~Pooled(); } -void Pool::restore_local_exception(Exception *e) { - flocal_exception=e; + +Pooled::Pooled(Pool& apool): fpool(apool) { + fpool.register_cleanup(cleanup, this); +} + +/// Sole: this got called automatically from Pool::~Pool() +Pooled::~Pooled() { + fpool.unregister_cleanup(this); }