[comp.lang.c++] Objectifying contents read in from disk

thanh@athertn.Atherton.COM (Thanh Diec) (07/24/89)

    After objects are written out to disk, and then read back in again,
possibly by a different program, what does the reader need to do to set the
content read in to behave correctly as objects?  For example, the runtime
_vptr's in the writer program are going to be different from the reader's,
even though both programs include the same ".h".

Writer:

  // write out an array of 1024 objects 

  write(fd, &object_array[0], sizeof(class1) * 1024);

  
Reader:

  read(fd, buf, sizeof(class1) * 1024);
  
  class1 *p1 = (class1 *)buf;		

  // buf is not ready to used as objects yet, must set runtime values
  // such as _vptr's

  for (idx = 0; idx < 1024; idx++)
  {
    // ??? what to here without doing a second copy into a correctly 
    // initialized dummy object.
    
    dummy_obj->member1 = (p1+idx)->member1;
    dummy_obj->member2 = (p1+idx)->member2;
    dummy_obj->member3 = (p1+idx)->member3;
    
  }
    

Thanks in advance,

Thanh Diec
Atherton Technology	(408)734-9822
thanh@atherton.com
{hpda,sun,decwrl}!athertn!thanh

    

bright@Data-IO.COM (Walter Bright) (07/25/89)

In article <7316@athertn.Atherton.COM> thanh@athertn.Atherton.COM (Thanh Diec) writes:
<    After objects are written out to disk, and then read back in again,
<possibly by a different program, what does the reader need to do to set the
<content read in to behave correctly as objects?  For example, the runtime
<_vptr's in the writer program are going to be different from the reader's,
<even though both programs include the same ".h".
<Writer:
<  write(fd, &object_array[0], sizeof(class1) * 1024);
<Reader:
<  read(fd, buf, sizeof(class1) * 1024);

This will not work unless class1 has no virtual functions and no
virtual base classes. It also violates the C++ paradigm that only
a constructor can create an instance of a class (read() is creating
the instances).

The solution is to define a member function to write out the object,
and a constructor to read it in. Unfortunately, this can be significantly
less efficient than the write()/read() above. If you know what you're
doing and are willing to modify your code for new compilers, you can
use the write()/read() above.

dog@cbnewsl.ATT.COM (edward.n.schiebel) (07/25/89)

From article <7316@athertn.Atherton.COM>, by thanh@athertn.Atherton.COM (Thanh Diec):
.. stuff deleted
> Writer:
> 
>   // write out an array of 1024 objects 
> 
>   write(fd, &object_array[0], sizeof(class1) * 1024);
> 
>   
> Reader:
> 
>   read(fd, buf, sizeof(class1) * 1024);
>   
>   class1 *p1 = (class1 *)buf;		
> 
>   // buf is not ready to used as objects yet, must set runtime values
>   // such as _vptr's
> 
In our project, each class that may need to have instances read and write
themselves have read and write member functions which read and write
the member data.  On a read, the application creates an instance, then
calls the read function to fill it up.  There are a couple of small
problems:
	- this is not as fast as a block read like your example, but
	  then it doesn't need vptr fixing.
	- the object may have to output small headers ahead of some
	  member data as a type field
	  if the data is a pointer to a base class which may actually
	  pont to a derived class object.  If this is true, the object 
	  needs to know what type to create to call the proper reader.
	
  Ed Schiebel