]> gitweb.factorcode.org Git - factor.git/blob - basis/io/streams/duplex/duplex.factor
Fix conflict in images vocab
[factor.git] / basis / io / streams / duplex / duplex.factor
1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel continuations destructors io io.encodings
4 io.encodings.private io.timeouts io.ports io.styles summary
5 accessors delegate delegate.protocols ;
6 IN: io.streams.duplex
7
8 TUPLE: duplex-stream in out ;
9
10 C: <duplex-stream> duplex-stream
11
12 CONSULT: input-stream-protocol duplex-stream in>> ;
13 CONSULT: output-stream-protocol duplex-stream out>> ;
14 CONSULT: formatted-output-stream-protocol duplex-stream out>> ;
15
16 : >duplex-stream< ( stream -- in out ) [ in>> ] [ out>> ] bi ; inline
17
18 M: duplex-stream set-timeout
19     >duplex-stream< [ set-timeout ] bi-curry@ bi ;
20
21 M: duplex-stream dispose
22     #! The output stream is closed first, in case both streams
23     #! are attached to the same file descriptor, the output
24     #! buffer needs to be flushed before we close the fd.
25     [ >duplex-stream< [ &dispose drop ] bi@ ] with-destructors ;
26
27 : <encoder-duplex> ( stream-in stream-out encoding -- duplex )
28     [ re-decode ] [ re-encode ] bi-curry bi* <duplex-stream> ;
29
30 : with-stream* ( stream quot -- )
31     [ >duplex-stream< ] dip with-streams* ; inline
32
33 : with-stream ( stream quot -- )
34     [ >duplex-stream< ] dip with-streams ; inline
35
36 ERROR: invalid-duplex-stream ;
37
38 M: duplex-stream underlying-handle
39     >duplex-stream<
40     [ underlying-handle ] bi@
41     [ = [ invalid-duplex-stream ] when ] keep ;
42