mixin.lisp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #+xcvb (module (:depends-on ("package")))
  2. (in-package :trivial-gray-streams)
  3. (defclass trivial-gray-stream-mixin () ())
  4. (defgeneric stream-read-sequence
  5. (stream sequence start end &key &allow-other-keys))
  6. (defgeneric stream-write-sequence
  7. (stream sequence start end &key &allow-other-keys))
  8. (defgeneric stream-file-position (stream))
  9. (defgeneric (setf stream-file-position) (newval stream))
  10. (defmethod stream-write-string
  11. ((stream trivial-gray-stream-mixin) seq &optional start end)
  12. (stream-write-sequence stream seq (or start 0) (or end (length seq))))
  13. ;; Implementations should provide this default method, I believe, but
  14. ;; at least sbcl and allegro don't.
  15. (defmethod stream-terpri ((stream trivial-gray-stream-mixin))
  16. (write-char #\newline stream))
  17. (defmethod stream-file-position ((stream trivial-gray-stream-mixin))
  18. nil)
  19. (defmethod (setf stream-file-position)
  20. (newval (stream trivial-gray-stream-mixin))
  21. (declare (ignore newval))
  22. nil)
  23. #+abcl
  24. (progn
  25. (defmethod gray-streams:stream-read-sequence
  26. ((s trivial-gray-stream-mixin) seq &optional start end)
  27. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  28. (defmethod gray-streams:stream-write-sequence
  29. ((s trivial-gray-stream-mixin) seq &optional start end)
  30. (stream-write-sequence s seq (or start 0) (or end (length seq))))
  31. (defmethod gray-streams:stream-write-string
  32. ((stream xp::xp-structure) string &optional (start 0) (end (length string)))
  33. (xp::write-string+ string stream start end)))
  34. #+allegro
  35. (progn
  36. (defmethod excl:stream-read-sequence
  37. ((s trivial-gray-stream-mixin) seq &optional start end)
  38. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  39. (defmethod stream:stream-write-sequence
  40. ((s trivial-gray-stream-mixin) seq &optional start end)
  41. (stream-write-sequence s seq (or start 0) (or end (length seq)))))
  42. #+cmu
  43. (progn
  44. (defmethod ext:stream-read-sequence
  45. ((s trivial-gray-stream-mixin) seq &optional start end)
  46. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  47. (defmethod ext:stream-write-sequence
  48. ((s trivial-gray-stream-mixin) seq &optional start end)
  49. (stream-write-sequence s seq (or start 0) (or end (length seq)))))
  50. #+lispworks
  51. (progn
  52. (defmethod stream:stream-read-sequence
  53. ((s trivial-gray-stream-mixin) seq start end)
  54. (stream-read-sequence s seq start end))
  55. (defmethod stream:stream-write-sequence
  56. ((s trivial-gray-stream-mixin) seq start end)
  57. (stream-write-sequence s seq start end))
  58. (defmethod stream:stream-file-position ((stream trivial-gray-stream-mixin))
  59. (stream-file-position stream))
  60. (defmethod (setf stream:stream-file-position)
  61. (newval (stream trivial-gray-stream-mixin))
  62. (setf (stream-file-position stream) newval)))
  63. #+openmcl
  64. (progn
  65. (defmethod ccl:stream-read-vector
  66. ((s trivial-gray-stream-mixin) seq start end)
  67. (stream-read-sequence s seq start end))
  68. (defmethod ccl:stream-write-vector
  69. ((s trivial-gray-stream-mixin) seq start end)
  70. (stream-write-sequence s seq start end)))
  71. ;; up to version 2.43 there were no
  72. ;; stream-read-sequence, stream-write-sequence
  73. ;; functions in CLISP
  74. #+clisp
  75. (eval-when (:compile-toplevel :load-toplevel :execute)
  76. (when (find-symbol "STREAM-READ-SEQUENCE" "GRAY")
  77. (pushnew :clisp-has-stream-read/write-sequence *features*)))
  78. #+clisp
  79. (progn
  80. #+clisp-has-stream-read/write-sequence
  81. (defmethod gray:stream-read-sequence
  82. (seq (s trivial-gray-stream-mixin) &key start end)
  83. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  84. #+clisp-has-stream-read/write-sequence
  85. (defmethod gray:stream-write-sequence
  86. (seq (s trivial-gray-stream-mixin) &key start end)
  87. (stream-write-sequence s seq (or start 0) (or end (length seq))))
  88. ;; Even despite the stream-read/write-sequence are present in newer
  89. ;; CLISP, it's better to provide stream-(read/write)-(byte/char)-sequence
  90. ;; methods too.
  91. ;; Example: if fundamental-binary-input-stream comes in the
  92. ;; class precedence list of your user-defined stream before
  93. ;; the trivial-gray-steam-mixin, the default CLISP's implementation
  94. ;; of the gray:stream-read-sequence will be used; and this default
  95. ;; implementation calls the gray:stream-read-byte-sequence.
  96. ;; Therefore we override gray:stream-read-byte-sequence and call
  97. ;; our stream-read-sequence.
  98. (defmethod gray:stream-read-byte-sequence
  99. ((s trivial-gray-stream-mixin)
  100. seq
  101. &optional start end no-hang interactive)
  102. (when no-hang
  103. (error "this stream does not support the NO-HANG argument"))
  104. (when interactive
  105. (error "this stream does not support the INTERACTIVE argument"))
  106. (stream-read-sequence s seq start end))
  107. (defmethod gray:stream-write-byte-sequence
  108. ((s trivial-gray-stream-mixin)
  109. seq
  110. &optional start end no-hang interactive)
  111. (when no-hang
  112. (error "this stream does not support the NO-HANG argument"))
  113. (when interactive
  114. (error "this stream does not support the INTERACTIVE argument"))
  115. (stream-write-sequence s seq start end))
  116. (defmethod gray:stream-read-char-sequence
  117. ((s trivial-gray-stream-mixin) seq &optional start end)
  118. (stream-read-sequence s seq start end))
  119. (defmethod gray:stream-write-char-sequence
  120. ((s trivial-gray-stream-mixin) seq &optional start end)
  121. (stream-write-sequence s seq start end))
  122. (defmethod gray:stream-position ((stream trivial-gray-stream-mixin) position)
  123. (if position
  124. (setf (stream-file-position stream) position)
  125. (stream-file-position stream))))
  126. #+sbcl
  127. (progn
  128. (defmethod sb-gray:stream-read-sequence
  129. ((s trivial-gray-stream-mixin) seq &optional start end)
  130. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  131. (defmethod sb-gray:stream-write-sequence
  132. ((s trivial-gray-stream-mixin) seq &optional start end)
  133. (stream-write-sequence s seq (or start 0) (or end (length seq))))
  134. (defmethod sb-gray:stream-file-position
  135. ((stream trivial-gray-stream-mixin) &optional position)
  136. (if position
  137. (setf (stream-file-position stream) position)
  138. (stream-file-position stream)))
  139. ;; SBCL extension:
  140. (defmethod sb-gray:stream-line-length ((stream trivial-gray-stream-mixin))
  141. 80))
  142. #+ecl
  143. (progn
  144. (defmethod gray:stream-read-sequence
  145. ((s trivial-gray-stream-mixin) seq &optional start end)
  146. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  147. (defmethod gray:stream-write-sequence
  148. ((s trivial-gray-stream-mixin) seq &optional start end)
  149. (stream-write-sequence s seq (or start 0) (or end (length seq)))))