On Wed, 05 Sep 2001 15:20:23 +0200, in qdn.public.qnxrtp.devtools you
wrote:
< (…)
To clarify: the term “passivation/activation” is sometimes
used in the context of the Objective-C language, and basically means
saving and restoring objects to/from a file. The requirement for the
objects to be global is in fact not necessary, but for object with
pointers the method won’t work anyway. Furthermore, each object may
contain a hidden pointer (or other kind of information) that binds it
to its class, and this is why the method won’t work when the objects
are saved and restored by different programs.
For a simple solution, start with a function that takes two
arguments: a stream (pointer) and a (pointer to) an instance of a
class. Save the instance to the stream member by member (field by
field). You may choose text or binary representation; the first has an
advantage of producing readable files (but beware of the double type),
the second is faster but less portable. For each “nonstandard” field
type write another function that saves it to a stream. For pointers,
you have to save the information whether it was NULL, and then (if
not) the contents - using the same method. Since you are in C++, the
functions may be operators or methods of the same name, distinguished
by the type of the arguments. Define a(n) fstream superclass with new
saving methods or operators, see the definition of the “<<” operator,
for example. If you don’t know the exact type of the object a field
references, make the storing methods virtual. If your objects contain
circular references, provide an explicit solution.
For reading in, use the same approach. Each time an object is
to be read, invoke a constructor that creates an instance of the
proper class, and then read and assign its fields, one by one (it may
me the responsibility of the constructor itself). If you don’t know
the sequence the objects are stored in, let the saving methods provide
a class identifier in front of each stored object. If the objects are
already declared, you don’t have to invoke constructors.
If objects are read in by a different program, ensure it links
each class that may be needed. With the reading method explicitly
referencing the class constructors, it is guaranteed, but not always
desirable (because of the size of the program).
It is hard to write a general object saver/reader in C++ that
solves the type problem and the circular reference problem, but not
impossible, at some cost.
ako