From Modula-2 Reloaded

WiP: IO Handlers For Generic READ And WRITE

One of the design objectives is to provide a universal I/O facility through two pervasive generic macros READ and WRITE.

A possible way to implement this facility would be to maintain a table of pointers to R/W handler procedures in the runtime system. At compile time, any invocation of READ or WRITE is translated into a respective call into the R/W handler table. A procedure that allows the installation of I/O procedures as handlers is provided either by module SYSTEM or by a new system module.

DEFINITION MODULE M2RTS;

(* Modula-2 Run Time System Interface *)

TYPE
    Builtin = ( BOOLEAN, CHAR, UNICHAR, CARDINAL, LONGCARD, INTEGER, LONGINT, REAL, LONGREAL );

    ReadHandler = PROCEDURE ( File; VAR <PervasiveType> );

    WriteHandler = PROCEDURE ( File; <PervasiveType> );


PROCEDURE InstallRWHandlers( type : Builtin;
                               rh : ReadHandler;
                               wh : WriteHandler );

END M2RTS.

A library that implements I/O procedures then imports the handler installation procedure and installs its I/O procedures as handlers in the runtime system's handler table.

IMPLEMENTATION MODULE PervasiveIO;

FROM M2RTS IMPORT Builtin, InstallRWHandlers;

(* implementation of I/O procedures: ReadBool, WriteBool,
   ReadChar, WriteChar, ReadUChar, WriteUChar, ReadCard, WriteCard,
   ReadInt, WriteInt, ReadLInt, WriteLInt, ReadReal, WriteLReal *)

(* installing I/O procedures as R/W handlers *)
BEGIN
    InstallRWHandlers(Builtin.BOOLEAN, ReadBool, WriteBool);
    InstallRWHandlers(Builtin.CHAR, ReadChar, WriteChar);
    InstallRWHandlers(Builtin.UNICHAR, ReadUChar, WriteUChar);
    InstallRWHandlers(Builtin.CARDINAL, ReadCard, WriteCard);
    InstallRWHandlers(Builtin.LONGCARD, ReadLCard, WriteLCard);
    InstallRWHandlers(Builtin.INTEGER, ReadInt, WriteInt);
    InstallRWHandlers(Builtin.LONGINT, ReadLInt, WriteLInt);
    InstallRWHandlers(Builtin.REAL, ReadReal, WriteReal);
    InstallRWHandlers(Builtin.LONGREAL, ReadLReal, WriteLReal);
END PervasiveIO.
Retrieved from http://modula-2.net/m2r10/pmwiki.php?n=WiP.IOHandlersForGenericREADAndWRITE
Page last modified on 2010-01-13 18:01