1 ! Copyright (C) 2007, 2008 Doug Coleman, Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors continuations kernel namespaces make
7 TUPLE: disposable disposed ;
9 GENERIC: dispose* ( disposable -- )
11 ERROR: already-disposed disposable ;
13 : check-disposed ( disposable -- )
14 dup disposed>> [ already-disposed ] [ drop ] if ; inline
16 GENERIC: dispose ( disposable -- )
19 dup disposed>> [ drop ] [ t >>disposed dispose* ] if ;
21 : dispose-each ( seq -- )
23 [ [ dispose ] curry [ , ] recover ] each
24 ] { } make [ peek rethrow ] unless-empty ;
26 : with-disposal ( object quot -- )
27 over [ dispose ] curry [ ] cleanup ; inline
31 SYMBOL: always-destructors
33 SYMBOL: error-destructors
35 : do-always-destructors ( -- )
36 always-destructors get <reversed> dispose-each ;
38 : do-error-destructors ( -- )
39 error-destructors get <reversed> dispose-each ;
43 : &dispose ( disposable -- disposable )
44 dup always-destructors get push ; inline
46 : |dispose ( disposable -- disposable )
47 dup error-destructors get push ; inline
49 : with-destructors ( quot -- )
51 V{ } clone always-destructors set
52 V{ } clone error-destructors set
53 [ do-always-destructors ]
54 [ do-error-destructors ]