Use keyword instead of optional args for device functions.
[iolib.git] / io.streams / zeta / device.lisp
blobcca85b32c62400c85fc4b6e8a268aca1bae00147
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 &key start end timeout))
36 (defgeneric device-write (device vector &key start end 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 &key timeout))
46 (defgeneric device-poll-output (device &key 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 &key (start 0) end timeout)
91 (check-bounds vector start end)
92 (if (= start end)
94 (call-next-method device vector :start start :end end :timeout timeout)))
96 (defmethod device-read ((device device) vector &key start end timeout)
97 (if (and timeout (zerop timeout))
98 (device-read/non-blocking device vector start end)
99 (device-read/timeout device vector start end timeout)))
102 ;;;-----------------------------------------------------------------------------
103 ;;; Default DEVICE-WRITE
104 ;;;-----------------------------------------------------------------------------
106 (defmethod device-write :around ((device device) vector &key start end timeout)
107 (check-bounds vector start end)
108 (if (= start end)
110 (call-next-method device vector :start start :end end :timeout timeout)))
112 (defmethod device-write ((device device) vector &key start end timeout)
113 (if (and timeout (zerop timeout))
114 (device-write/non-blocking device vector start end)
115 (device-write/timeout device vector start end timeout)))