Next Previous Up Top Contents Index

15.2 An illustrative example of user defined streams

15.2.3 Stream directionality

Streams can be defined for input only, output only, or both. In our example, theunicode-ls-stream class needs to be able to read from a file and write to a file, and we therefore defined it to inherit from an input and an output stream class. We could have defined disjoint classes instead, one inheriting fromfundamental-character-input-stream and the other fromfundamental-character-output-stream. This would have allowed us to rely on the default methods for the direction predicates. However, given that we have defined one bi-directional stream class, we must define our own methods for the direction predicates.

(defmethod input-stream-p ((stream unicode-ls-stream))
  (input-stream-p (ls-stream-file-stream stream)))

(defmethod output-stream-p ((stream unicode-ls-stream)) (output-stream-p (ls-stream-file-stream stream)))

The above code allows us to "trampoline" the correct direction predicate functionality fromfile-stream, using thels-stream-file-stream accessor we defined previously.

input-stream-p

Generic Function

output-stream-p

Generic Function

input-stream-p stream

output-stream-p stream

Package:common-lisp
The predicatesinput-stream-p andoutput-stream-p are implemented as generic functions. Their default methods returnt if stream is respectively an input or output stream. If the user wants to implement a stream with no inherent directionality (and thus does not includefundamental-input-stream orfundamental-output-stream) but for which the directionality depends on the instance, then suitable methods should be provided.

LispWorks User Guide - 14 Oct 1998

Next Previous Up Top Contents Index

Generated with Harlequin WebMaker