What it the preferred method to write a class to a file?

Stewart Gordon smjg_1998 at yahoo.com
Sun Jul 23 04:56:01 PDT 2006


Charles D Hixson wrote:
> I'm thinking that I should build two methods, possibly
> called write and print where write would export a "binary"
> version of the class that is not intended to be human
> readable and print would export something with, e.g.,
> numbers translated into strings.
> 
> It appears as if the methods should be instance methods of
> the class.  It looks as if write should be implemented
> something like:
> void write(Stream stream)
> {  stream.writeExact(&this, this.sizeof);	}

That won't work at all.  Because classes have reference semantics, all 
it'll do is write out the memory address of the object.

Even so, the arrangement of members within a class isn't guaranteed:

http://www.digitalmars.com/d/class.html
"The D compiler is free to rearrange the order of fields in a class to 
optimally pack them in an implementation-defined manner. Consider the 
fields much like the local variables in a function - the compiler 
assigns some to registers and shuffles others around all to get the 
optimal stack frame layout. This frees the code designer to organize the 
fields in a manner that makes the code more readable rather than being 
forced to organize it according to machine optimization rules. Explicit 
control of field layout is provided by struct/union types, not classes."

Moreover, every object includes a pointer to the vtable, which will 
screw things up when the program is run again and the data is read back 
in.  Add to that any pointers, dynamic arrays or object references that 
your class may contain....

> Though probably in this version I'd want to include
> delimiters, a type id, and a length to facilitate writing
> the corresponding read routing (which would need to be a
> class method).

You need to define a data format that includes all the information that 
is needed in order to reconstruct the object.  Start with a type ID of 
your own devising (if there's any chance that more than one type fits 
the context), and write out each member.  For primitive types, static 
arrays and structs that have no reference-semantics members, this is 
trivial.  For dynamic arrays, write out the length followed by the 
contents.  For object references within the class, follow this principle 
recursively.  Be careful of any potential circularity.

> It this the best approach?   Also are there any suggestions
> as to a reasonable way to specify the type id, so that it
> would be the same from run to run? 

Only the one you suggest next:

> Should I keep track of the ids myself (manually)?
<snip>

Yes.

Stewart.

-- 
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/M d- s:-@ C++@ a->--- UB@ P+ L E@ W++@ N+++ o K-@ w++@ O? M V? PS- 
PE- Y? PGP- t- 5? X? R b DI? D G e++++ h-- r-- !y
------END GEEK CODE BLOCK------

My e-mail is valid but not my primary mailbox.  Please keep replies on 
the 'group where everyone may benefit.



More information about the Digitalmars-d-learn mailing list