PersistentObject
that supports reading and writing
of arbitrary class objects to a stream (p. 202) using the static functions
template <class T>
PersistentObject::write(ostream&, T*)
PersistentObject* PersistentObject::read(ostream&)
typeinfo
(RTTI) followed by the output of a user defined
operator<<(ostream&, const T&)
The result of PersistentObject::read(ostream&)
can be dynamically cast to T*
because, for any class type
T
, Persistent<T>
is derived
from both PersistentObject
and T
.
In fact the ``real'' return type of
PersistentObject::read()
is
Persistent<T>*
, ensuring that e.g.
dynamic_cast<T*>(PersistentObject::read(istream&))
will return 0
or a pointer to the T
-object
read from the istream
.
For reading back objects, the system has to know which classes
correspond to the typeinfo strings. This information must be
supplied by calling Persistent<T>::register_class()
for each class T
for which objects may be saved/read. The mapping
is stored in a global PersistentObject::registry_
which is defined in persistent.C.
The files persistent_write.C
and persistent_read.C illustrate
the use of PersistentObject
by writing and reading
back a heterogeneous container of
Person
(person.h) and
Student
(student.h)
objects.