Enhanced SYSTEM.VAL - Serialization example

Topics related to the use of Oberon language features
Locked
cfbsoftware
Site Admin
Posts: 405
Joined: Fri Dec 31, 2010 12:30 pm
Contact:

Enhanced SYSTEM.VAL - Serialization example

Post by cfbsoftware » Sun Sep 28, 2014 1:41 am

A subtle but significant enhancement to the SYSTEM.VAL function was introduced in v5.1 Astrobe. The use of the System.VAL type-transfer function is no longer restricted to using basic types.

Previously the definition of SYSTEM.VAL was:

Code: Select all

PROCEDURE VAL*(typeName: <any basic type>; x: <any basic type or pointer>): typeName;
The definition of SYSTEM.VAL is now:

Code: Select all

PROCEDURE VAL*(typeName <any type>; x: <any type>): typeName;
The only limitation is that the size in bytes of the destination type must be greater than or equal to the size of the source type.

This Oberon enhancement was initially introduced in the Project Oberon 2014 compiler and operating system implemented by Niklaus Wirth and Paul Reed on a Xilinx FPGA development board.

Consequently, SYSTEM.VAL is now very useful for performing serialization i.e. transforming complex data structures into byte-streams and vice-versa. For example, in Project Oberon 2014 it is used to convert the internal filesystem directory structures and file headers to the raw sectors stored on a MicroSDHC card used for disk storage. The following is an extract from the Project Oberon file system type declarations:

Code: Select all

MODULE FileDir;
TYPE
    FileHeader* =
      RECORD (*first page of each file on disk*)
        mark*: INTEGER;
        name*: FileName;
        aleng*, bleng*, date*: INTEGER;
        ext*:  ExtensionTable;
        sec*: SectorTable;
        fill: ARRAY SectorSize - HeaderSize OF BYTE;
      END ;

   DataSector* = ARRAY SectorSize OF BYTE;

Code: Select all

MODULE Files;
TYPE
    BufferRecord =
      RECORD apos, lim: INTEGER;
        mod: BOOLEAN;
        next: Buffer;
        data: FileDir.DataSector
      END ;

   Buffer  = POINTER TO BufferRecord;
Using these data types the following statement is all that is required to convert the data from the array of bytes as it appears in a sector read from the disk into a file header containing all the information about the file:

Code: Select all

VAR
  buf: Buffer;
  fh: FileDir.FileHeader;
...
...
  fh := SYSTEM.VAL(FileDir.FileHeader, buf.data);

Locked