portable way to define new stream classes?

From: Frank Buss (fb_at_frank-buss.de)
Date: 02/21/05

  • Next message: Mike Thomas: "Re: CLOS / MOP package name"
    Date: Mon, 21 Feb 2005 00:51:13 +0000 (UTC)
    
    

    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ß, fb@frank-buss.de
    http://www.frank-buss.de, http://www.it4-systems.de
    

  • Next message: Mike Thomas: "Re: CLOS / MOP package name"

    Relevant Pages

    • I need help debugging an MFC OCX
      ... void CFlintCtrl::DoPropExchange(CPropExchange* pPX) ... This routine is called by the framework and always sets m_bReadAhead to ... Where does this stream come from? ...
      (microsoft.public.win32.programmer.ole)
    • Re: Is there any iterator-like constructs for Streams?
      ... > IEnumerator and IEnumerable interfaces which allow for more then one ... > in the framework that wraps a stream in such a way that the stream position ... don't believe it's provided in the framework. ...
      (microsoft.public.dotnet.languages.csharp)
    • Dynamically add soap headers in Compact Framework
      ... collection in BeforeSerialize and then they will be serialized to the stream ... in AfterSerialize. ... Framework it doesn't seem to work. ...
      (microsoft.public.dotnet.framework.webservices)
    • Re: a question about overloading operator<<
      ... >> and 'imbue' the stream with the resulting object. ... stream and have the num_put facet obtain the formatting information ... framework to make it easier (and boost may have one already, ...
      (comp.lang.cpp)
    • Re: Writing listbox items into text file
      ... Check the Stream Classes and their derivates ... classes instead of using Stream Writer's "writeline" method. ... If append is true, previous + current items are saved into text file. ... i want to save what and how i see in listbox items as text ...
      (microsoft.public.dotnet.languages.vb)