From: Thomas A. Russ on
Krzysztof Drewniak <krzysdrewniakNOSPAM(a)gmail.com> writes:

> Hi all,
>
> I am going to try and write a roguelike in CL. To implement
> saving/loading, I would like to implement saves by 'print'ing and
> 'read'ing from a file. However, I will be using CLOS, and the
> standard 'print-object' is useless in this situation. Could someone
> please provide code or pointers to code that lets you print an object
> (and all of its slots) and read it back in, without losing any values.

Well, the simplest solution would be to use DEFSTRUCT instead to build
your hierarchy of structures. It comes with read syntax that will work
correctly. And it will even work with circular structures, if you set
*PRINT-CIRCLE* to T.

You can still write methods that dispatch on the types of your
structures. You do give up the ability to do multiple inheritance in
your class hierarchy, but that may be worth it to be able to avoid
having to write code that can properly store and read in especially
circular structures.

Otherwise, you could look at the MOP-level functions to find out what
slots are on your instance and write something that you will later be
able to load. This will, in general, not be easily done if you have to
support circular structures, because circularity is handled at the read
stage and without standard syntax that the reader understands you can't
really do things then.



--
Thomas A. Russ, USC/Information Sciences Institute
From: piscesboy on
On Mar 19, 7:10 pm, Krzysztof Drewniak <krzysdrewniakNOS...(a)gmail.com>
wrote:
> Hi all,
>
>    I am going to try and write a roguelike in CL. To implement
> saving/loading, I would like to implement saves by 'print'ing and
> 'read'ing from a file. However, I will be using CLOS, and the
> standard 'print-object' is useless in this situation. Could someone
> please provide code or pointers to code that lets you print an object
> (and all of its slots) and read it back in, without losing any values.
>
> Thanks,
>
> Krzysztof Drewniak
>
> --
> X-Real-Email-With-Antispam: krzysdrewniak at gmail dot com
> pgp key on keyserver.ubuntu.com and maybe some other place too

I've been working on a library to parse CLOS classes into XML output
format and read them back again into CLOS structures. I've come pretty
far, I just need to refactor some of the algorithms for parsing the
information back into CLOS classes and generalize the functions to
work with any CLOS class.
From: vanekl on
Krzysztof Drewniak wrote:
> Hi all,
>
> I am going to try and write a roguelike in CL. To implement
> saving/loading, I would like to implement saves by 'print'ing and
> 'read'ing from a file. However, I will be using CLOS, and the
> standard 'print-object' is useless in this situation. Could someone
> please provide code or pointers to code that lets you print an object
> (and all of its slots) and read it back in, without losing any values.
>
> Thanks,
>
> Krzysztof Drewniak

you may find this useful.
requires installation of cl-store.

CL-USER>(require :cl-store)
....
; compilation unit finished
; printed 65 notes
("CL-STORE")
CL-USER> (defclass foo ()
((bar :accessor bar :initarg :bar)))
#<STANDARD-CLASS FOO>
CL-USER> (cl-store:store (make-instance 'foo :bar "bar") "baz.store")
#<FOO {B222709}>
CL-USER> (cl-store:restore "baz.store")
#<FOO {B6CEAF9}>
CL-USER> (bar *)
"bar"
CL-USER> (type-of **)
FOO



From: Raffael Cavallaro on
On 2010-03-19 23:06:46 -0400, piscesboy said:

> I've been working on a library to parse CLOS classes into XML output
> format and read them back again into CLOS structures.

I'm starting to sound like a broken record, but did you realize that
the s-serialization library which is associated with/part of
cl-prevalence already does this?

warmest regards,

Ralph

--
Raffael Cavallaro

From: Raffael Cavallaro on
On 2010-03-20 00:19:26 -0400, Raffael Cavallaro said:

> I'm starting to sound like a broken record, but did you realize that
> the s-serialization library which is associated with/part of
> cl-prevalence already does this?

Sorry, forgot the obligatory minimal examples:

sexp:

? (serialize-sexp (progn (defclass foo () ((bar :accessor bar :initarg
:bar) (quux :accessor quux :initarg :quux))) (make-instance 'foo :bar 3
:quux 28)) *standard-output*)
(:OBJECT 1 :CLASS COMMON-LISP-USER::FOO :SLOTS ( (COMMON-LISP-USER::BAR
.. 3) (COMMON-LISP-USER::QUUX . 28) ) )
" ) )"

xml:

? (serialize-xml (progn (defclass foo () ((bar :accessor bar :initarg
:bar) (quux :accessor quux :initarg :quux))) (make-instance 'foo :bar 3
:quux 28)) *standard-output*)
<OBJECT ID="1" CLASS="COMMON-LISP-USER::FOO"><SLOT
NAME="COMMON-LISP-USER::BAR"><INT>3</INT></SLOT><SLOT
NAME="COMMON-LISP-USER::QUUX"><INT>28</INT></SLOT></OBJECT>
"</OBJECT>"

warmest regards,

Ralph

--
Raffael Cavallaro