[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
6.3 Serialization
- bigloo procedure: string->obj string
This function converts a string which has been produced by
obj->string
into a Bigloo object.
- bigloo procedure: obj->string object
This function converts into a string any Bigloo object which does not contain a procedure.
The implementation of the last two functions ensures that for every
Bigloo object obj
(containing no procedure), the expression:
(equal? obj (string->obj (obj->string obj))) ⇒ #t
- bigloo procedure: binary-port? obj
- bigloo procedure: open-output-binary-file file-name
- bigloo procedure: append-output-binary-file file-name
- bigloo procedure: open-input-binary-file file-name
- bigloo procedure: close-binary-port binary-port
- bigloo procedure: flush-binary-port binary-port
- bigloo procedure: input-obj binary-port
- bigloo procedure: output-obj binary-port obj
Bigloo allows Scheme objects to be dumped into, and restored from, files. These operations are performed by the previous functions. The dump and the restore use the two functions
obj->string
andstring->obj
.It is also possible to use a binary file as a flat character file. This can be done by the means of
output-char
,input-char
,output-string
, andinput-string
functions.
- bigloo procedure: input-char binary-port
- bigloo procedure: output-char binary-port char
- bigloo procedure: output-byte binary-port byte
The function
input-char
reads a single character from a binary-port. It returns the read character or theend-of-file
object. The functionoutput-char
andoutput-byte
writes a character, respectively a byte, into a binary-port.
- bigloo procedure: input-string binary-port len
- bigloo procedure: output-string binary-port
The function
input-string
reads a string from a binary-port of maximum length len. It returns a newly allocated string whose length is possibly smaller than len. The functionoutput-string
writes a string into a binary-port.
- bigloo procedure: input-fill-string! binary-port string
Fills a string with characters read from binary-port with at most the length of string. The function returns the number of filled characters.
- bigloo procedure: register-procedure-serialization! serializer unserializer
- bigloo procedure: register-custom-serialization! ident serializer unserializer
- bigloo procedure: register-process-serialization! serializer unserializer
- bigloo procedure: register-opaque-serialization! serializer unserializer
There is no existing portable method to dump and restore a procedure. Thus, if
obj->string
is passed a procedure, it will emit an error message. Sometime, using strict restrictions, it may be convenient to use an ad-hoc framework to serialize and unserialize procedures. User may specify there own procedure serializer and unserializer. This is the role ofregister-procedure-serialization!
. The argument serializer is a procedure of one argument, converting a procedure into a characters strings. The argument unserializer is a procedure of one argument, converting a characters string into a procedure. It belongs to the user to provide correct serializer and unserializer.Here is an example of procedure serializer and unserializer that may be correct under some Unix platform:
(module foo (extern (macro %sprintf::int (::string ::string ::procedure) "sprintf"))) (define (string->procedure str) (pragma "(obj_t)(strtoul(BSTRING_TO_STRING($1), 0, 16))" str)) (define (procedure->string proc) (let ((item (make-string 10))) (%sprintf item "#p%lx" proc) item)) (register-procedure-serialization! procedure->string string->procedure) (let ((x 4)) (let ((obj (cons "toto" (lambda (y) (+ x y))))) (let ((nobj (string->obj (obj->string obj)))) (print ((cdr nobj) 5)))))
- bigloo procedure: register-class-serialization! class serializer unserializer
Register a serializer/unserializer for a class. Subclasses of class inherit this serializer.
(module class-serialization-example (static (class point::object (x (default 10)) (y (default 20))))) (register-class-serialization! point (lambda (o) (with-access::point o (x y) (cons x y))) (lambda (l) (instantiate::point (x (car l)) (y (cdr l))))) (let ((o (instantiate::point))) (let ((s (obj->string (list o o)))) (print (string-for-read s)) (let ((l (string->obj s))) (print l) (eq? (car l) (cadr l))))) ⇒ #t
- bigloo procedure: get-procedure-serialization
- bigloo procedure: get-custom-serialization ident
- bigloo procedure: get-process-serialization
- bigloo procedure: get-opaque-serialization
- bigloo procedure: get-class-serialization class
Returns the a multiple-values whose first element is the current procedure serializer and whose second element is the current procedure unserializer. If no serializer/unserializer is defined, these procedures return the values
#f #f
.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated on March 31, 2014 using texi2html 5.0.