From: Frank Buss
Subject: portable way to define new stream classes?
Date: 
Message-ID: <cvbba1$6qn$1@newsreader2.netcologne.de>
I use a framework, which expects streams. But sometimes I need
in-memory streams, because I don't want to create temporary files.
In Lispworks I can define my own stream classes (see below), but how can
I implement this in Common Lisp?

I have the source code of the framework and can change it. One idea
would be to use a stream-wrapper class, which defines my-write-byte and
my-read-byte methods and derived classes for my memory streams and a
derived class for delegating it to a system stream, but this doesn't
sound very elegant.


(defclass byte-output-stream (stream:fundamental-character-output-stream)
  ((content :initform (make-array 0 
                                  :element-type '(unsigned-byte 8) 
                                  :adjustable t 
                                  :fill-pointer t)))
  (:documentation
   "A byte output stream, which writes to an adjustable array."))

(defmethod stream:stream-write-byte ((self byte-output-stream) byte)
  (vector-push-extend byte (slot-value self 'content)))

(defmethod stream-element-type ((stream byte-output-stream))  '(unsigned-byte 8))


(defclass byte-input-stream (stream:fundamental-character-input-stream)
  ((content :initarg :content)
   (position :initform 0))
  (:documentation
   "A byte input stream, which reads from an array."))

(defmethod stream:stream-read-byte ((self byte-input-stream))
  (with-slots (content position) self
    (let ((result (aref content position)))
      (incf position)
      result)))

(defmethod stream-element-type ((stream byte-input-stream))  '(unsigned-byte 8))


-- 
Frank Bu�, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de

From: Steven M. Haflich
Subject: Re: portable way to define new stream classes?
Date: 
Message-ID: <KHdSd.5681$DC6.5245@newssvr14.news.prodigy.com>
Frank Buss wrote:

> I use a framework, which expects streams. But sometimes I need
> in-memory streams, because I don't want to create temporary files.
> In Lispworks I can define my own stream classes (see below), but how can
> I implement this in Common Lisp?

Most implementations provide a way customize streams, including defining
new suibclasses of stream, because they find this capability necessary
themselves.  But to answer your question strictly, there is no way you
can define customized stream classes in portable Common Lisp.  The class
stream might in some implementation be of metaclass built-in-class,
and there is no portable way to customize a built-in-class.

Encapsulation can nearly solve the problem, as you suggest, but the
solution could be tedious.  You probably won't be able to pass your
encapsulated stream object to other portable code that might examine
the stream object, since it won't satisfy the streamp predicate.
Also there are a lot of places where streams have extraneous semantics,
such as when a stream is passed to the pathname function.
From: Steven E. Harris
Subject: Re: portable way to define new stream classes?
Date: 
Message-ID: <jk4650loflf.fsf@W003275.na.alarismed.com>
Frank Buss <··@frank-buss.de> writes:

> how can I implement this in Common Lisp?

I'm not sure if you're looking for something straight out of standard
Common Lisp, or if something de facto would be close enough. Have you
considered Gray Streams�?


Footnotes: 
� http://www.franz.com/support/documentation/6.2/doc/gray-streams.htm
  http://clisp.cons.org/impnotes/clos-stream.html#gray
  http://groups-beta.google.com/group/comp.lang.lisp/browse_frm/thread/333b7f569cd11abd/ad93f0bea2d15701

-- 
Steven E. Harris