Refactor device code.
[iolib.git] / io.streams / zeta / device.lisp
blob7913f4fd0ca3c14ea85292215e2953621ae46402
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; indent-tabs-mode: nil -*-
2 ;;;
3 ;;; --- Device common functions.
4 ;;;
6 (in-package :io.zeta-streams)
8 ;;;-----------------------------------------------------------------------------
9 ;;; Device Classes and Types
10 ;;;-----------------------------------------------------------------------------
12 (defclass device ()
13 ((handle :initarg :handle :accessor handle-of)
14 (readable :initarg :readable :accessor device-readablep)
15 (writeable :initarg :writeable :accessor device-writeablep)
16 (seekable :initarg :seekable :accessor device-seekablep)))
18 (defclass direct-device (device) ())
20 (defclass memory-buffer-device (direct-device) ())
23 ;;;-----------------------------------------------------------------------------
24 ;;; Relinquish I/O resources
25 ;;;-----------------------------------------------------------------------------
27 (defgeneric relinquish (device &key abort &allow-other-keys))
30 ;;;-----------------------------------------------------------------------------
31 ;;; Generic functions
32 ;;;-----------------------------------------------------------------------------
34 (defgeneric device-read (device vector start end &optional timeout))
36 (defgeneric device-write (device vector start end &optional timeout))
38 (defgeneric device-position (device))
40 (defgeneric (setf device-position) (position device &optional from))
42 (defgeneric device-length (device))
44 (defgeneric device-poll-input (device &optional timeout))
46 (defgeneric device-poll-output (device &optional timeout))
48 ;;; Internal functions
50 (defgeneric device-open (device &rest initargs))
52 (defgeneric device-read/non-blocking (device vector start end))
54 (defgeneric device-read/timeout (device vector start end timeout))
56 (defgeneric device-write/non-blocking (device vector start end))
58 (defgeneric device-write/timeout (device vector start end timeout))
61 ;;;-----------------------------------------------------------------------------
62 ;;; Helper macros
63 ;;;-----------------------------------------------------------------------------
65 (defmacro with-device ((name) &body body)
66 `(let ((*device* ,name))
67 (declare (special *device*))
68 ,@body))
71 ;;;-----------------------------------------------------------------------------
72 ;;; Default no-op methods
73 ;;;-----------------------------------------------------------------------------
75 (defmethod device-position ((device device))
76 (values nil))
78 (defmethod (setf device-position) (position (device device) &optional from)
79 (declare (ignore position from))
80 (values nil))
82 (defmethod device-length ((device device))
83 (values nil))
86 ;;;-----------------------------------------------------------------------------
87 ;;; Default DEVICE-READ
88 ;;;-----------------------------------------------------------------------------
90 (defmethod device-read :around ((device device) vector start end &optional timeout)
91 (declare (ignore timeout))
92 (if (= start end) 0 (call-next-method)))
94 (defmethod device-read ((device device) vector start end &optional timeout)
95 (if (and timeout (zerop timeout))
96 (device-read/non-blocking device vector start end)
97 (device-read/timeout device vector start end timeout)))
100 ;;;-----------------------------------------------------------------------------
101 ;;; Default DEVICE-WRITE
102 ;;;-----------------------------------------------------------------------------
104 (defmethod device-write :around ((device device) vector start end &optional timeout)
105 (declare (ignore timeout))
106 (if (= start end) 0 (call-next-method)))
108 (defmethod device-write ((device device) vector start end &optional timeout)
109 (if (and timeout (zerop timeout))
110 (device-write/non-blocking device vector start end)
111 (device-write/timeout device vector start end timeout)))