mixin.lisp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. (defmethod excl::stream-file-position
  43. ((stream trivial-gray-stream-mixin) &optional position)
  44. (if position
  45. (setf (stream-file-position stream) position)
  46. (stream-file-position stream))))
  47. #+cmu
  48. (progn
  49. (defmethod ext:stream-read-sequence
  50. ((s trivial-gray-stream-mixin) seq &optional start end)
  51. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  52. (defmethod ext:stream-write-sequence
  53. ((s trivial-gray-stream-mixin) seq &optional start end)
  54. (stream-write-sequence s seq (or start 0) (or end (length seq)))))
  55. #+lispworks
  56. (progn
  57. (defmethod stream:stream-read-sequence
  58. ((s trivial-gray-stream-mixin) seq start end)
  59. (stream-read-sequence s seq start end))
  60. (defmethod stream:stream-write-sequence
  61. ((s trivial-gray-stream-mixin) seq start end)
  62. (stream-write-sequence s seq start end))
  63. (defmethod stream:stream-file-position ((stream trivial-gray-stream-mixin))
  64. (stream-file-position stream))
  65. (defmethod (setf stream:stream-file-position)
  66. (newval (stream trivial-gray-stream-mixin))
  67. (setf (stream-file-position stream) newval)))
  68. #+openmcl
  69. (progn
  70. (defmethod ccl:stream-read-vector
  71. ((s trivial-gray-stream-mixin) seq start end)
  72. (stream-read-sequence s seq start end))
  73. (defmethod ccl:stream-write-vector
  74. ((s trivial-gray-stream-mixin) seq start end)
  75. (stream-write-sequence s seq start end)))
  76. ;; up to version 2.43 there were no
  77. ;; stream-read-sequence, stream-write-sequence
  78. ;; functions in CLISP
  79. #+clisp
  80. (eval-when (:compile-toplevel :load-toplevel :execute)
  81. (when (find-symbol "STREAM-READ-SEQUENCE" "GRAY")
  82. (pushnew :clisp-has-stream-read/write-sequence *features*)))
  83. #+clisp
  84. (progn
  85. #+clisp-has-stream-read/write-sequence
  86. (defmethod gray:stream-read-sequence
  87. (seq (s trivial-gray-stream-mixin) &key start end)
  88. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  89. #+clisp-has-stream-read/write-sequence
  90. (defmethod gray:stream-write-sequence
  91. (seq (s trivial-gray-stream-mixin) &key start end)
  92. (stream-write-sequence s seq (or start 0) (or end (length seq))))
  93. ;; Even despite the stream-read/write-sequence are present in newer
  94. ;; CLISP, it's better to provide stream-(read/write)-(byte/char)-sequence
  95. ;; methods too.
  96. ;; Example: if fundamental-binary-input-stream comes in the
  97. ;; class precedence list of your user-defined stream before
  98. ;; the trivial-gray-steam-mixin, the default CLISP's implementation
  99. ;; of the gray:stream-read-sequence will be used; and this default
  100. ;; implementation calls the gray:stream-read-byte-sequence.
  101. ;; Therefore we override gray:stream-read-byte-sequence and call
  102. ;; our stream-read-sequence.
  103. (defmethod gray:stream-read-byte-sequence
  104. ((s trivial-gray-stream-mixin)
  105. seq
  106. &optional start end no-hang interactive)
  107. (when no-hang
  108. (error "this stream does not support the NO-HANG argument"))
  109. (when interactive
  110. (error "this stream does not support the INTERACTIVE argument"))
  111. (stream-read-sequence s seq start end))
  112. (defmethod gray:stream-write-byte-sequence
  113. ((s trivial-gray-stream-mixin)
  114. seq
  115. &optional start end no-hang interactive)
  116. (when no-hang
  117. (error "this stream does not support the NO-HANG argument"))
  118. (when interactive
  119. (error "this stream does not support the INTERACTIVE argument"))
  120. (stream-write-sequence s seq start end))
  121. (defmethod gray:stream-read-char-sequence
  122. ((s trivial-gray-stream-mixin) seq &optional start end)
  123. (stream-read-sequence s seq start end))
  124. (defmethod gray:stream-write-char-sequence
  125. ((s trivial-gray-stream-mixin) seq &optional start end)
  126. (stream-write-sequence s seq start end))
  127. (defmethod gray:stream-position ((stream trivial-gray-stream-mixin) position)
  128. (if position
  129. (setf (stream-file-position stream) position)
  130. (stream-file-position stream))))
  131. #+sbcl
  132. (progn
  133. (defmethod sb-gray:stream-read-sequence
  134. ((s trivial-gray-stream-mixin) seq &optional start end)
  135. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  136. (defmethod sb-gray:stream-write-sequence
  137. ((s trivial-gray-stream-mixin) seq &optional start end)
  138. (stream-write-sequence s seq (or start 0) (or end (length seq))))
  139. (defmethod sb-gray:stream-file-position
  140. ((stream trivial-gray-stream-mixin) &optional position)
  141. (if position
  142. (setf (stream-file-position stream) position)
  143. (stream-file-position stream)))
  144. ;; SBCL extension:
  145. (defmethod sb-gray:stream-line-length ((stream trivial-gray-stream-mixin))
  146. 80))
  147. #+ecl
  148. (progn
  149. (defmethod gray:stream-read-sequence
  150. ((s trivial-gray-stream-mixin) seq &optional start end)
  151. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  152. (defmethod gray:stream-write-sequence
  153. ((s trivial-gray-stream-mixin) seq &optional start end)
  154. (stream-write-sequence s seq (or start 0) (or end (length seq)))))