You are looking at historical revision 16261 of this page. It may differ significantly from its current revision.

blob-record

Description

blob-record defines a macro to perform conversion between Scheme records and their representation as byte-blob|byte blobs.

The syntax of a record-type definition is loosely based on SRFI-9:

<record type definition>
   -> (define-blob-record <type name>
 ( <predicate name> <constructor name> <size name> )
        ( <blob->record name> <record->blob name> )
 <field spec> ...)
<field spec> -> ( <field tag> <size in bytes> <accessor name> 
                  <field->blob name> <blob->field name> )
<field tag> -> <identifier>
<... name>  -> <identifier>

An instance of define-blob-record is equivalent to the following definitions:

   * {{<type name>}} is bound to a representation of the record type
     itself.
   * {{<constructor name>}} is bound to a procedure that takes as many
     arguments as there are {{<field spec>}} elements and returns a
     new {{<type name>}} record. 
   * {{<predicate name>}} is a predicate that returns #T when given a
     value returned by {{<constructor name>}} and #F for everything
     else.
   * {{<size name>}} is the name of a variable that contains the
     total size of the record blob representation in bytes.
   * Each {{<field spec>}} must provide the size of bytes for the
     byte blob representation of that field.
   * Each {{<field spec>}} must also provide the names of procedures
     that can convert the field value to and from byte blobs. 
   * Each {{<accessor name>}} is a procedure that takes a record of
     type {{<type name>}} and returns the current value of the
     corresponding field. It is an error to pass an accessor a value
     which is not a record of the appropriate type.

Example

(use srfi-4 byte-blob blob-record test)
(define-blob-record testrec
  (testrec? make-testrec testrec-size)
  (blob->testrec testrec->blob)
  (x  8 testrec-x s16vector->byte-blob byte-blob->s16vector)
  (y  4 testrec-y string->byte-blob byte-blob->string)
  (z  12 testrec-z f32vector->byte-blob byte-blob->f32vector))

(define-record-printer (testrec x out)
  (fprintf out "#<testrec x=~A y=~A z=~A>"
 	   (testrec-x x)
 	   (testrec-y x)
	   (testrec-z x)))

(define a (make-testrec (s16vector 1 3 5 7) "blah" (f32vector 4.0 5.0 6.0)))

(test-group "blob-record test" 

            (test (->string a)
                  (->string a)
 	  (->string (blob->testrec (testrec->blob a))))
     )
 

Version History

License

 Copyright 2009 Ivan Raikov.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are
 met:
 
 Redistributions of source code must retain the above copyright
 notice, this list of conditions and the following disclaimer.
 
 Redistributions in binary form must reproduce the above copyright
 notice, this list of conditions and the following disclaimer in the
 documentation and/or other materials provided with the distribution.
 
 Neither the name of the author nor the names of its contributors may
 be used to endorse or promote products derived from this software
 without specific prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 OF THE POSSIBILITY OF SUCH DAMAGE.