Flattening and resurrecting objects

Streaming objects to a serial form is usually referred to as converting objects to a flat form, or as flattening objects to a stream. Flattening objects is what allows them to have a persistent state which can span across tasks, processes, sessions, and individual machines. Instances of classes derived from MCollectible can be flattened to a persistent state using the function FlattenPointer, which is declared in the abstract class TStream.

FlattenPointer passes over the structure of an object as well as all objects referenced by its instance variables. This traversal of the object's structure continues recursively as individual object components are written out to the specified stream. The result is a linear (flattened) sequence of bytes representing the various components found in the original object. This flattened form of the object is the persistent representation of the object that can be sent to other CPUs, shared by tasks, or other application programs, libraries, or servers.

The reverse process of this flattening of objects is referred to as resurrecting persistent objects. When a persistent object is resurrected, it is restored to a state in memory that is identical to its state at the time it was stored. The Resurrect member function defined in TStream takes a flattened persistent object and creates a new MCollectible object as a result.

The flatten and resurrect operations are represented in Figure 22


Instances that contain pointers to other instances (nested instances) can be written polymorphically and completely recreated upon reading back from the stream. Multiple references to the same instance are restored properly. Circular references are also correctly handled by the system. Sets of instances can be streamed out (written) and streamed in (read) together, keeping the correct relationships and data.

Streams do not address where instances are stored, how they are found, indexing into a database of instances, or garbage collection of persistent instances.

The architecture of Streams is set up to allow efficient representations of flattened instances that are ephemeral or temporary (for example a microkernel message) as well as efficient representations of flattened instances which are persistent.

The flattening process considers the lifetime of a persistent stream when generating a flattened form for an object, or set of objects. The system also lets you provide hints concerning the nature of the object's structure.

For example, if you know that the structure of an instance is a tree, you can give hints to the flattening mechanism by providing a streaming context. It helps a streamer to know that there are no duplicate items in a set of objects being streamed or that there are no circular pointer references among objects.

You don't always need to make calls to FlattenPointer or Resurrect directly. Often you will use operator>>= to flatten an object or collection of objects and operator<<= to resurrect objects stored in a persistent stream. It is important to know, however, that the main work taking place behind the operator interface for streams is carried out by FlattenPointer and Resurrect. The importance of this knowledge becomes apparent primarily when you implement streaming operations for your own newly defined classes.


[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker