Email list hosting service & mailing list manager

The uses of custom ports John Cowan (22 Apr 2020 19:45 UTC)
Re: The uses of custom ports Marc Nieper-Wi├čkirchen (23 Apr 2020 13:07 UTC)
Re: The uses of custom ports John Cowan (23 Apr 2020 14:01 UTC)
Re: The uses of custom ports Marc Feeley (23 Apr 2020 14:25 UTC)
Re: The uses of custom ports Lassi Kortela (23 Apr 2020 14:31 UTC)
Re: The uses of custom ports Marc Feeley (23 Apr 2020 14:43 UTC)
Re: The uses of custom ports Marc Nieper-Wi├čkirchen (23 Apr 2020 14:35 UTC)
Re: The uses of custom ports John Cowan (23 Apr 2020 14:39 UTC)

Re: The uses of custom ports Marc Feeley 23 Apr 2020 14:43 UTC

> On Apr 23, 2020, at 10:31 AM, Lassi Kortela <xxxxxx@lassi.io> wrote:
>
> Does Gambit have the ability to make custom ports from arbitrary closures?
>
> What do these do:
>
> > (apropos "generic")
> "##" namespace:
>  generic-hash, open-file-generic, open-file-generic-from-psettings, open-process-generic, open-string-generic, open-string-pipe-generic, open-u8vector-generic,
>  open-u8vector-pipe-generic, open-vector-generic, open-vector-pipe-generic, write-generic-to-character-port
>

“generic” in those names does not refer to generic ports… rather the common code for several “open” variants, such as open-input-file, open-output-file, open-input-output-file, with-input-from-file, etc that call ##open-file-generic.

No Gambit does not currently support an API to create custom ports.  However, this would be easy to add because the port representation contains a procedure for every method (in the OO sense).

For example, here is the type definition for the port type that is at the base of the inheritance tree:

(define-type port
  id: fe3e988a-c59d-47ce-8592-93b02ce12af1
  type-exhibitor: macro-type-port
  constructor: macro-make-port
  implementer: implement-type-port
  macros:
  prefix: macro-
  opaque:
  unprintable:

  extender: define-type-of-port

  mutex              ;; access to the port is controlled with this mutex

  rkind              ;; port kind for reading (none-port if can't read)
  wkind              ;; port kind for writing (none-port if can't write)

  name               ;; procedure which returns the name of the port

  wait               ;; procedure for waiting until port readable or writable

  close              ;; procedure to close the port

  roptions           ;; options for reading (buffering type, encoding, etc)
  rtimeout           ;; time at which a read that would block times out
  rtimeout-thunk     ;; thunk called when a read timeout occurs
  set-rtimeout       ;; procedure to set rtimeout and rtimeout-thunk

  woptions           ;; options for writing (buffering type, encoding, etc)
  wtimeout           ;; time at which a write that would block times out
  wtimeout-thunk     ;; thunk called when a write timeout occurs
  set-wtimeout       ;; procedure to set wtimeout and wtimeout-thunk

  io-exception-handler ;; procedure to handle I/O exceptions on this port
)

“Object ports” are a subtype of that:

(define-type-of-port object-port
  id: a4ef4750-7ce6-4388-9d5f-48e04bf3ae4b
  type-exhibitor: macro-type-object-port
  constructor: macro-make-object-port
  implementer: implement-type-object-port
  macros:
  prefix: macro-
  opaque:
  unprintable:

  extender: define-type-of-object-port

  read-datum         ;; procedure to read a datum
  write-datum        ;; procedure to write a datum
  newline            ;; procedure to write a datum separator
  force-output       ;; procedure to force output to occur on target device
)

“Character ports” are a subtype of that:

(define-type-of-object-port character-port
  id: a7e0fe95-65e9-4b00-b080-b7e6b12d9c6f
  type-exhibitor: macro-type-character-port
  constructor: macro-make-character-port
  implementer: implement-type-character-port
  macros:
  prefix: macro-
  opaque:
  unprintable:

  extender: define-type-of-character-port

  rbuf               ;; character read buffer (a string)
  rlo                ;; low pointer (start of unread characters)
  rhi                ;; high pointer (end of unread characters)
  rchars             ;; number of characters read at start of read buffer
  rlines             ;; number of lines read up to low pointer
  rcurline           ;; absolute character position where current line starts
  rbuf-fill          ;; procedure to read characters into the read buffer
  peek-eof?          ;; peeking the next character should return end-of-file?

  wbuf               ;; character write buffer (a string)
  wlo                ;; low pointer (start of unwritten characters)
  whi                ;; high pointer (end of unwritten characters)
  wchars             ;; number of characters written at start of write buffer
  wlines             ;; number of lines written up to high pointer
  wcurline           ;; absolute character position where current line starts
  wbuf-drain         ;; procedure to write characters from the write buffer

  input-readtable    ;; readtable for reading
  output-readtable   ;; readtable for writing
  output-width       ;; procedure to get the output width in characters
)

And finally “Byte ports” are a subtype of that:

(define-type-of-character-port byte-port
  id: fe99424c-d1da-48f1-b613-9c735692790e
  type-exhibitor: macro-type-byte-port
  constructor: macro-make-byte-port
  implementer: implement-type-byte-port
  macros:
  prefix: macro-
  opaque:
  unprintable:

  extender: define-type-of-byte-port

  rbuf               ;; byte read buffer (a u8vector)
  rlo                ;; low pointer (start of unread bytes)
  rhi                ;; high pointer (end of unread bytes)
  rbuf-fill          ;; procedure to read bytes into the read buffer

  wbuf               ;; byte write buffer (a u8vector)
  wlo                ;; low pointer (start of unwritten bytes)
  whi                ;; high pointer (end of unwritten bytes)
  wbuf-drain         ;; procedure to write bytes from the write buffer
)

Marc