summaryrefslogtreecommitdiffstats
path: root/tests/018/close-delegate.tl
blob: 64a1dc919d14736460d08a4059dec83f40da9b2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
(load "../common")

(defstruct refcount-close stream-wrap
  stream
  (count 1)

  (:method close (me throw-on-error-p)
    (put-line `close called, count @{me.count}`)
    (when (plusp me.count)
      (if (zerop (dec me.count))
        (close-stream me.stream throw-on-error-p)))))

(flow
  (with-stream (s (make-struct-delegate-stream
                    (new refcount-close
                         count 2
                         stream (open-file *load-path*))))
    (get-lines s))
  len
  prinl)

(defstruct refcount-close-alt stream-wrap
  stream
  (count 1)

  (:method close (me throw-on-error-p)
    (put-line `close called, count @{me.count}`)
    (when (plusp me.count)
      (if (zerop (dec me.count))
        (close-stream me.stream throw-on-error-p)
        :))))

(flow
  (with-stream (s (make-struct-delegate-stream
                    (new refcount-close-alt
                         count 2
                         stream (open-file *load-path*))))
    (get-lines s))
  len
  prinl)