Tools.h++ Manual

104011 Tandem Computers Incorporated 17-15
17
is used to calculate the number of bytes necessary to store an object using
RWFile
. This is useful for classes
RWFileManager
and
RWBTreeOnDisk
which require space to be allocated for an object before it can be stored.
Writing a version of
binaryStoreSize()
is usually very straightforward.
Just follow the pattern set by
saveGuts(RWFile&)
, except that instead of
saving member data, add up their sizes. The only real difference is a syntactic
one: instead of l-shift operators, you use member functions and
sizeof()
:
For primitives, use
sizeof()
;
For objects that inherit from
RWCollectable
, use member function
RWspace RWCollectable::recursiveStoreSize()
;
For other objects, use member function
binaryStoreSize()
.
Here’s a sample definition for class
Bus
:
Persisting custom collections
Class
RWCollection
has versions of
saveGuts()
and
restoreGuts()
built
into it that are sufficient for most collection classes.
RWCollection::saveGuts()
works by repeatedly calling
RWvostream& operator<<(RWvostream&, const
RWCollectable&);
for each item in the collection. Similarly,
RWCollection::restoreGuts()
works by repeatedly calling
RWspace Bus::binaryStoreSize() const
{
RWspace count = RWCollectable::binaryStoreSize() +
customers_.recursiveStoreSize() +
sizeof(busNumber_) +
driver_.binaryStoreSize();
if (passengers_)
count += passengers_->recursiveStoreSize();
return count;
}