AutoDeletedFilename.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Fri, 24 May 2013 18:52:05 +0100
branchjv
changeset 18060 3708e12e9aa8
parent 18011 deb0c3355881
parent 15247 be9fd0956f80
child 18071 009cf668b0ed
permissions -rw-r--r--
Merged b882507b9fdf and 9a8afa1b41cb (branch default - CVS HEAD)

"
 COPYRIGHT (c) 2007 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
"{ Package: 'stx:libbasic' }"

Filename subclass:#AutoDeletedFilename
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'System-Support'
!

!AutoDeletedFilename class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2007 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

documentation
"
    Used with temporary files - these will automatically delete themself,
    when no longer referenced.
    See -> Filename asAutoDeletedFilename
"
!

examples
"
    the following file will be automatically deleted after some time:
                                                    [exBegin]
    |f p|

    f := AutoDeletedFilename newTemporary.
    f writeStream
        nextPutLine:'hello';
        close.
    p := f pathName.
    Transcript showCR:p.
    f := f asAutoDeletedFilename.
    self assert:(p asFilename exists).
    ObjectMemory collectGarbage.
    Delay waitForSeconds:2.
    self assert:(p asFilename exists).
    f := nil.
    ObjectMemory collectGarbage.
    Delay waitForSeconds:2.
    self assert:(p asFilename exists not).
                                                    [exEnd]


    you can also delete it manually:
                                                    [exBegin]
    |f p|

    f := Filename newTemporary.
    f writeStream
        nextPutLine:'hello';
        close.
    p := f pathName.
    Transcript showCR:p.
    f := f asAutoDeletedFilename.
    self assert:(p asFilename exists).
    ObjectMemory collectGarbage.
    Delay waitForSeconds:2.
    self assert:(p asFilename exists).
    f remove.
    f := nil.
    ObjectMemory collectGarbage.
    Delay waitForSeconds:2.
    self assert:(p asFilename exists not).
                                                    [exEnd]
"
! !

!AutoDeletedFilename methodsFor:'accessing'!

finalize
"/    Transcript showCR:'AutoDeletedFilename: deleting ', self pathName.

    |linkInfo|

    linkInfo := self linkInfo.
    linkInfo notNil ifTrue:[
        linkInfo isDirectory ifTrue:[
            super recursiveRemove
        ] ifFalse:[
            super removeFile.
        ].
    ].
!

setName:aString
    super setName:aString.
    self registerForFinalization    
! !

!AutoDeletedFilename methodsFor:'removing'!

recursiveRemove
    super recursiveRemove.
    self unregisterForFinalization
!

remove
    super remove.
    self unregisterForFinalization
!

removeDirectory
    super removeDirectory.
    self unregisterForFinalization
!

removeFile
    super removeFile.
    self unregisterForFinalization
! !

!AutoDeletedFilename class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic/AutoDeletedFilename.st,v 1.9 2013-05-15 10:25:11 stefan Exp $'
! !